World world = interactLocation.getWorld();
boolean currentState = (interactLocation.getBlock().getData() & 0x8) > 0;
String state = switchState.toString();
// Try for a linked player
CraftPlayer craftPlayer = (CraftPlayer) player;
if (craftPlayer == null && Bukkit.getOnlinePlayers().size() > 0) {
// If there's none, link any player
if (Bukkit.getOnlinePlayers().size() > 0) {
craftPlayer = (CraftPlayer) Bukkit.getOnlinePlayers().toArray()[0];
}
else if (Depends.citizens != null) {
// If there are no players, link any Human NPC
for (NPC npc: CitizensAPI.getNPCRegistry()) {
if (npc.isSpawned() && npc.getEntity() instanceof Player) {
craftPlayer = (CraftPlayer) npc.getEntity();
break;
}
}
// TODO: backup if no human NPC available? (Fake EntityPlayer instance?)
}
}
if ((state.equals("ON") && !currentState) ||
(state.equals("OFF") && currentState) ||
state.equals("TOGGLE")) {
try {
if (interactLocation.getBlock().getType() == Material.IRON_DOOR_BLOCK) {
Location block;
if (interactLocation.clone().add(0, -1, 0).getBlock().getType() == Material.IRON_DOOR_BLOCK)
block = interactLocation.clone().add(0, -1, 0);
else
block = interactLocation;
block.getBlock().setData((byte) (block.getBlock().getData() ^ 4));
}
else {
// TODO: Rewrite the below code to not use freakin' NMS!
Block.getById(interactLocation.getBlock().getType().getId())
.interact(((CraftWorld)world).getHandle(),
interactLocation.getBlockX(),
interactLocation.getBlockY(),
interactLocation.getBlockZ(),
craftPlayer != null ? craftPlayer.getHandle(): null, 0, 0f, 0f, 0f);
}
dB.echoDebug(scriptEntry, "Switched " + interactLocation.getBlock().getType().toString() + "! Current state now: " +
((interactLocation.getBlock().getData() & 0x8) > 0 ? "ON" : "OFF"));