Push v1.0

This commit is contained in:
Zontreck 2022-09-26 05:01:59 -07:00
commit 86714db360
15 changed files with 1435 additions and 0 deletions

View 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;
}
}

View 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;
}
}

View 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;
}
}
}

View file

@ -0,0 +1,70 @@
# This is an example mods.toml file. It contains the data relating to the loading mods.
# There are several mandatory fields (#mandatory), and many more that are optional (#optional).
# The overall format is standard TOML format, v0.5.0.
# Note that there are a couple of TOML lists in this file.
# Find more information on toml format here: https://github.com/toml-lang/toml
# The name of the mod loader type to load - for regular FML @Mod mods it should be javafml
modLoader="javafml" #mandatory
# A version range to match for said mod loader - for regular FML @Mod it will be the forge version
loaderVersion="[43,)" #mandatory This is typically bumped every Minecraft version by Forge. See our download page for lists of versions.
# The license for you mod. This is mandatory metadata and allows for easier comprehension of your redistributive properties.
# Review your options at https://choosealicense.com/. All rights reserved is the default copyright stance, and is thus the default here.
license="GPL-v2"
# A URL to refer people to when problems occur with this mod
#issueTrackerURL="https://change.me.to.your.issue.tracker.example.invalid/" #optional
# A list of mods - how many allowed here is determined by the individual mod loader
[[mods]] #mandatory
# The modid of the mod
modId="watchmydurability" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
# see the associated build.gradle script for how to populate this completely automatically during a build
version="${file.jarVersion}" #mandatory
# A display name for the mod
displayName="Watch My Durability" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/
#updateJSONURL="https://change.me.example.invalid/updates.json" #optional
# A URL for the "homepage" for this mod, displayed in the mod UI
#displayURL="https://change.me.to.your.mods.homepage.example.invalid/" #optional
# A file name (in the root of the mod JAR) containing a logo for display
# logoFile="examplemod.png" #optional
# A text field displayed in the mod UI
credits="zontreck @ ZNI Creations" #optional
# A text field displayed in the mod UI
authors="zontreck" #optional
# Display Test controls the display for your mod in the server connection screen
# MATCH_VERSION means that your mod will cause a red X if the versions on client and server differ. This is the default behaviour and should be what you choose if you have server and client elements to your mod.
# IGNORE_SERVER_VERSION means that your mod will not cause a red X if it's present on the server but not on the client. This is what you should use if you're a server only mod.
# IGNORE_ALL_VERSION means that your mod will not cause a red X if it's present on the client or the server. This is a special case and should only be used if your mod has no server component.
# NONE means that no display test is set on your mod. You need to do this yourself, see IExtensionPoint.DisplayTest for more information. You can define any scheme you wish with this value.
# IMPORTANT NOTE: this is NOT an instruction as to which environments (CLIENT or DEDICATED SERVER) your mod loads on. Your mod should load (and maybe do nothing!) whereever it finds itself.
#displayTest="MATCH_VERSION" # MATCH_VERSION is the default if nothing is specified (#optional)
# The description text for the mod (multi line!) (#mandatory)
description='''
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.
'''
# A dependency - use the . to indicate dependency for a specific modid. Dependencies are optional.
[[dependencies.watchmydurability]] #optional
# the modid of the dependency
modId="forge" #mandatory
# Does this dependency have to exist - if not, ordering below must be specified
mandatory=true #mandatory
# The version range of the dependency
versionRange="[43,)" #mandatory
# 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"
# Here's another dependency
[[dependencies.watchmydurability]]
modId="minecraft"
mandatory=true
# 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"

View file

@ -0,0 +1,8 @@
{
"pack": {
"description": "Watch My Durability Resources",
"pack_format": 9,
"forge:resource_pack_format": 9,
"forge:data_pack_format": 10
}
}