diff --git a/gradle.properties b/gradle.properties index e1530e8..9e9a22c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -48,7 +48,7 @@ mod_name=Stack One Mod # The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default. mod_license=Apache License 2.0 # The mod version. See https://semver.org/ -mod_version=1.20.1-1.0.5 +mod_version=1.20.1-1.0.6 # The group ID for the mod. It is only important when publishing as an artifact to a Maven repository. # This should match the base package used for the mod sources. # See https://maven.apache.org/guides/mini/guide-naming-conventions.html diff --git a/src/main/java/com/laosun/stackone/StackOneMod.java b/src/main/java/com/laosun/stackone/StackOneMod.java index f2a7a38..4e9575c 100644 --- a/src/main/java/com/laosun/stackone/StackOneMod.java +++ b/src/main/java/com/laosun/stackone/StackOneMod.java @@ -1,12 +1,19 @@ package com.laosun.stackone; import com.mojang.logging.LogUtils; -import net.minecraft.world.item.Item; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.entity.player.PlayerEvent; +import net.minecraftforge.event.entity.player.PlayerContainerEvent; +import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.IEventBus; +import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.registries.ForgeRegistries; +import net.minecraft.world.item.Item; import org.slf4j.Logger; import java.lang.reflect.Field; @@ -19,8 +26,8 @@ public class StackOneMod { public StackOneMod() { IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus(); - modEventBus.addListener(this::commonSetup); + MinecraftForge.EVENT_BUS.register(this); } @SuppressWarnings("deprecation") @@ -50,4 +57,53 @@ private void commonSetup(final FMLCommonSetupEvent event) { } } } -} + + @SubscribeEvent(priority = EventPriority.HIGH) + public void onItemPickup(PlayerEvent.ItemPickupEvent event) { + Player player = event.getEntity(); + ItemStack stack = event.getStack(); + + if (stack.getCount() > 1) { + splitStacksInInventory(player); + } + } + + @SubscribeEvent(priority = EventPriority.HIGH) + public void onItemCrafted(PlayerEvent.ItemCraftedEvent event) { + Player player = event.getEntity(); + ItemStack stack = event.getCrafting(); + + if (stack.getCount() > 1) { + splitStacksInInventory(player); + } + } + + @SubscribeEvent(priority = EventPriority.HIGH) + public void onItemSmelted(PlayerEvent.ItemSmeltedEvent event) { + Player player = event.getEntity(); + ItemStack stack = event.getSmelting(); + + if (stack.getCount() > 1) { + splitStacksInInventory(player); + } + } + + private void splitStacksInInventory(Player player) { + for (int i = 0; i < player.getInventory().getContainerSize(); i++) { + ItemStack stack = player.getInventory().getItem(i); + + if (stack.getCount() > 1) { + int stackSize = stack.getCount(); + stack.setCount(1); + + for (int j = 1; j < stackSize; j++) { + ItemStack singleItemStack = stack.copy(); + + if (!player.getInventory().add(singleItemStack)) { + player.drop(singleItemStack, false); + } + } + } + } + } +} \ No newline at end of file