Made alot of features and fixed bugs

This commit is contained in:
lukasabbe
2024-11-13 20:58:22 +01:00
parent 147168e8a5
commit 1787860ed6
8 changed files with 103 additions and 33 deletions
@@ -17,6 +17,7 @@ import java.util.Map;
public class Config {
public boolean lecternToggle = true;
public int scale = 10;
public boolean useRoman = false;
public void loadConfig(){
Path configPath = FabricLoader.getInstance().getConfigDir().resolve("bookshelfinspector-config.yml");
@@ -30,6 +31,9 @@ public class Config {
if(configMap.containsKey("scale")){
scale = (int) configMap.get("scale");
}
if(configMap.containsKey("roman")){
useRoman = (boolean) configMap.get("roman");
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
@@ -55,6 +59,7 @@ public class Config {
Map<String, Object> configMap = yaml.load(new FileReader(configPath.toFile()));
configMap.put("lectern-toggle",lecternToggle);
configMap.put("scale",scale);
configMap.put("roman", useRoman);
FileWriter writer = new FileWriter(configPath.toString());
yaml.dump(configMap,writer);
writer.close();
@@ -31,7 +31,14 @@ public class ModMenu implements ModMenuApi {
.startIntSlider(Text.translatable("bookshelfinspector.config.scale"),BookshelfinspectorClient.config.scale,0,20)
.setTooltip(Text.translatable("bookshelfinspector.config.scale.tooltip"))
.setDefaultValue(10).setSaveConsumer(val -> BookshelfinspectorClient.config.scale = val)
.build())
.addEntry(entryBuilder
.startBooleanToggle(Text.translatable("bookshelfinspector.config.roman_scale"), BookshelfinspectorClient.config.useRoman)
.setTooltip(Text.translatable("bookshelfinspector.config.roman_scale.tooltip"))
.setDefaultValue(false)
.setSaveConsumer(val -> BookshelfinspectorClient.config.useRoman = val)
.build());
builder.setSavingRunnable(BookshelfinspectorClient.config::saveConfig);
return builder.build();
};
@@ -52,9 +52,15 @@ public class HudRenderer {
final int level = storedComponents.getLevel(enchantment);
if(level != 1)
lvl = String.valueOf(level);
final MutableText append = enchantment.value().description().copy().append(" " + lvl);
final MutableText append;
if(!BookshelfinspectorClient.config.useRoman || level == -1)
append = enchantment.value().description().copy().append(" " + lvl);
else
append = enchantment.value().description().copy().append(" " + RomanNumerals.toRoman(level));
if(enchantment.isIn(EnchantmentTags.CURSE)) {
Texts.setStyleIfAbsent(append, Style.EMPTY.withColor(Formatting.RED));
}else {
Texts.setStyleIfAbsent(append, Style.EMPTY.withColor(Formatting.GRAY));
}
drawScaledText(context, append, x,y+i, 0xFFFFFFFF,client.textRenderer);
i+=(int)(10*scaleFactor);
@@ -12,7 +12,6 @@ import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.ChiseledBookshelfBlock;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.ChiseledBookshelfBlockEntity;
import net.minecraft.block.entity.LecternBlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.hit.BlockHitResult;
@@ -31,68 +30,75 @@ public class Inspector {
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) {
bookShelfData.isCurrentBookDataToggled = false;
currentBookData = BookData.empty();
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, client);
lecternInspect(pos);
}else{
bookShelfData.isCurrentBookDataToggled = false;
currentBookData = BookData.empty();
bookShelfData.latestPos = null;
bookShelfData.requestSent = false;
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, MinecraftClient client){
Optional<LecternBlockEntity> optionalLecternBlockEntity = client.player.getWorld().getBlockEntity(pos, BlockEntityType.LECTERN);
if(optionalLecternBlockEntity.isEmpty()){
bookShelfData.isCurrentBookDataToggled = false;
currentBookData = BookData.empty();
return;
}
private void lecternInspect(BlockPos pos){
if(bookShelfData.latestPos != null && bookShelfData.latestPos.equals(pos)){
return;
}
//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));
bookShelfData.latestPos = pos;
}
}
private void bookShelfInspect(BlockPos pos, BlockHitResult blockHitResult, MinecraftClient client){
Optional<ChiseledBookshelfBlockEntity> optionalChiseledBookshelfBlockEntity = client.player.getWorld().getBlockEntity(pos, BlockEntityType.CHISELED_BOOKSHELF);
if(optionalChiseledBookshelfBlockEntity.isEmpty()){
bookShelfData.isCurrentBookDataToggled = false;
currentBookData = BookData.empty();
return;
}
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()) {
bookShelfData.isCurrentBookDataToggled = false;
resetBookShelfData();
return;
}
//Checks if there is saved data.
final BookData currentBookData = BookshelfinspectorClient.currentBookData;
int temp = bookShelfData.currentSlotInt;
//Changes the id for the new one if it's new.
final int temp = bookShelfData.currentSlotInt;
final int slotNum = optionalInt.getAsInt();
bookShelfData.currentSlotInt = slotNum;
@@ -109,4 +115,11 @@ public class Inspector {
}
}
}
private void resetBookShelfData(){
if(!bookShelfData.isCurrentBookDataToggled) return;
bookShelfData.isCurrentBookDataToggled = false;
currentBookData = BookData.empty();
}
}
@@ -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);
}
}
@@ -4,5 +4,7 @@
"bookshelfinspector.config.lectern.toggle": "Turn OFF or ON lectern support",
"bookshelfinspector.config.lectern.toggle.tooltip": "This will turn OFF/ON the visibility of lectern inspection",
"bookshelfinspector.config.scale": "Change the scale of the text",
"bookshelfinspector.config.scale.tooltip": "Change the scale of the text by dragging the slider"
"bookshelfinspector.config.scale.tooltip": "Change the scale of the text by dragging the slider",
"bookshelfinspector.config.roman_scale": "Use roman numerals!",
"bookshelfinspector.config.roman_scale.tooltip": "Use roman numerals or arabic numerals"
}
@@ -4,5 +4,7 @@
"bookshelfinspector.config.lectern.toggle": "Stäng AV eller PÅ läspulpet stöd",
"bookshelfinspector.config.lectern.toggle.tooltip": "Denna inställning kommer stänga av eller på synligeten av läspulpet inspektion",
"bookshelfinspector.config.scale": "Ändra storleken på texten",
"bookshelfinspector.config.scale.tooltip": "Du kan göra det igenom att dra på den nedan"
"bookshelfinspector.config.scale.tooltip": "Du kan göra det igenom att dra på den nedan",
"bookshelfinspector.config.roman_scale": "Använd romerska siffror",
"bookshelfinspector.config.roman_scale.tooltip": "Använd romerska siffror eller arabiska siffror"
}
@@ -1,2 +1,3 @@
lectern-toggle: true
scale: 10
scale: 10
roman: false