Skip to content

Commit

Permalink
Fix up doublechests and explregen.
Browse files Browse the repository at this point in the history
  • Loading branch information
LlmDl committed Dec 17, 2019
1 parent 9634fef commit 64147a6
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/com/palmergames/bukkit/towny/tasks/ProtectionRegenTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Chest;
import org.bukkit.block.data.BlockData;
import org.bukkit.inventory.BlockInventoryHolder;
import org.bukkit.inventory.DoubleChestInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

public class ProtectionRegenTask extends TownyTimerTask {
Expand All @@ -25,15 +29,22 @@ public ProtectionRegenTask(Towny plugin, Block block) {
this.setBlockLocation(new BlockLocation(block.getLocation()));

if (state instanceof BlockInventoryHolder) {
Inventory inven = ((BlockInventoryHolder) state).getInventory();
items = inven.getContents();
inven.clear();
Inventory inven = ((BlockInventoryHolder) state).getInventory();

// Because DoubleChests are special we have to handle their inventory in two parts.
if (inven instanceof DoubleChestInventory) {
Inventory dblChestInven = ((Chest) state).getBlockInventory(); // .getBlockInventory() grabs only the 1/2 of the DoubleChest we want.
items = dblChestInven.getContents();
dblChestInven.clear();
} else { // Handle all other BlockInventoryHolders.
items = inven.getContents();
inven.clear();
}
}
}

@Override
public void run() {

replaceProtections();
TownyRegenAPI.removeProtectionRegenTask(this);
}
Expand All @@ -45,8 +56,14 @@ public void replaceProtections() {
BlockData blockData = state.getBlockData().clone();
block.setType(state.getType(), false);
block.setBlockData(blockData);
if (block.getState() instanceof BlockInventoryHolder)
((BlockInventoryHolder) block.getState()).getInventory().setContents(items);
if (block.getState() instanceof BlockInventoryHolder) {
InventoryHolder inven = ((BlockInventoryHolder) state).getInventory().getHolder();
if (inven instanceof DoubleChest) {
((Chest) block.getState()).getBlockInventory().setContents(items);
} else {
((BlockInventoryHolder) block.getState()).getInventory().setContents(items);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
Expand Down

0 comments on commit 64147a6

Please sign in to comment.