Well I seem to have everything else working pretty well. What this does is it makes a chest that is capable of exploding when opened if it is armed. However, I am having a problem with the arming and disarming part. The first time the chest is placed down, it is already armed when it is not suppose to be. Also, I do not get the message whether or not it is armed or disarmed. Instead, it only records it through the variable. I feel like I have one part of it that is backwards that is throwing everything off or something like that.
Here is the code:
package Alexiades.John.ChestBomb;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener{
private boolean armed;
private ArrayList<Location> chestList = new ArrayList<Location>();
private ArrayList<Integer> countList = new ArrayList<Integer>();
private int counter = 0;
private int index;
public final Logger log = Logger.getLogger("Minecraft");
public void onEnable(){
log.info(ChatColor.GREEN + "ChestBomb plugin is enabled!");
getServer().getPluginManager().registerEvents(this, this);
}
public void onDisable(){
log.info(ChatColor.RED + "ChestBomb plugin is disabled!");
}
@EventHandler
public void onPlayerChestOpen(PlayerInteractEvent event)
{
Action action = event.getAction();
Block clicked = event.getClickedBlock();
Location blockLocation = clicked.getLocation();
Player player = event.getPlayer();
World world = player.getWorld();
if (action == Action.LEFT_CLICK_BLOCK)
{
if((clicked.getType() == Material.CHEST) || (clicked.getType() == Material.ENDER_CHEST))
{
chestList.add(blockLocation);
// CHESTLIST CHECKER
for(int i = 0; i < chestList.size(); i++)
if(chestList.get(i) == blockLocation)
index = i;
// else
// index = -1;
// *****************
// COUNTER CHECKER
if(counter % 2 != 0)
armed = true;
else
armed = false;
counter++;
countList.set(index, counter);
// ****************
if(armed == true){
player.sendMessage("This chest is now" + ChatColor.RED + " armed!");
}
if(armed == false){
player.sendMessage("This chest is now" + ChatColor.GREEN + " disarmed!");
}
}
else{ }
}
else{ }
if(action == Action.RIGHT_CLICK_BLOCK){
if((clicked.getType() == Material.CHEST) || (clicked.getType() == Material.ENDER_CHEST)){
if(armed == true){
world.createExplosion(blockLocation, 4.0F);
chestList.remove(index);
countList.remove(index);
}
}
}
}
}
Though this isn't quite the place for bukkit modding, it appears your armed variable is global therefore causing all chests in the world to become armed and unarmed. (Im no bukkit modder though.)
Though this isn't quite the place for bukkit modding, it appears your armed variable is global therefore causing all chests in the world to become armed and unarmed. (Im no bukkit modder though.)
Yea I actually made some other adjustments right now so that the each unique chest is an index in an ArrayList as well as a seperate ArrayList to keep track of a counter for the matching index so that when the number is odd it will arm the bomb and when it is even it will disarm it. I just don't know what I am doing wrong.
Here is the code:
Any help is greatly appreciated!
Yea I actually made some other adjustments right now so that the each unique chest is an index in an ArrayList as well as a seperate ArrayList to keep track of a counter for the matching index so that when the number is odd it will arm the bomb and when it is even it will disarm it. I just don't know what I am doing wrong.