diff --git a/.gitignore b/.gitignore index 12f8644..c0d09a5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,9 @@ out *.iml .idea +# vscode +.vscode + # gradle build .gradle diff --git a/build.gradle b/build.gradle index 30b9a4f..c53330d 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ plugins { id 'net.minecraftforge.gradle' version '5.1.+' } -version = '1.0.1' +version = '1.1.0' group = 'dev.zontreck.mcmods' // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = 'WatchMyDurability' diff --git a/src/main/java/dev/zontreck/mcmods/CheckInventory.java b/src/main/java/dev/zontreck/mcmods/CheckInventory.java index b6f6fbb..88237e9 100644 --- a/src/main/java/dev/zontreck/mcmods/CheckInventory.java +++ b/src/main/java/dev/zontreck/mcmods/CheckInventory.java @@ -5,10 +5,10 @@ import java.util.List; import java.util.Map; import java.util.TimerTask; +import dev.zontreck.mcmods.configs.WMDClientConfig; import net.minecraft.client.Minecraft; import net.minecraft.core.NonNullList; import net.minecraft.network.chat.Component; -import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundEvents; import net.minecraft.world.entity.player.Inventory; @@ -20,19 +20,25 @@ public class CheckInventory extends TimerTask @Override public void run() { - if(!WatchMyDurability.isInGame)return; - //WatchMyDurability.LOGGER.info("TICKING CHECK INVENTORY EVENT"); - // Get the player inventory - Inventory inv = WatchMyDurability._player.getInventory(); - - if(checkList("_armor", inv.armor)) Soundify(); - if(checkList("_items", inv.items)) Soundify(); - if(checkList("_offhand", inv.offhand)) Soundify(); - + try { + + if(!WatchMyDurability.isInGame)return; + //WatchMyDurability.LOGGER.info("TICKING CHECK INVENTORY EVENT"); + // Get the player inventory + Inventory inv = Minecraft.getInstance().player.getInventory(); + + checkList("_armor", inv.armor); + checkList("_items", inv.items); + checkList("_offhand", inv.offhand); + - PushItems("_armor", inv.armor); - PushItems("_items", inv.items); - PushItems("_offhand", inv.offhand); + PushItems("_armor", inv.armor); + PushItems("_items", inv.items); + PushItems("_offhand", inv.offhand); + } catch (Exception e) { + WatchMyDurability.LOGGER.warn(": : : : [ERROR] : : : :"); + WatchMyDurability.LOGGER.warn("A error in the WatchMyDurability timer code has occurred. This could happen with hub worlds and the server transfers that occur. If this happened in another instance, please report this error to the developer, along with what you were doing."); + } } @@ -54,14 +60,15 @@ public class CheckInventory extends TimerTask ItemRegistry.register(type,items); } - public void Soundify() + public void Soundify(SoundEvent sound) { //WatchMyDurability.LOGGER.info("PLAY ALERT SOUND"); - WatchMyDurability._player.playSound(SoundEvents.ITEM_BREAK, 1.0f, 1.0f); + Minecraft.getInstance().player.playSound(sound, 1.0f, 1.0f); } - public boolean checkList(String type, NonNullList stacks){ + public void checkList(String type, NonNullList stacks){ Integer slotNum = 0; + //boolean ret=false; for (ItemStack is1 : stacks) { if(is1.isDamageableItem() && is1.isDamaged() && !ItemRegistry.contains(type, slotNum, WatchMyDurability.REGISTRY.GetNewItem(is1))){ @@ -72,19 +79,27 @@ public class CheckInventory extends TimerTask percent = cur * 100 / max; //WatchMyDurability.LOGGER.debug("ITEM DURABILITY: "+cur+"; MAXIMUM: "+max+"; PERCENT: "+percent); - if(percent <= 10) - { - Component X = Component.literal(is1.getDisplayName().getString()+" is about to break!"); - WatchMyDurability._player.displayClientMessage(X, false); - // Play Sound - return true; - } else return false; + for (Integer entry : WMDClientConfig.alertPercents.get()) { + Integer idx = WMDClientConfig.alertPercents.get().indexOf(entry); + String entryStr = WMDClientConfig.alertMessages.get().get(idx); + + if(percent <= entry){ + String replaced = entryStr.replaceAll("!item!", is1.getDisplayName().getString()); + WatchMyDurability.LOGGER.info("Enqueue alert for an item. Playing sound for item: "+is1.getDisplayName().getString()); + + SoundEvent theSound = SoundEvents.ITEM_BREAK; + Soundify(theSound); + + Component X = Component.literal(replaced); + Minecraft.getInstance().player.displayClientMessage(X, false); + break; // Rule applies, break out of this loop, move to next item. + } + } } slotNum ++; } - - return false; + //return ret; } } diff --git a/src/main/java/dev/zontreck/mcmods/WatchMyDurability.java b/src/main/java/dev/zontreck/mcmods/WatchMyDurability.java index 5eb3493..865130b 100644 --- a/src/main/java/dev/zontreck/mcmods/WatchMyDurability.java +++ b/src/main/java/dev/zontreck/mcmods/WatchMyDurability.java @@ -1,18 +1,20 @@ package dev.zontreck.mcmods; import com.mojang.logging.LogUtils; + +import dev.zontreck.mcmods.configs.WMDClientConfig; import net.minecraft.client.Minecraft; import net.minecraft.client.User; -import net.minecraft.client.player.LocalPlayer; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.config.ModConfig.Type; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.event.server.ServerStartingEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; - import net.minecraftforge.client.event.ClientPlayerNetworkEvent; import java.util.Timer; @@ -31,7 +33,6 @@ public class WatchMyDurability /// DO NOT USE FROM ANY THIRD PARTY PACKAGES public static User CurrentUser = null; // This is initialized by the client public static boolean isInGame = false; // This locks the timer thread - public static LocalPlayer _player = null; // Updated on login! public static ItemRegistry REGISTRY; @@ -42,6 +43,7 @@ public class WatchMyDurability // Register the commonSetup method for modloading modEventBus.addListener(this::commonSetup); + ModLoadingContext.get().registerConfig(Type.CLIENT, WMDClientConfig.SPEC, "watchmydurability-client.toml"); // Register ourselves for server and other game events we are interested in MinecraftForge.EVENT_BUS.register(this); @@ -70,13 +72,13 @@ public class WatchMyDurability @SubscribeEvent public static void onClientSetup(FMLClientSetupEvent event) { - //LOGGER.info(": : : CLIENT SETUP : : :"); + LOGGER.info(": : : CLIENT SETUP : : :"); // Some client setup code //LOGGER.info("HELLO FROM CLIENT SETUP"); //LOGGER.info("MINECRAFT NAME >> {}", Minecraft.getInstance().getUser().getName()); WatchMyDurability.CurrentUser = Minecraft.getInstance().getUser(); - time.schedule(new CheckInventory(), 10000, 10000); + time.schedule(new CheckInventory(), WMDClientConfig.TimerVal.get()*1000, WMDClientConfig.TimerVal.get()*1000); ItemRegistry.Initialize(); @@ -91,14 +93,22 @@ public class WatchMyDurability public static void onJoin(ClientPlayerNetworkEvent.LoggingIn event){ // Joined //LOGGER.info("PLAYER LOGGED IN"); - WatchMyDurability._player = event.getPlayer(); + LOGGER.info(": : : PLAYER LOGGED IN : : :"); WatchMyDurability.isInGame=true; } @SubscribeEvent public static void onLeave(ClientPlayerNetworkEvent.LoggingOut event){ //LOGGER.info("PLAYER LOGGED OUT"); + LOGGER.info(": : : PLAYER LOGGED OUT : : :"); WatchMyDurability.isInGame=false; } + + @SubscribeEvent + public static void onClone(ClientPlayerNetworkEvent.Clone event) + { + LOGGER.info(": : : : PLAYER RESPAWNED OR MOVED TO A NEW WORLD : : : :"); + + } } } diff --git a/src/main/java/dev/zontreck/mcmods/configs/WMDClientConfig.java b/src/main/java/dev/zontreck/mcmods/configs/WMDClientConfig.java new file mode 100644 index 0000000..4353398 --- /dev/null +++ b/src/main/java/dev/zontreck/mcmods/configs/WMDClientConfig.java @@ -0,0 +1,33 @@ +package dev.zontreck.mcmods.configs; + +import java.util.ArrayList; +import java.util.List; + +import net.minecraftforge.common.ForgeConfigSpec; + +public class WMDClientConfig { + public static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder(); + public static final ForgeConfigSpec SPEC; + + public static ForgeConfigSpec.ConfigValue> alertPercents; + public static ForgeConfigSpec.ConfigValue> alertMessages; + public static ForgeConfigSpec.ConfigValue TimerVal; + static{ + List alerts1 = new ArrayList<>(); + alerts1.add(10); + + List alerts2 = new ArrayList<>(); + alerts2.add("!item! is about to break"); + + + BUILDER.push("Alerts"); + BUILDER.comment("Both of the following lists must have the same number of entries. NOTE: Percents do NOT stack. After the first rule is applied, it will move to the next item, so please make the list ascend, and not descend. Example: 10, 50").define("VERSION", "1.0.0"); + + alertPercents = BUILDER.comment("The list of alerts you want at what percentages of remaining durability").define("Percents", alerts1); + alertMessages = BUILDER.comment("The messages you want displayed when a alert is triggered. You must have the same amount of messages as alerts").define("Messages", alerts2); + TimerVal = BUILDER.comment("How many seconds between timer ticks to check your inventory items?").define("Timer", 5); + + BUILDER.pop(); + SPEC=BUILDER.build(); + } +} diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 115aaa9..af62d71 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -46,7 +46,7 @@ This mod watches durability of an item. This is most useful for armor or weapons to alert you to the fact that your armor or weapon might break soon. -At 10% or less of maximum durability, you will be alerted. +Edit the config file to customize the alerts ''' # A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional. [[dependencies.watchmydurability]] #optional @@ -59,7 +59,7 @@ At 10% or less of maximum durability, you will be alerted. # An ordering relationship for the dependency - BEFORE or AFTER required if the relationship is not mandatory ordering="NONE" # Side this dependency is applied on - BOTH, CLIENT or SERVER - side="BOTH" + side="CLIENT" # Here's another dependency [[dependencies.watchmydurability]] modId="minecraft" @@ -67,4 +67,4 @@ At 10% or less of maximum durability, you will be alerted. # This version range declares a minimum of the current minecraft version up to but not including the next major version versionRange="[1.19.2,1.20)" ordering="NONE" - side="BOTH" + side="CLIENT"