Push v1.0
This commit is contained in:
commit
86714db360
15 changed files with 1435 additions and 0 deletions
90
src/main/java/dev/zontreck/mcmods/CheckInventory.java
Normal file
90
src/main/java/dev/zontreck/mcmods/CheckInventory.java
Normal file
|
@ -0,0 +1,90 @@
|
|||
package dev.zontreck.mcmods;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimerTask;
|
||||
|
||||
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;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
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();
|
||||
|
||||
|
||||
PushItems("_armor", inv.armor);
|
||||
PushItems("_items", inv.items);
|
||||
PushItems("_offhand", inv.offhand);
|
||||
|
||||
}
|
||||
|
||||
public void PushItems(String type, List<ItemStack> stack)
|
||||
{
|
||||
// OK
|
||||
// Push the items into the registry, replacing the existing entry
|
||||
ItemRegistry.purge(type);
|
||||
Map<Integer, ItemRegistry.Item> items = new HashMap<Integer, ItemRegistry.Item>();
|
||||
Integer slotNum = 0;
|
||||
for (ItemStack itemStack : stack) {
|
||||
ItemRegistry.Item itx = WatchMyDurability.REGISTRY.GetNewItem(itemStack);
|
||||
|
||||
items.put(slotNum, itx);
|
||||
slotNum++;
|
||||
|
||||
}
|
||||
|
||||
ItemRegistry.register(type,items);
|
||||
}
|
||||
|
||||
public void Soundify()
|
||||
{
|
||||
//WatchMyDurability.LOGGER.info("PLAY ALERT SOUND");
|
||||
WatchMyDurability._player.playSound(SoundEvents.ITEM_BREAK, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
public boolean checkList(String type, NonNullList<ItemStack> stacks){
|
||||
Integer slotNum = 0;
|
||||
for (ItemStack is1 : stacks) {
|
||||
if(is1.isDamageableItem() && is1.isDamaged() && !ItemRegistry.contains(type, slotNum, WatchMyDurability.REGISTRY.GetNewItem(is1))){
|
||||
|
||||
int percent = 0;
|
||||
int max = is1.getMaxDamage();
|
||||
int val = is1.getDamageValue();
|
||||
int cur = max - val;
|
||||
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;
|
||||
}
|
||||
|
||||
slotNum ++;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
85
src/main/java/dev/zontreck/mcmods/ItemRegistry.java
Normal file
85
src/main/java/dev/zontreck/mcmods/ItemRegistry.java
Normal file
|
@ -0,0 +1,85 @@
|
|||
package dev.zontreck.mcmods;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public class ItemRegistry {
|
||||
public class Item {
|
||||
public String Name;
|
||||
public int PercentDamaged;
|
||||
public int Count;
|
||||
|
||||
|
||||
public boolean Compare(Item other)
|
||||
{
|
||||
if(other.Name.equals(Name) && Count == other.Count){
|
||||
if(PercentDamaged != other.PercentDamaged) return false;
|
||||
else return true;
|
||||
}else return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Map<String,Map<Integer, Item>> CachedItems;
|
||||
public ItemRegistry()
|
||||
{
|
||||
CachedItems = new HashMap<String,Map<Integer,Item>>();
|
||||
}
|
||||
|
||||
public static void Initialize()
|
||||
{
|
||||
WatchMyDurability.REGISTRY = new ItemRegistry();
|
||||
}
|
||||
|
||||
public static void purge(String type) {
|
||||
if(WatchMyDurability.REGISTRY.CachedItems.containsKey(type))
|
||||
WatchMyDurability.REGISTRY.CachedItems.remove(type);
|
||||
}
|
||||
|
||||
public Item GetNewItem(ItemStack itemStack) {
|
||||
Item x = new Item();
|
||||
x.Name = itemStack.getDisplayName().getString();
|
||||
if(itemStack.isDamageableItem() && itemStack.isDamaged()){
|
||||
int max = itemStack.getMaxDamage();
|
||||
int val = itemStack.getDamageValue();
|
||||
int cur = max-val;
|
||||
int percent = cur * 100 /max;
|
||||
x.PercentDamaged=percent;
|
||||
}
|
||||
|
||||
x.Count = itemStack.getCount();
|
||||
//WatchMyDurability.LOGGER.debug("ITEM: "+x.Name + "; "+x.PercentDamaged+"; "+x.Type+"; "+x.Count);
|
||||
return x;
|
||||
}
|
||||
|
||||
public static void register(String type, Map<Integer, Item> items) {
|
||||
|
||||
WatchMyDurability.REGISTRY.CachedItems.put(type, items);
|
||||
}
|
||||
|
||||
public static boolean contains(String type, Integer slot, Item getNewItem) {
|
||||
ItemRegistry reg = WatchMyDurability.REGISTRY;
|
||||
if(reg.CachedItems.containsKey(type)){
|
||||
WatchMyDurability.LOGGER.debug("Registry contains "+type);
|
||||
Map<Integer,Item> items = reg.CachedItems.get(type);
|
||||
if(items.containsKey(slot)){
|
||||
WatchMyDurability.LOGGER.debug("ItemRegistry contains slot: "+slot);
|
||||
Item x = items.get(slot);
|
||||
if(x.Compare(getNewItem)){
|
||||
WatchMyDurability.LOGGER.debug("Items are identical!");
|
||||
// Items are identical
|
||||
return true;
|
||||
}else {
|
||||
WatchMyDurability.LOGGER.debug("ITEMS ARE NOT IDENTICAL");
|
||||
return false;
|
||||
}
|
||||
}else return false;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
104
src/main/java/dev/zontreck/mcmods/WatchMyDurability.java
Normal file
104
src/main/java/dev/zontreck/mcmods/WatchMyDurability.java
Normal file
|
@ -0,0 +1,104 @@
|
|||
package dev.zontreck.mcmods;
|
||||
|
||||
import com.mojang.logging.LogUtils;
|
||||
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.common.Mod;
|
||||
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;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
|
||||
// The value here should match an entry in the META-INF/mods.toml file
|
||||
@Mod(WatchMyDurability.MODID)
|
||||
public class WatchMyDurability
|
||||
{
|
||||
// Define mod id in a common place for everything to reference
|
||||
public static final String MODID = "watchmydurability";
|
||||
// Directly reference a slf4j logger
|
||||
public static final Logger LOGGER = LogUtils.getLogger();
|
||||
|
||||
/// 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;
|
||||
|
||||
|
||||
|
||||
public WatchMyDurability()
|
||||
{
|
||||
IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
|
||||
|
||||
// Register the commonSetup method for modloading
|
||||
modEventBus.addListener(this::commonSetup);
|
||||
|
||||
// Register ourselves for server and other game events we are interested in
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
private void commonSetup(final FMLCommonSetupEvent event)
|
||||
{
|
||||
// Some common setup code
|
||||
//LOGGER.info("HELLO FROM COMMON SETUP");
|
||||
}
|
||||
|
||||
// You can use SubscribeEvent and let the Event Bus discover methods to call
|
||||
@SubscribeEvent
|
||||
public void onServerStarting(ServerStartingEvent event)
|
||||
{
|
||||
// Do something when the server starts
|
||||
//LOGGER.warn("If this is running on a server, it is doing absolutely nothing, please remove me.");
|
||||
}
|
||||
|
||||
// You can use EventBusSubscriber to automatically register all static methods in the class annotated with @SubscribeEvent
|
||||
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
public static class ClientModEvents
|
||||
{
|
||||
static Timer time = new Timer();
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onClientSetup(FMLClientSetupEvent event)
|
||||
{
|
||||
//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);
|
||||
|
||||
ItemRegistry.Initialize();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber(modid = MODID, bus = Mod.EventBusSubscriber.Bus.FORGE)
|
||||
public static class ClientEvents
|
||||
{
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onJoin(ClientPlayerNetworkEvent.LoggingIn event){
|
||||
// Joined
|
||||
//LOGGER.info("PLAYER LOGGED IN");
|
||||
WatchMyDurability._player = event.getPlayer();
|
||||
WatchMyDurability.isInGame=true;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void onLeave(ClientPlayerNetworkEvent.LoggingOut event){
|
||||
//LOGGER.info("PLAYER LOGGED OUT");
|
||||
WatchMyDurability.isInGame=false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in a new issue