Add support for class item chests.

This is an extremely ugly, hacky way to do it, but the feature
should definitely make it into 1.0 - it requires no commands or
any other complex setting up besides flipping one option in the
config-file.

Upon clicking a class sign in the lobby, MobArena will (if the
config-setting is true) search for chests below the sign, and
below (and including) the block behind the sign, i.e. the block
that a wall sign would be attached to. If a chest is found, the
contents will become the class inventory.

The search can be optimized to be a one-off thing unless the
block changes from being a chest. A todo for 1.0, perhaps.
This commit is contained in:
Andreas Troelsen
2013-01-25 23:27:32 +01:00
parent 3f3518ab5b
commit 2de480709d
5 changed files with 82 additions and 4 deletions
+1
View File
@@ -33,3 +33,4 @@ spout-class-select: false
player-time-in-arena: world player-time-in-arena: world
auto-ignite-tnt: false auto-ignite-tnt: false
auto-start-timer: 0 auto-start-timer: 0
use-class-chests: false
+5 -1
View File
@@ -244,6 +244,10 @@ public class ArenaClass
return pets; return pets;
} }
public boolean hasUnbreakableWeapons() {
return unbreakableWeapons;
}
/** /**
* Used by isWeapon() to determine if an ItemStack is a weapon type. * Used by isWeapon() to determine if an ItemStack is a weapon type.
*/ */
@@ -256,7 +260,7 @@ public class ArenaClass
* @param stack an ItemStack * @param stack an ItemStack
* @return true, if the item is a weapon * @return true, if the item is a weapon
*/ */
private boolean isWeapon(ItemStack stack) { public boolean isWeapon(ItemStack stack) {
int id = stack.getTypeId(); int id = stack.getTypeId();
for (int type : weaponTypes) { for (int type : weaponTypes) {
@@ -27,6 +27,7 @@ import org.bukkit.inventory.PlayerInventory;
import org.bukkit.permissions.PermissionAttachment; import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import com.garbagemule.MobArena.ArenaClass.ArmorType;
import com.garbagemule.MobArena.autostart.AutoStartTimer; import com.garbagemule.MobArena.autostart.AutoStartTimer;
import com.garbagemule.MobArena.events.*; import com.garbagemule.MobArena.events.*;
import com.garbagemule.MobArena.framework.Arena; import com.garbagemule.MobArena.framework.Arena;
@@ -977,6 +978,46 @@ public class ArenaImpl implements Arena
arenaClass.grantItems(p); arenaClass.grantItems(p);
} }
@Override
public void assignClassGiveInv(Player p, String className, ItemStack[] contents) {
ArenaPlayer arenaPlayer = arenaPlayerMap.get(p);
ArenaClass arenaClass = classes.get(className);
if (arenaPlayer == null || arenaClass == null) {
return;
}
inventoryManager.clearInventory(p);
arenaPlayer.setArenaClass(arenaClass);
PlayerInventory inv = p.getInventory();
// Check the last four slots to see if they are armor items
for (int i = contents.length-1; i > contents.length-5; i--) {
if (contents[i] == null) continue;
ArmorType type = ArmorType.getType(contents[i]);
switch (type) {
case HELMET: inv.setHelmet(contents[i]); break;
case CHESTPLATE: inv.setChestplate(contents[i]); break;
case LEGGINGS: inv.setLeggings(contents[i]); break;
case BOOTS: inv.setBoots(contents[i]); break;
default:
continue;
}
contents[i] = null;
}
// Check the remaining slots for weapons
if (arenaClass.hasUnbreakableWeapons()) {
for (ItemStack stack : contents) {
if (stack != null && arenaClass.isWeapon(stack)) {
stack.setDurability(Short.MIN_VALUE);
}
}
}
p.getInventory().setContents(contents);
}
@Override @Override
public void addRandomPlayer(Player p) { public void addRandomPlayer(Player p) {
randoms.add(p); randoms.add(p);
@@ -98,7 +98,8 @@ public class ArenaListener
pvpOn, // pvp-enabled in config pvpOn, // pvp-enabled in config
pvpEnabled = false, // activated on first wave pvpEnabled = false, // activated on first wave
foodRegen, foodRegen,
lockFoodLevel; lockFoodLevel,
useClassChests;
@SuppressWarnings("unused") @SuppressWarnings("unused")
private boolean allowTeleport, private boolean allowTeleport,
canShare, canShare,
@@ -131,6 +132,7 @@ public class ArenaListener
this.allowTeleport = s.getBoolean("allow-teleporting", false); this.allowTeleport = s.getBoolean("allow-teleporting", false);
this.canShare = s.getBoolean("share-items-in-arena", true); this.canShare = s.getBoolean("share-items-in-arena", true);
this.autoIgniteTNT = s.getBoolean("auto-ignite-tnt", false); this.autoIgniteTNT = s.getBoolean("auto-ignite-tnt", false);
this.useClassChests = s.getBoolean("use-class-chests", false);
this.classLimits = arena.getClassLimitManager(); this.classLimits = arena.getClassLimitManager();
@@ -814,7 +816,7 @@ public class ArenaListener
classLimits.playerPickedClass(newAC); classLimits.playerPickedClass(newAC);
// Delay the inventory stuff to ensure that right-clicking works. // Delay the inventory stuff to ensure that right-clicking works.
delayAssignClass(p, className); delayAssignClass(p, className, sign);
} }
/*private boolean cansPlayerJoinClass(ArenaClass ac, Player p) { /*private boolean cansPlayerJoinClass(ArenaClass ac, Player p) {
@@ -829,10 +831,38 @@ public class ArenaListener
return true; return true;
}*/ }*/
private void delayAssignClass(final Player p, final String className) { private void delayAssignClass(final Player p, final String className, final Sign sign) {
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,new Runnable() { plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin,new Runnable() {
public void run() { public void run() {
if (!className.equalsIgnoreCase("random")) { if (!className.equalsIgnoreCase("random")) {
if (useClassChests) {
BlockFace backwards = ((org.bukkit.material.Sign) sign.getData()).getFacing().getOppositeFace();
Block blockBelow = sign.getBlock();
Block blockBehind = blockBelow.getRelative(backwards);
Block blockChest = null;
for (int i = 0; i < 6; i++) {
if (blockBelow.getType() == Material.CHEST) {
blockChest = blockBelow;
break;
}
if (blockBehind.getType() == Material.CHEST) {
blockChest = blockBehind;
break;
}
blockBelow = blockBelow.getRelative(BlockFace.DOWN);
blockBehind = blockBehind.getRelative(BlockFace.DOWN);
}
if (blockChest != null) {
InventoryHolder holder = (InventoryHolder) blockChest.getState();
ItemStack[] contents = holder.getInventory().getContents();
arena.assignClassGiveInv(p, className, contents);
p.getInventory().setContents(contents);
Messenger.tellPlayer(p, Msg.LOBBY_CLASS_PICKED, TextUtils.camelCase(className), arena.getClassLogo(className));
return;
}
}
arena.assignClass(p, className); arena.assignClass(p, className);
Messenger.tellPlayer(p, Msg.LOBBY_CLASS_PICKED, TextUtils.camelCase(className), arena.getClassLogo(className)); Messenger.tellPlayer(p, Msg.LOBBY_CLASS_PICKED, TextUtils.camelCase(className), arena.getClassLogo(className));
} }
@@ -179,6 +179,8 @@ public interface Arena
public void assignClass(Player p, String className); public void assignClass(Player p, String className);
public void assignClassGiveInv(Player p, String className, ItemStack[] contents);
public void addRandomPlayer(Player p); public void addRandomPlayer(Player p);
public void assignRandomClass(Player p); public void assignRandomClass(Player p);