mirror of
https://github.com/lukasabbe/bookshelf-inspector.git
synced 2026-04-30 10:40:53 +00:00
graddle update
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
package me.lukasabbe.bookshelfinspector.util;
|
||||
|
||||
import me.lukasabbe.bookshelfinspector.Bookshelfinspector;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.block.entity.ChiseledBookshelfBlockEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class BookshelfTools {
|
||||
public static ItemStack getItemById(BlockPos pos, int slotNum, PlayerEntity player){
|
||||
final World world = Bookshelfinspector.serverInstance.getPlayerManager().getPlayer(player.getUuid()).getWorld();
|
||||
|
||||
if(world == null) return null;
|
||||
Optional<ChiseledBookshelfBlockEntity> blockEntityOptional = world.getBlockEntity(pos,BlockEntityType.CHISELED_BOOKSHELF);
|
||||
if(blockEntityOptional.isEmpty()) return null;
|
||||
|
||||
ChiseledBookshelfBlockEntity blockEntity = blockEntityOptional.get();
|
||||
|
||||
final ItemStack stack = blockEntity.getStack(slotNum);
|
||||
if(stack.isEmpty()) return null;
|
||||
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package me.lukasabbe.bookshelfinspector.util;
|
||||
|
||||
import me.lukasabbe.bookshelfinspector.BookshelfinspectorClient;
|
||||
import me.lukasabbe.bookshelfinspector.data.BookData;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.font.TextRenderer;
|
||||
import net.minecraft.client.gui.DrawContext;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.component.DataComponentTypes;
|
||||
import net.minecraft.component.type.ItemEnchantmentsComponent;
|
||||
import net.minecraft.enchantment.Enchantment;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.registry.entry.RegistryEntry;
|
||||
import net.minecraft.registry.tag.EnchantmentTags;
|
||||
import net.minecraft.text.MutableText;
|
||||
import net.minecraft.text.Style;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.text.Texts;
|
||||
import net.minecraft.util.Formatting;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class HudRenderer {
|
||||
public static void hudRender(DrawContext context, MinecraftClient client){
|
||||
if(!BookshelfinspectorClient.modAvailable) return;
|
||||
|
||||
if(client.options.hudHidden) return;
|
||||
|
||||
if(BookshelfinspectorClient.bookShelfData.isCurrentBookDataToggled){
|
||||
final BookData currentBookData = BookshelfinspectorClient.currentBookData;
|
||||
int screenWidth = client.getWindow().getScaledWidth();
|
||||
int screenHeight = client.getWindow().getScaledHeight();
|
||||
int x = screenWidth / 2;
|
||||
int y = screenHeight / 2;
|
||||
final ItemStack itemStack = currentBookData.itemStack;
|
||||
int color = 0xFFFFFFFF;
|
||||
|
||||
final Integer colorValue = itemStack.getRarity().getFormatting().getColorValue();
|
||||
if(colorValue != null){
|
||||
color = colorValue;
|
||||
}
|
||||
|
||||
float scaleFactor = ((float) BookshelfinspectorClient.config.scale /10);
|
||||
drawScaledText(context, itemStack.getName(), x,y+((int)(10*scaleFactor)), color, client.textRenderer);
|
||||
|
||||
ItemEnchantmentsComponent storedComponents = itemStack.getComponents().get(DataComponentTypes.STORED_ENCHANTMENTS);
|
||||
if(storedComponents != null){
|
||||
int i = ((int)(20*scaleFactor));
|
||||
for(RegistryEntry<Enchantment> enchantment : storedComponents.getEnchantments()){
|
||||
String lvl = "";
|
||||
final int level = storedComponents.getLevel(enchantment);
|
||||
if(level != 1)
|
||||
lvl = String.valueOf(level);
|
||||
final MutableText enchantmentText;
|
||||
|
||||
if(!BookshelfinspectorClient.config.useRoman || level == -1)
|
||||
enchantmentText = enchantment.value().description().copy().append(" " + lvl);
|
||||
else if (level != 1)
|
||||
enchantmentText = enchantment.value().description().copy().append(" " + RomanNumerals.toRoman(level));
|
||||
else
|
||||
enchantmentText = enchantment.value().description().copy();
|
||||
|
||||
if(enchantment.isIn(EnchantmentTags.CURSE)) {
|
||||
Texts.setStyleIfAbsent(enchantmentText, Style.EMPTY.withColor(Formatting.RED));
|
||||
}else {
|
||||
Texts.setStyleIfAbsent(enchantmentText, Style.EMPTY.withColor(Formatting.GRAY));
|
||||
}
|
||||
drawScaledText(context, enchantmentText, x,y+i, 0xFFFFFFFF,client.textRenderer);
|
||||
i+=(int)(10*scaleFactor);
|
||||
}
|
||||
}
|
||||
|
||||
var writtenBookContentComponent = itemStack.getComponents().get(DataComponentTypes.WRITTEN_BOOK_CONTENT);
|
||||
|
||||
if(writtenBookContentComponent != null){
|
||||
drawScaledText(context, Text.translatable("book.byAuthor",writtenBookContentComponent.author()), x,y+(int)(20*scaleFactor), 0xFFFFFFFF,client.textRenderer);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
private static void drawScaledText(DrawContext context, Text text, int centerX, int y, int color, TextRenderer textRenderer){
|
||||
MatrixStack stack = context.getMatrices();
|
||||
stack.push();
|
||||
stack.translate(centerX,y,0);
|
||||
final float scale = (float) BookshelfinspectorClient.config.scale / 10;
|
||||
stack.scale(scale, scale, scale);
|
||||
stack.translate(-centerX,-y,0);
|
||||
context.drawCenteredTextWithShadow(textRenderer,text,centerX,y,color);
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package me.lukasabbe.bookshelfinspector.util;
|
||||
|
||||
import me.lukasabbe.bookshelfinspector.BookshelfinspectorClient;
|
||||
import me.lukasabbe.bookshelfinspector.data.BookData;
|
||||
import me.lukasabbe.bookshelfinspector.mixin.BookshelfInvoker;
|
||||
import me.lukasabbe.bookshelfinspector.network.packets.BookShelfInventoryRequestPayload;
|
||||
import me.lukasabbe.bookshelfinspector.network.packets.LecternInventoryRequestPayload;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
|
||||
import net.fabricmc.fabric.api.tag.convention.v2.ConventionalBlockTags;
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.block.ChiseledBookshelfBlock;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.registry.tag.BlockTags;
|
||||
import net.minecraft.util.hit.BlockHitResult;
|
||||
import net.minecraft.util.hit.HitResult;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
|
||||
import java.util.OptionalInt;
|
||||
|
||||
import static me.lukasabbe.bookshelfinspector.BookshelfinspectorClient.*;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class Inspector {
|
||||
public void inspect(MinecraftClient client){
|
||||
if(!modAvailable) return;
|
||||
|
||||
if(client.cameraEntity == null || client.player == null) return;
|
||||
|
||||
//Send raycast max 5 blocks
|
||||
HitResult hit = client.cameraEntity.raycast(5f,0f,false);
|
||||
|
||||
//find block hit, if not found block returns
|
||||
final HitResult.Type type = hit.getType();
|
||||
if(type != HitResult.Type.BLOCK) {
|
||||
resetBookShelfData();
|
||||
return;
|
||||
}
|
||||
|
||||
final BlockHitResult blockHitResult = (BlockHitResult) hit;
|
||||
BlockPos pos = blockHitResult.getBlockPos();
|
||||
|
||||
if(bookShelfData.latestPos == null)
|
||||
bookShelfData.latestPos = pos;
|
||||
|
||||
//If you look at a new block
|
||||
if(!bookShelfData.latestPos.equals(pos)){
|
||||
resetBookShelfData();
|
||||
currentBookData = BookData.empty();
|
||||
}
|
||||
bookShelfData.latestPos = pos;
|
||||
|
||||
|
||||
if(client.player.getWorld().getBlockState(pos).isOf(Blocks.CHISELED_BOOKSHELF)){
|
||||
bookShelfInspect(pos, blockHitResult, client);
|
||||
}else if(client.player.getWorld().getBlockState(pos).isOf(Blocks.LECTERN) && config.lecternToggle){
|
||||
lecternInspect(pos);
|
||||
}else{
|
||||
|
||||
bookShelfData.requestSent = false; // Just for servers that don't have the latest version of mod
|
||||
|
||||
if(!bookShelfData.isCurrentBookDataToggled) return;
|
||||
resetBookShelfData();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void lecternInspect(BlockPos pos){
|
||||
|
||||
//Checks if there is saved data.
|
||||
final BookData currentBookData = BookshelfinspectorClient.currentBookData;
|
||||
|
||||
if(currentBookData.pos != null && currentBookData.pos.equals(pos)) return;
|
||||
|
||||
if(!bookShelfData.requestSent){
|
||||
bookShelfData.requestSent = true;
|
||||
ClientPlayNetworking.send(new LecternInventoryRequestPayload(pos));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void bookShelfInspect(BlockPos pos, BlockHitResult blockHitResult, MinecraftClient client){
|
||||
final BlockState blockState = client.player.getWorld().getBlockState(pos);
|
||||
|
||||
//Gets index position for a book in the bookshelf
|
||||
ChiseledBookshelfBlock bookshelfBlock = (ChiseledBookshelfBlock) blockState.getBlock();
|
||||
OptionalInt optionalInt = ((BookshelfInvoker)bookshelfBlock).invokerGetSlotForHitPos(blockHitResult,blockState);
|
||||
|
||||
//if the position is empty, return
|
||||
if(optionalInt.isEmpty()) {
|
||||
resetBookShelfData();
|
||||
return;
|
||||
}
|
||||
|
||||
//Checks if there is saved data.
|
||||
final BookData currentBookData = BookshelfinspectorClient.currentBookData;
|
||||
|
||||
//Changes the id for the new one if it's new.
|
||||
final int temp = bookShelfData.currentSlotInt;
|
||||
final int slotNum = optionalInt.getAsInt();
|
||||
bookShelfData.currentSlotInt = slotNum;
|
||||
|
||||
if(currentBookData.slotId!= slotNum && currentBookData.slotId!=-2 && !bookShelfData.requestSent){
|
||||
bookShelfData.requestSent = true;
|
||||
ClientPlayNetworking.send(new BookShelfInventoryRequestPayload(pos, slotNum));
|
||||
}
|
||||
else {
|
||||
if(temp == slotNum)
|
||||
bookShelfData.isCurrentBookDataToggled = currentBookData.slotId != -2;
|
||||
else{
|
||||
bookShelfData.isCurrentBookDataToggled = false;
|
||||
BookshelfinspectorClient.currentBookData = BookData.empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void resetBookShelfData(){
|
||||
if(!bookShelfData.isCurrentBookDataToggled) return;
|
||||
|
||||
bookShelfData.isCurrentBookDataToggled = false;
|
||||
currentBookData = BookData.empty();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package me.lukasabbe.bookshelfinspector.util;
|
||||
|
||||
import me.lukasabbe.bookshelfinspector.Bookshelfinspector;
|
||||
import net.minecraft.block.entity.BlockEntityType;
|
||||
import net.minecraft.block.entity.LecternBlockEntity;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class LecternTools {
|
||||
public static ItemStack getItemStack(BlockPos pos, PlayerEntity player){
|
||||
final World world = Bookshelfinspector.serverInstance.getPlayerManager().getPlayer(player.getUuid()).getWorld();
|
||||
|
||||
if(world == null) return null;
|
||||
Optional<LecternBlockEntity> blockEntityOptional = world.getBlockEntity(pos, BlockEntityType.LECTERN);
|
||||
if(blockEntityOptional.isEmpty()) return null;
|
||||
|
||||
LecternBlockEntity lecternBlock = blockEntityOptional.get();
|
||||
|
||||
return lecternBlock.getBook();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package me.lukasabbe.bookshelfinspector.util;
|
||||
|
||||
import java.util.TreeMap;
|
||||
|
||||
//https://stackoverflow.com/questions/12967896/converting-integers-to-roman-numerals-java
|
||||
public class RomanNumerals {
|
||||
private final static TreeMap<Integer, String> map = new TreeMap<>();
|
||||
static {
|
||||
map.put(1000, "M");
|
||||
map.put(900, "CM");
|
||||
map.put(500, "D");
|
||||
map.put(400, "CD");
|
||||
map.put(100, "C");
|
||||
map.put(90, "XC");
|
||||
map.put(50, "L");
|
||||
map.put(40, "XL");
|
||||
map.put(10, "X");
|
||||
map.put(9, "IX");
|
||||
map.put(5, "V");
|
||||
map.put(4, "IV");
|
||||
map.put(1, "I");
|
||||
}
|
||||
|
||||
|
||||
public static String toRoman(int number){
|
||||
int key = map.floorKey(number);
|
||||
|
||||
if(number == key){
|
||||
return map.get(number);
|
||||
}
|
||||
|
||||
return map.get(key) + toRoman(number-key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user