This commit is contained in:
2024-07-24 13:28:59 +02:00
parent 97c2285f77
commit 5ea34e9f16
20 changed files with 347 additions and 50 deletions

View File

@@ -0,0 +1,50 @@
package systems.brn.plasticgun.lib;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.hit.EntityHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import systems.brn.plasticgun.guns.Gun;
import static systems.brn.plasticgun.PlasticGun.guns;
public class EventHandler {
public static TypedActionResult<ItemStack> onItemUse(PlayerEntity playerEntity, World world, Hand hand) {
ItemStack stack = playerEntity.getStackInHand(hand);
if (!world.isClient) {
Item stackInHand = playerEntity.getStackInHand(hand).getItem();
for (Gun gun : guns) {
if (gun == stackInHand) {
gun.reload(world, playerEntity, hand);
break;
}
}
}
return TypedActionResult.pass(stack);
}
public static void onWorldTick(World world) {
// Iterate through all players to detect hand swings or item interactions
for (PlayerEntity player : world.getPlayers()) {
if (!world.isClient && player.handSwinging && player.handSwingTicks == 1) {
Hand hand = player.getActiveHand();
Item stackInHand = player.getStackInHand(hand).getItem();
for (Gun gun : guns) {
if (gun == stackInHand) {
gun.shoot(world, player, hand);
break;
}
}
}
}
}
}

View File

@@ -13,10 +13,10 @@ public abstract class SimpleItem extends SimplePolymerItem implements PolymerIte
private final PolymerModelData polymerModel;
protected final Identifier identifier;
public SimpleItem(Settings settings, Identifier identifier) {
super(settings, Items.BARRIER);
public SimpleItem(Settings settings, Identifier identifier, Item replacement) {
super(settings, replacement);
this.identifier = identifier;
this.polymerModel = PolymerResourcePackUtils.requestModel(Items.BARRIER, identifier.withPath("item/" + identifier.getPath()));
this.polymerModel = PolymerResourcePackUtils.requestModel(replacement, identifier.withPath("item/" + identifier.getPath()));
}
@Override

View File

@@ -1,5 +1,7 @@
package systems.brn.plasticgun.lib;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
@@ -28,4 +30,71 @@ public class Util {
}
return ItemStack.EMPTY;
}
public static int canInsertItemIntoInventory(Inventory inventory, ItemStack itemStack) {
// Get the player's inventory
int maxInsert = 0;
if (inventory instanceof PlayerInventory playerInventory) {
// Iterate through the slots in the player's inventory
for (int i = 0; i < playerInventory.main.size(); i++) {
ItemStack slotStack = playerInventory.main.get(i);
maxInsert = canInsertToStack(slotStack, itemStack, maxInsert);
}
} else {
for (int i = 0; i < inventory.size(); i++) {
ItemStack slotStack = inventory.getStack(i);
maxInsert = canInsertToStack(slotStack, itemStack, maxInsert);
}
}
return maxInsert; // Return the maximum insertion count
}
public static int canInsertToStack(ItemStack stack1, ItemStack stack2, int maxInsert) {
if (stack1.isEmpty() || ItemStack.areItemsEqual(stack1, stack2)) {
int remainingSpace = stack1.isEmpty() ? stack2.getMaxCount() : stack1.getMaxCount() - stack1.getCount();
maxInsert += remainingSpace;
// If the maximum insertion count is greater than or equal to the item count, return the item count
if (maxInsert >= stack2.getCount()) {
return stack2.getCount();
}
}
return maxInsert;
}
public static boolean canCombine(ItemStack stack1, ItemStack stack2) {
return !stack1.isEmpty() && stack1.getItem() == stack2.getItem() && ItemStack.areItemsAndComponentsEqual(stack1, stack2);
}
public static ItemStack insertStackIntoInventory(Inventory inventory, ItemStack stack) {
// First, try to merge with existing stacks
for (int i = 0; i < inventory.size(); i++) {
ItemStack slotStack = inventory.getStack(i);
if (canCombine(slotStack, stack)) {
int transferAmount = Math.min(stack.getCount(), slotStack.getMaxCount() - slotStack.getCount());
if (transferAmount > 0) {
slotStack.increment(transferAmount);
stack.decrement(transferAmount);
inventory.markDirty();
if (stack.isEmpty()) {
return ItemStack.EMPTY;
}
}
}
}
// Next, try to find an empty slot
for (int i = 0; i < inventory.size(); i++) {
ItemStack slotStack = inventory.getStack(i);
if (slotStack.isEmpty()) {
inventory.setStack(i, stack.copy());
stack.setCount(0);
inventory.markDirty();
return ItemStack.EMPTY;
}
}
return stack;
}
}