Skip to content

Commit

Permalink
add option to keep items in the chest fill event
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonUden committed Nov 18, 2023
1 parent f053cf9 commit fca7130
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public void onPlayerInteract(PlayerInteractEvent e) {
return;
}

ChestFillEvent event = new ChestFillEvent(e.getClickedBlock(), lootTable, ChestType.ENDERCHEST);
ChestFillEvent event = new ChestFillEvent(e.getClickedBlock(), lootTable, ChestType.ENDERCHEST, clearOldItems);

Bukkit.getServer().getPluginManager().callEvent(event);

Expand All @@ -202,7 +202,7 @@ public void onPlayerInteract(PlayerInteractEvent e) {
lootTable = event.getLootTable();
}

if (clearOldItems) {
if (event.isClearOldItems()) {
inventory.clear();
}

Expand Down Expand Up @@ -262,7 +262,7 @@ private void fillChest(Block block, boolean clearOldItems) {

Inventory inventory = chest.getBlockInventory();

ChestFillEvent event = new ChestFillEvent(block, lootTable, ChestType.CHEST);
ChestFillEvent event = new ChestFillEvent(block, lootTable, ChestType.CHEST, clearOldItems);

Bukkit.getServer().getPluginManager().callEvent(event);

Expand All @@ -274,7 +274,7 @@ private void fillChest(Block block, boolean clearOldItems) {
lootTable = event.getLootTable();
}

if (clearOldItems) {
if (event.isClearOldItems()) {
inventory.clear();
}

Expand All @@ -301,7 +301,7 @@ private void fillChest(Block block, boolean clearOldItems) {
if (nextBlock.getType() == Material.CHEST || nextBlock.getType() == Material.TRAPPED_CHEST) {
if (!chests.contains(nextBlock.getLocation())) {
Log.trace("Executing recursive fill to chest at location " + nextBlock.getLocation().toString());
this.fillChest(nextBlock, clearOldItems);
this.fillChest(nextBlock, event.isClearOldItems());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ public static HandlerList getHandlerList() {

private boolean cancelled;
private boolean lootTableChanged;

private boolean clearOldItems;

public ChestFillEvent(Block block, LootTable lootTable, ChestType chestType) {
public ChestFillEvent(Block block, LootTable lootTable, ChestType chestType, boolean clearOldItems) {
this.block = block;
this.lootTable = lootTable;
this.chestType = chestType;

this.cancelled = false;
this.lootTableChanged = false;

this.clearOldItems = clearOldItems;
}

public Block getBlock() {
Expand All @@ -60,6 +64,14 @@ public boolean isCancelled() {
public boolean hasLootTableChanged() {
return lootTableChanged;
}

public boolean isClearOldItems() {
return clearOldItems;
}

public void setClearOldItems(boolean clearOldItems) {
this.clearOldItems = clearOldItems;
}

public void setLootTable(LootTable lootTable) {
this.lootTable = lootTable;
Expand Down

0 comments on commit fca7130

Please sign in to comment.