Skip to content

Commit

Permalink
Fix explosion regen so containers respawn with the correct inventories.
Browse files Browse the repository at this point in the history
  • Loading branch information
ElgarL committed Aug 13, 2012
1 parent 264d248 commit 228973f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 79 deletions.
1 change: 0 additions & 1 deletion build.xml
Expand Up @@ -16,7 +16,6 @@
<javac srcdir="src" destdir="${build}" includeantruntime="false" target="1.6" source="1.6">
<classpath>
<pathelement location="${env.LIB}/bukkit.jar"/>
<pathelement location="${env.LIB}/craftbukkit.jar"/>
<pathelement location="${env.LIB}/bpermissions.jar"/>
<pathelement location="${env.LIB}/Essentials.jar"/>
<pathelement location="${env.LIB}/EssentialsGroupManager.jar"/>
Expand Down
4 changes: 3 additions & 1 deletion src/ChangeLog.txt
Expand Up @@ -1680,4 +1680,6 @@ v0.81.0.6:
- Add missing towny.command.resident.* permission.
- Fix typo on 'towny.command.town.add' permission.
v0.81.0.7:
- Fix player permissions not being updated when performing a '/ta reload'.
- Fix player permissions not being updated when performing a '/ta reload'.
v0.81.0.8:
- Fix explosion regen so containers respawn with the correct inventories.
168 changes: 92 additions & 76 deletions src/com/palmergames/bukkit/towny/tasks/ProtectionRegenTask.java
@@ -1,14 +1,17 @@
package com.palmergames.bukkit.towny.tasks;

import java.util.ArrayList;
import java.util.List;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.CreatureSpawner;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Attachable;
import org.bukkit.material.Door;
import org.bukkit.material.PistonExtensionMaterial;
Expand All @@ -24,6 +27,7 @@ public class ProtectionRegenTask extends TownyTimerTask {
private BlockState altState;
private BlockLocation blockLocation;
private int TaskId;
private List<ItemStack> contents = new ArrayList<ItemStack>();

private static final Material placeholder = Material.DIRT;

Expand All @@ -33,7 +37,19 @@ public ProtectionRegenTask(Towny plugin, Block block, boolean update) {
this.state = block.getState();
this.altState = null;
this.setBlockLocation(new BlockLocation(block.getLocation()));

if (state instanceof InventoryHolder) {

// Contents we are respawning.
Inventory inven = ((InventoryHolder) state).getInventory();

for (ItemStack item : inven.getContents()) {
contents.add((item != null) ? item.clone() : null);
}

inven.clear();
}

if (update)
if (state.getData() instanceof Door) {
Door door = (Door) state.getData();
Expand Down Expand Up @@ -70,87 +86,87 @@ public void run() {

public void replaceProtections() {

Block block = state.getBlock();

if (state.getData() instanceof Door) {

Door door = (Door) state.getData();
Block topHalf;
Block bottomHalf;
if (door.isTopHalf()) {
topHalf = block;
bottomHalf = block.getRelative(BlockFace.DOWN);
try {

Block block = state.getBlock();

if (state.getData() instanceof Door) {

Door door = (Door) state.getData();
Block topHalf;
Block bottomHalf;
if (door.isTopHalf()) {
topHalf = block;
bottomHalf = block.getRelative(BlockFace.DOWN);
} else {
bottomHalf = block;
topHalf = block.getRelative(BlockFace.UP);
}
door.setTopHalf(true);
topHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
door.setTopHalf(false);
bottomHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);

} else if (state instanceof Sign) {

block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
Sign sign = (Sign) block.getState();
int i = 0;
for (String line : ((Sign) state).getLines())
sign.setLine(i++, line);

sign.update(true);

} else if (state instanceof CreatureSpawner) {

block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
((CreatureSpawner) block.getState()).setSpawnedType(((CreatureSpawner) state).getSpawnedType());

} else if (state instanceof InventoryHolder) {

block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);

// Container to receive the inventory
Inventory container = ((InventoryHolder) block.getState()).getInventory();
container.setContents(contents.toArray(new ItemStack[0]));

} else if (state.getData() instanceof PistonExtensionMaterial) {

PistonExtensionMaterial extension = (PistonExtensionMaterial) state.getData();
Block piston = block.getRelative(extension.getAttachedFace());
block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
if (altState != null) {
piston.setTypeIdAndData(altState.getTypeId(), altState.getData().getData(), false);
}
} else if (state.getData() instanceof Attachable) {

Block attachedBlock = block.getRelative(((Attachable) state.getData()).getAttachedFace());
if (attachedBlock.getTypeId() == 0) {
attachedBlock.setTypeId(placeholder.getId(), false);
TownyRegenAPI.addPlaceholder(attachedBlock);
}
block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);

} else {
bottomHalf = block;
topHalf = block.getRelative(BlockFace.UP);
}
door.setTopHalf(true);
topHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
door.setTopHalf(false);
bottomHalf.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);

} else if (state instanceof Sign) {

block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
Sign sign = (Sign) block.getState();
int i = 0;
for (String line : ((Sign) state).getLines())
sign.setLine(i++, line);

sign.update(true);

} else if (state instanceof CreatureSpawner) {

block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
((CreatureSpawner) block.getState()).setSpawnedType(((CreatureSpawner) state).getSpawnedType());

} else if ((state instanceof InventoryHolder) && !(state instanceof Player)) {

block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);

// Container to receive the inventory
InventoryHolder container = (InventoryHolder) block.getState();

// Contents we are respawning.
Inventory inven = ((InventoryHolder) state).getInventory();

if (inven.getContents().length > 0)
container.getInventory().setContents(inven.getContents());


} else if (state.getData() instanceof PistonExtensionMaterial) {

PistonExtensionMaterial extension = (PistonExtensionMaterial) state.getData();
Block piston = block.getRelative(extension.getAttachedFace());
block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);
if (altState != null) {
piston.setTypeIdAndData(altState.getTypeId(), altState.getData().getData(), false);
}
} else if (state.getData() instanceof Attachable) {

Block attachedBlock = block.getRelative(((Attachable) state.getData()).getAttachedFace());
if (attachedBlock.getTypeId() == 0) {
attachedBlock.setTypeId(placeholder.getId(), false);
TownyRegenAPI.addPlaceholder(attachedBlock);
}
block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), false);

} else {

if (NeedsPlaceholder.contains(state.getType())) {
Block blockBelow = block.getRelative(BlockFace.DOWN);
if (blockBelow.getTypeId() == 0) {
if (state.getType().equals(Material.CROPS)) {
blockBelow.setTypeId(Material.SOIL.getId(), true);
} else {
blockBelow.setTypeId(placeholder.getId(), true);
if (NeedsPlaceholder.contains(state.getType())) {
Block blockBelow = block.getRelative(BlockFace.DOWN);
if (blockBelow.getTypeId() == 0) {
if (state.getType().equals(Material.CROPS)) {
blockBelow.setTypeId(Material.SOIL.getId(), true);
} else {
blockBelow.setTypeId(placeholder.getId(), true);
}
TownyRegenAPI.addPlaceholder(blockBelow);
}
TownyRegenAPI.addPlaceholder(blockBelow);
}
block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), !NeedsPlaceholder.contains(state.getType()));
}
block.setTypeIdAndData(state.getTypeId(), state.getData().getData(), !NeedsPlaceholder.contains(state.getType()));
TownyRegenAPI.removePlaceholder(block);

} catch (Exception ex) {
ex.printStackTrace();
}
TownyRegenAPI.removePlaceholder(block);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/plugin.yml
@@ -1,6 +1,6 @@
name: Towny
main: com.palmergames.bukkit.towny.Towny
version: 0.81.0.7
version: 0.81.0.8
author: Shade, Modified by FuzzeWuzze. Forked by ElgarL
website: http://code.google.com/a/eclipselabs.org/p/towny/
description: >
Expand Down Expand Up @@ -169,6 +169,13 @@ permissions:
towny.command.town.claim.*: true
towny.command.town.unclaim: true
towny.command.town.online: true

towny.command.town.claim.*:
description: User can access all town related commands.
default: false
children:
towny.command.town.claim.town: true
towny.command.town.claim.outpost: true

towny.command.town.set.*:
description: User can access all town set commands.
Expand Down

0 comments on commit 228973f

Please sign in to comment.