Skip to content

Commit

Permalink
feat: Improve trash slot events to allow for compat to deny deletion …
Browse files Browse the repository at this point in the history
…of items #142
  • Loading branch information
BlayTheNinth committed Oct 27, 2024
1 parent 4bbb836 commit 36308d1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

public class ItemRemovedFromTrashSlotEvent extends BalmEvent {

public abstract class ItemTrashedEvent extends BalmEvent {
private final Player player;
private final ItemStack itemStack;

public ItemRemovedFromTrashSlotEvent(Player player, ItemStack itemStack) {
public ItemTrashedEvent(Player player, ItemStack itemStack) {
this.player = player;
this.itemStack = itemStack;
}
Expand All @@ -21,4 +20,16 @@ public Player getPlayer() {
public ItemStack getItemStack() {
return itemStack;
}

public static class Pre extends ItemTrashedEvent {
public Pre(Player player, ItemStack itemStack) {
super(player, itemStack);
}
}

public static class Post extends ItemTrashedEvent {
public Post(Player player, ItemStack itemStack) {
super(player, itemStack);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

public class ItemAddedToTrashSlotEvent extends BalmEvent {
public class ItemUntrashedEvent extends BalmEvent {

private final Player player;
private final ItemStack itemStack;

public ItemAddedToTrashSlotEvent(Player player, ItemStack itemStack) {
public ItemUntrashedEvent(Player player, ItemStack itemStack) {
this.player = player;
this.itemStack = itemStack;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,22 @@

import net.blay09.mods.balm.api.event.BalmEvent;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;

public class TrashSlotEmptiedEvent extends BalmEvent {
private final Player player;
private final ItemStack itemStack;

public TrashSlotEmptiedEvent(Player player) {
public TrashSlotEmptiedEvent(Player player, ItemStack itemStack) {
this.player = player;
this.itemStack = itemStack;
}

public Player getPlayer() {
return player;
}

public ItemStack getItemStack() {
return itemStack;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.blay09.mods.trashslot.client.deletion;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.trashslot.api.ItemAddedToTrashSlotEvent;
import net.blay09.mods.trashslot.api.ItemRemovedFromTrashSlotEvent;
import net.blay09.mods.trashslot.api.ItemTrashedEvent;
import net.blay09.mods.trashslot.api.ItemUntrashedEvent;
import net.blay09.mods.trashslot.api.TrashSlotEmptiedEvent;
import net.blay09.mods.trashslot.network.MessageDeleteFromSlot;
import net.blay09.mods.trashslot.network.MessageTrashSlotClick;
Expand All @@ -20,7 +20,7 @@ public void undeleteLast(Player player, TrashSlotSlot trashSlot, boolean isRight
player.containerMenu.setCarried(mouseStack);
trashSlot.set(isRightClick ? trashStack : ItemStack.EMPTY);
Balm.getNetworking().sendToServer(new MessageTrashSlotClick(ItemStack.EMPTY, isRightClick));
Balm.getEvents().fireEvent(new ItemRemovedFromTrashSlotEvent(player, mouseStack));
Balm.getEvents().fireEvent(new ItemUntrashedEvent(player, mouseStack));
}

@Override
Expand All @@ -32,22 +32,35 @@ public boolean canUndeleteLast() {
public void deleteMouseItem(Player player, ItemStack mouseItem, TrashSlotSlot trashSlot, boolean isRightClick) {
ItemStack mouseStack = mouseItem.copy();
ItemStack trashStack = isRightClick ? mouseStack.split(1) : mouseStack;
final var preEvent = new ItemTrashedEvent.Pre(player, trashStack);
Balm.getEvents().fireEvent(preEvent);
if (preEvent.isCanceled()) {
return;
}
player.containerMenu.setCarried(isRightClick ? mouseStack : ItemStack.EMPTY);
trashSlot.set(trashStack);
Balm.getNetworking().sendToServer(new MessageTrashSlotClick(mouseItem, isRightClick));
Balm.getEvents().fireEvent(new ItemAddedToTrashSlotEvent(player, trashStack));
Balm.getEvents().fireEvent(new ItemTrashedEvent.Post(player, trashStack));
}

@Override
public void deleteContainerItem(AbstractContainerMenu container, int slotNumber, boolean isDeleteAll, TrashSlotSlot slotTrash) {
final var player = Minecraft.getInstance().player;
final var itemStack = container.getSlot(slotNumber).getItem();
final var preEvent = new ItemTrashedEvent.Pre(player, itemStack);
Balm.getEvents().fireEvent(preEvent);
if (preEvent.isCanceled()) {
return;
}
Balm.getNetworking().sendToServer(new MessageDeleteFromSlot(slotNumber, isDeleteAll));
Balm.getEvents().fireEvent(new ItemAddedToTrashSlotEvent(Minecraft.getInstance().player, container.getSlot(slotNumber).getItem()));
Balm.getEvents().fireEvent(new ItemTrashedEvent.Post(player, itemStack));
}

@Override
public void emptyTrashSlot(TrashSlotSlot trashSlot) {
final var itemStack = trashSlot.getItem();
trashSlot.set(ItemStack.EMPTY);
Balm.getNetworking().sendToServer(new MessageDeleteFromSlot(-1, false));
Balm.getEvents().fireEvent(new TrashSlotEmptiedEvent(Minecraft.getInstance().player));
Balm.getEvents().fireEvent(new TrashSlotEmptiedEvent(Minecraft.getInstance().player, itemStack));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.trashslot.TrashHelper;
import net.blay09.mods.trashslot.TrashSlot;
import net.blay09.mods.trashslot.api.ItemAddedToTrashSlotEvent;
import net.blay09.mods.trashslot.api.ItemTrashedEvent;
import net.blay09.mods.trashslot.api.TrashSlotEmptiedEvent;
import net.blay09.mods.trashslot.config.TrashSlotConfig;
import net.minecraft.network.FriendlyByteBuf;
Expand Down Expand Up @@ -46,9 +46,10 @@ public static void handle(ServerPlayer player, MessageDeleteFromSlot message) {
}

if (message.slotNumber == -1) {
final var itemStack = TrashHelper.getTrashItem(player);
TrashHelper.setTrashItem(player, ItemStack.EMPTY);
Balm.getNetworking().reply(new MessageTrashSlotContent(ItemStack.EMPTY));
Balm.getEvents().fireEvent(new TrashSlotEmptiedEvent(player));
Balm.getEvents().fireEvent(new TrashSlotEmptiedEvent(player, itemStack));
return;
}

Expand Down Expand Up @@ -92,10 +93,12 @@ private static boolean attemptDeleteFromSlot(Player player, AbstractContainerMen

container.clicked(slotNumber, 0, ClickType.PICKUP, player);
ItemStack mouseStack = container.getCarried();
if (ItemStack.matches(itemStack, mouseStack)) {
final var preEvent = new ItemTrashedEvent.Pre(player, mouseStack);
Balm.getEvents().fireEvent(preEvent);
if (!preEvent.isCanceled() && ItemStack.matches(itemStack, mouseStack)) {
container.setCarried(ItemStack.EMPTY);
TrashHelper.setTrashItem(player, mouseStack);
Balm.getEvents().fireEvent(new ItemAddedToTrashSlotEvent(player, mouseStack));
Balm.getEvents().fireEvent(new ItemTrashedEvent.Post(player, mouseStack));
return !itemStack.isEmpty();
} else {
// Abort mission - something went weirdly wrong - sync the current mouse item to prevent desyncs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.trashslot.TrashHelper;
import net.blay09.mods.trashslot.TrashSlot;
import net.blay09.mods.trashslot.api.ItemAddedToTrashSlotEvent;
import net.blay09.mods.trashslot.api.ItemRemovedFromTrashSlotEvent;
import net.blay09.mods.trashslot.api.ItemTrashedEvent;
import net.blay09.mods.trashslot.api.ItemUntrashedEvent;
import net.blay09.mods.trashslot.config.TrashSlotConfig;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
Expand Down Expand Up @@ -48,7 +48,7 @@ public static void handle(ServerPlayer player, MessageTrashSlotClick message) {
return;
}

ItemStack actualMouseItem = player.containerMenu.getCarried();
ItemStack actualMouseItem = player.containerMenu.getCarried().copy();
var registryName = Balm.getRegistries().getKey(actualMouseItem.getItem());
if (registryName != null && TrashSlotConfig.getActive().deletionDenyList.contains(registryName.toString())) {
return;
Expand All @@ -60,12 +60,17 @@ public static void handle(ServerPlayer player, MessageTrashSlotClick message) {
ItemStack mouseStack = message.isRightClick ? trashStack.split(1) : trashStack;
player.containerMenu.setCarried(mouseStack);
TrashHelper.setTrashItem(player, message.isRightClick ? trashStack : ItemStack.EMPTY);
Balm.getEvents().fireEvent(new ItemRemovedFromTrashSlotEvent(player, mouseStack));
Balm.getEvents().fireEvent(new ItemUntrashedEvent(player, mouseStack));
} else {
ItemStack trashStack = message.isRightClick ? actualMouseItem.split(1) : actualMouseItem;
final var preEvent = new ItemTrashedEvent.Pre(player, trashStack);
Balm.getEvents().fireEvent(preEvent);
if (preEvent.isCanceled()) {
return;
}
TrashHelper.setTrashItem(player, trashStack);
player.containerMenu.setCarried(message.isRightClick ? actualMouseItem : ItemStack.EMPTY);
Balm.getEvents().fireEvent(new ItemAddedToTrashSlotEvent(player, trashStack));
Balm.getEvents().fireEvent(new ItemTrashedEvent.Post(player, trashStack));
}
}
}
Expand Down

0 comments on commit 36308d1

Please sign in to comment.