Advertisement
Evyatar12

Main.java - SignCommands

Feb 1st, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.55 KB | None | 0 0
  1. package com.thebukkitarea.signcommands;
  2.  
  3. import org.bukkit.block.Sign;
  4. import org.bukkit.event.EventHandler;
  5. import org.bukkit.event.Listener;
  6. import org.bukkit.event.block.Action;
  7. import org.bukkit.event.player.PlayerInteractEvent;
  8. import org.bukkit.plugin.java.JavaPlugin;
  9.  
  10. import java.util.*;
  11.  
  12. public class Main extends JavaPlugin implements Listener {
  13.  
  14.     private HashMap<Integer, String> signCommands = new HashMap<Integer, String>();
  15.  
  16.     @Override
  17.     public void onEnable() {
  18.         getServer().getPluginManager().registerEvents(this, this);
  19.  
  20.         for (Map.Entry<String, Object> entry : getConfig().getValues(false).entrySet()) {
  21.             int id;
  22.  
  23.             try { id = Integer.parseInt(entry.getKey()); }
  24.             catch (NumberFormatException ex) { continue; }
  25.  
  26.             signCommands.put(id, String.valueOf(entry.getValue()));
  27.         }
  28.         saveConfig();
  29.     }
  30.  
  31.     @EventHandler
  32.     public void onSignClick(PlayerInteractEvent event) {
  33.         if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Sign) {
  34.             Sign sign = (Sign) event.getClickedBlock().getState();
  35.             if (sign.getLine(0).equalsIgnoreCase("[command]")) {
  36.                 int id;
  37.  
  38.                 try { id = Integer.parseInt(sign.getLine(1)); }
  39.                 catch (NumberFormatException ex) { return; }
  40.  
  41.                 String cmd;
  42.                 if ((cmd = signCommands.get(id)) != null)
  43.                     event.getPlayer().performCommand(cmd);
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement