Skip to content

Commit

Permalink
fix #19
Browse files Browse the repository at this point in the history
  • Loading branch information
12345-mcpython committed Sep 2, 2024
1 parent 86bb6eb commit 8597137
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 59 additions & 3 deletions src/main/java/com/laosun/stackone/StackOneMod.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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")
Expand Down Expand Up @@ -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);
}
}
}
}
}
}

0 comments on commit 8597137

Please sign in to comment.