Code

This has yet to be updated to v0.2

main.java:

package me.lol768.toggleBlocks;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import me.lol768.toggleBlocks.savedBlock;

public class main extends JavaPlugin implements Listener
{
	
	Logger log;
	//List<Player> editors = new ArrayList<Player>();
	HashMap<String, List<savedBlock>> bls = new HashMap<String,List<savedBlock>>();
	
	HashMap<Player, String> editors = new HashMap<Player, String>();
	
	  public void onEnable()
	  {
		  //Enable the plugin
		  log = Bukkit.getLogger();
		  try {
				bls = (HashMap<String, List<savedBlock>>) load("tg.bin");
			} catch (Exception e) {
				log.severe("Couldn't liad toggleBlock data :-(");
			}
		  log.info("toggleBlocks has started up...");
		  getServer().getPluginManager().registerEvents(this, this);
		  
	  }
	  
		public static void save(Object obj,String path) throws Exception
		{
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
			oos.writeObject(obj);
			oos.flush();
			oos.close();
		}
		public static Object load(String path) throws Exception
		{
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path));
			Object result = ois.readObject();
			ois.close();
			return result;
		}
		
	  @EventHandler
	  public void pje(PlayerJoinEvent e)
	  {
		 
	  }
	  
	  public void onDisable()
	  {
		  //Disable the plugin
		  log = Bukkit.getLogger();
		  try {
			save(bls,"tg.bin");
		} catch (Exception e) {
			log.severe("Couldn't save toggleBlock data :-(");
		}
		  log.info("toggleBlocks has shut down...");
		  
	  }
	  
	  @EventHandler
	  public void onBlock(BlockPlaceEvent e)
	  {
		  if (editors.containsKey(e.getPlayer()))
		  {
			  e.getPlayer().sendMessage("Added block to HashMap for region " + editors.get(e.getPlayer()));
			  List<savedBlock> f = bls.get(editors.get(e.getPlayer()));
			  f.add(new savedBlock(e.getBlock()));
			  bls.put(editors.get(e.getPlayer()), f);
			  
			  
		  }
	  }
	  
	  public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, final String[] args)
	  {
		  	if(cmd.getName().equalsIgnoreCase("tb") && (sender instanceof Player))
	    	{
		  		Player p = (Player)sender;
			  	if (editors.containsKey(p))
			  	{
			  		p.sendMessage("You've left editing mode for region "  + ChatColor.YELLOW + editors.get(p));
			  		editors.remove(p);
		    	}
			  	else
			  	{
			  		if (args.length == 1)
			  		{
			  			p.sendMessage("You've entered editing mode for region " + ChatColor.YELLOW + args[0]);
			  			if (!bls.containsKey(args[0]))
			  			{
			  				List<savedBlock> f = new ArrayList<savedBlock>();
			  				bls.put(args[0], f);
			  			}
			  				editors.put(p, args[0]);
			  			
			  		}
			  		else
			  		{
			  			p.sendMessage("Malformed command. Please supply region");
			  		}
			  	}
	    	}
	    	
	    	if(cmd.getName().equalsIgnoreCase("tbon"))
	    	{
	    		if(args.length == 1)
	    		{
	    			if (bls.containsKey(args[0]))
	    			{
			    		//Player p = (Player)sender;
			    		List<savedBlock> s = bls.get(args[0]);
			    		Iterator iterator = s.iterator();
			    		while(iterator.hasNext())
			    		{
			    			savedBlock ref = (savedBlock) iterator.next();
				    		int x = ref.getX();
				    		int y = ref.getY();
				    		int z = ref.getZ();
				    		Location l = new Location(Bukkit.getWorld(ref.getWorld()), x, y, z);
				    		Block b = l.getBlock();
				    		b.setTypeId(ref.getID());
				    		b.setData(ref.getData());
			    		}
	    			}
	    		
	    	}
	    	}
	    	
	    	if(cmd.getName().equalsIgnoreCase("tboff"))
	    	{
	    		//Player p = (Player)sender;
	    		if(args.length == 1)
	    		{
	    			//p.sendMessage("Good args number");

	    			if (bls.containsKey(args[0]))
	    			{
	    				//Player p = (Player)sender;
	    	    		List<savedBlock> s = bls.get(args[0]);
	    	    		Iterator iterator = s.iterator();
	    	    		//p.sendMessage("About to iterate");
	    	    		while(iterator.hasNext())
	    	    		{
	    	    			savedBlock ref = (savedBlock) iterator.next();
	    		    		int x = ref.getX();
	    		    		int y = ref.getY();
	    		    		int z = ref.getZ();
	    		    		Location l = new Location(Bukkit.getWorld(ref.getWorld()), x, y, z);
	    		    		l.getBlock().setType(Material.AIR);
	    		    		
	    		    		//p.sendMessage("Iterating over " + x + " " + y + " " + z);
	    	    		}
	    			}
	    			else
	    			{
	    				sender.sendMessage("Bad region?");
	    				


	    			}
	    		}
	    		
	    		
	    		
	    	}
	    	
	      	
	    	if(cmd.getName().equalsIgnoreCase("tbd"))
	    	{
	    		//Player p = (Player)sender;
	    		if(args.length == 1)
	    		{
	    			//p.sendMessage("Good args number");

	    			if (bls.containsKey(args[0]))
	    			{
	    				bls.remove(args[0]);
	    				sender.sendMessage("Deleted region");
	    			}
	    			else
	    			{
	    				sender.sendMessage("Bad region?");
	    				


	    			}
	    		}
	    		
	    		
	    		
	    	}
			return true;
	  }
	  
}

savedBlock.java:

package me.lol768.toggleBlocks;

import java.io.Serializable;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;

public class savedBlock implements Serializable
{
	private int x; //coords
	private int y;
	private int z;
	
	private int id; //material id
	private byte data;
	private String world;
	
	public savedBlock(Block xa) 
	{
		x = xa.getX();
		y = xa.getY();
		z = xa.getZ();
		id = xa.getTypeId();
		data = xa.getData();
		world = xa.getWorld().getName();
	}
	
	public Location getLocation()
	{
		Location l = new Location(Bukkit.getWorld(world), x, y, z);
		return l;
	}
	
	public int getX()
	{
		return x;
	}
	
	public String getWorld()
	{
		return world;
	}
	
	public int getY()
	{
		return y;
	}
	
	public int getZ()
	{
		return z;
	}
	
	public int getID()
	{
		return id;
	}
	
	public byte getData()
	{
		return data;
	}
	
}

Comments

Posts Quoted:
Reply
Clear All Quotes