Commits

Mike Bayer committed d774fe1

- identifies atms
- opens up the inventory

  • Participants
  • Parent commits 499a712

Comments (0)

Files changed (3)

File src/main/java/com/zzzcomputing/bukkit/atmcraft/AtmCraft.java

 package com.zzzcomputing.bukkit.atmcraft;
 
 import org.bukkit.Bukkit;
+import org.bukkit.Material;
+import org.bukkit.block.Block;
 import org.bukkit.configuration.file.FileConfiguration;
 import org.bukkit.event.EventHandler;
 import org.bukkit.event.EventPriority;
 import org.bukkit.event.Listener;
+import org.bukkit.event.block.Action;
+import org.bukkit.event.player.PlayerInteractEvent;
 import org.bukkit.event.player.PlayerJoinEvent;
 import org.bukkit.plugin.java.JavaPlugin;
 
     public void onPlayerJoin(PlayerJoinEvent event) {
         Util.sendPlayerMessage(event.getPlayer(), "AtmCraft enabled.");
     }
-	
-}
+
+    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
+    public void onPlayerInteract(PlayerInteractEvent event) {
+    	if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
+    		Block block = event.getClickedBlock();
+    		if (isAtmBlock(block)) {
+                Util.sendPlayerMessage(event.getPlayer(), "seems like an ATM!");
+                AtmSession atmSession = new AtmSession(event.getPlayer());
+                atmSession.display();
+    		}
+    	}
+    }
+    
+    private boolean isAtmBlock(Block block) {
+    	if (block != null) {
+        	Logger.info("blocktype is chest: " + (block.getType() == Material.CHEST));
+        	Logger.info("above block: " + getBlockTypeAbove(block, 1));
+        	Logger.info("below block: " + getBlockTypeAbove(block, -1));
+    	}
+    	
+    	return (
+    			block != null &&
+    			block.getType() == Material.CHEST &&
+    			getBlockTypeAbove(block, 1) == Material.REDSTONE_LAMP_ON &&
+    			getBlockTypeAbove(block, -1) == Material.QUARTZ_BLOCK
+    	);
+    }
+    
+    private Material getBlockTypeAbove(Block block, int offset) {
+    	Block aboveBlock = block.getRelative(0, offset, 0);
+    	if (aboveBlock != null) {
+    		return aboveBlock.getType();
+    	}
+    	else {
+    		return null;
+    	}
+    }
+}

File src/main/java/com/zzzcomputing/bukkit/atmcraft/AtmSession.java

+package com.zzzcomputing.bukkit.atmcraft;
+
+import org.bukkit.Bukkit;
+import org.bukkit.entity.Player;
+import org.bukkit.inventory.Inventory;
+import org.bukkit.inventory.InventoryHolder;
+
+public class AtmSession implements InventoryHolder {
+	private Inventory inventory;
+	private Player player;
+
+	public AtmSession(Player player) {
+		this.player = player;
+	}
+
+	@Override
+	public Inventory getInventory() {
+		if (inventory == null) {
+			inventory = Bukkit.createInventory(this, 18, "Welcome " + player.getDisplayName() + "!");
+		}
+		return inventory;
+	}
+	
+	public void display() {
+        if (getInventory().getViewers().contains(player)) {
+            throw new IllegalArgumentException(player.getName() + " is already viewing " + getInventory().getTitle());
+        }
+        player.openInventory(getInventory());
+	}
+	
+	
+}
+
+
+

File src/main/java/com/zzzcomputing/bukkit/atmcraft/Bootstrap.java

-package com.zzzcomputing.bukkit.atmcraft;
-
-public class Bootstrap {
-
-}