diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/src/org/bukkitmon/BMMob.java b/src/org/bukkitmon/BMMob.java new file mode 100644 index 0000000..86bd689 --- /dev/null +++ b/src/org/bukkitmon/BMMob.java @@ -0,0 +1,67 @@ +package org.bukkitmon; + +import org.bukkit.entity.MobType; + +public class BMMob { + + private byte nrOfMobs = 1; + private MobType mobtype = MobType.CHICKEN; + private boolean rndAmount = false; + private boolean rndType = false; + private boolean active = true; + private byte maxAmount = 10; + + public BMMob() + { + + } + + public boolean isRndAmount() { + return rndAmount; + } + + public void setRndAmount(boolean rndAmount) { + this.rndAmount = rndAmount; + } + + public boolean isRndType() { + return rndType; + } + + public void setRndType(boolean rndType) { + this.rndType = rndType; + } + + public byte getNrOfMobs() { + return nrOfMobs; + } + + public void setNrOfMobs(byte nrOfMobs) { + this.nrOfMobs = nrOfMobs; + } + + public MobType getMobtype() { + return mobtype; + } + + public void setMobtype(MobType mobtype) { + this.mobtype = mobtype; + } + + public boolean isActive() { + return active; + } + + public void setActive(boolean active) { + this.active = active; + } + + public byte getMaxAmount() { + return maxAmount; + } + + public void setMaxAmount(byte maxAmount) { + this.maxAmount = maxAmount; + } + +} diff --git a/src/org/bukkitmon/BMPListener.java b/src/org/bukkitmon/BMPListener.java new file mode 100644 index 0000000..badd373 --- /dev/null +++ b/src/org/bukkitmon/BMPListener.java @@ -0,0 +1,155 @@ +package org.bukkitmon; + +import java.util.Hashtable; +import java.util.Random; + +import org.bukkit.ChatColor; +import org.bukkit.entity.MobType; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.event.player.PlayerEggThrowEvent; +import org.bukkit.event.player.PlayerListener; +import org.bukkit.plugin.Plugin; + +public class BMPListener extends PlayerListener { + + private Plugin plugin; + private Hashtable ph = new Hashtable(); + private Random rnd = new Random(); + private String bmStr = ChatColor.GOLD + "[BukkitMon] " + ChatColor.YELLOW; + private tPermissions perms; + + public BMPListener(BukkitMon plugin, tPermissions perms) + { + this.plugin = plugin; + this.perms = perms; + } + + public void onPlayerEggThrow(PlayerEggThrowEvent event) + { + Player player = event.getPlayer(); + if (!ph.containsKey(player.getName())) + ph.put(player.getName(), new BMMob()); + BMMob bm = ph.get(player.getName()); + if (bm.isActive()) + { + MobType[] mts = MobType.values(); + int rndint = rnd.nextInt(mts.length - 1); + if (bm.isRndAmount()) + event.setNumHatches((byte)rnd.nextInt(bm.getMaxAmount())); + else + event.setNumHatches(bm.getNrOfMobs()); + if (bm.isRndType()) + event.setHatchType(mts[rndint]); + else + event.setHatchType(bm.getMobtype()); + event.setHatching(true); + this.plugin.getServer().broadcastMessage(ChatColor.RED + "SPAWNING --> Watch out! " + ChatColor.YELLOW + player.getDisplayName() + + " spawned " + event.getNumHatches() + " " + event.getHatchType().getName() + "s"); + } + else + event.setHatching(false); + } + + public void onPlayerCommand(PlayerChatEvent event) + { + Player player = event.getPlayer(); + String[] split = event.getMessage().split(" "); + if (split[0].equalsIgnoreCase("/bm")) + { + if (split.length >= 2) + { + if (!ph.containsKey(player.getName())) + ph.put(player.getName(), new BMMob()); + BMMob bm = ph.get(player.getName()); + if (split[1].equalsIgnoreCase("mobs") && this.perms.canPlayerUseCommand(player.getName(), "mobs")) + { + if (split.length >= 3) + { + try{ + byte val = Byte.valueOf(split[2]); + if (val <= bm.getMaxAmount()) + { + bm.setNrOfMobs(val); + player.sendMessage(this.bmStr + "You set the number of mobs to spawn to: " + val); + } + else + player.sendMessage(this.bmStr + val + " is more than the max amount"); + } + catch (NumberFormatException nfe){ + player.sendMessage(this.bmStr + "You need to define a valid number"); + } + } + else + player.sendMessage(this.bmStr + "You need to define a number"); + } + else if (split[1].equalsIgnoreCase("mobtype") && this.perms.canPlayerUseCommand(player.getName(), "mobtype")) + { + if (split.length >= 3) + { + MobType mt = MobType.valueOf(split[2].toUpperCase()); + if (mt != null) + { + bm.setMobtype(mt); + player.sendMessage(this.bmStr + "You set the mob type to " + split[2]); + } + else + player.sendMessage(this.bmStr + "Invalid mobtype!"); + } + else + player.sendMessage(this.bmStr + "You need to define a mobtype"); + } + else if (split[1].equalsIgnoreCase("randomamount") && this.perms.canPlayerUseCommand(player.getName(), "randomamount")) + { + bm.setRndAmount(!bm.isRndAmount()); + player.sendMessage(this.bmStr + "You set random amount to " + bm.isRndAmount()); + } + else if (split[1].equalsIgnoreCase("randommob") && this.perms.canPlayerUseCommand(player.getName(), "randommob")) + { + bm.setRndAmount(!bm.isRndAmount()); + player.sendMessage(this.bmStr + "You set randommob to " + bm.isRndAmount()); + } + else if (split[1].equalsIgnoreCase("maxamount") && this.perms.canPlayerUseCommand(player.getName(), "maxamount")) + { + if (split.length >= 3) + { + try{ + byte val = Byte.valueOf(split[2]); + bm.setMaxAmount(val); + player.sendMessage(this.bmStr + "You set the max amount of mobs to spawn to: " + val); + } + catch (NumberFormatException nfe){ + player.sendMessage(this.bmStr + "You need to define a valid number"); + } + } + else + player.sendMessage(this.bmStr + "You need to define a number"); + } + else if (split[1].equalsIgnoreCase("activate") && this.perms.canPlayerUseCommand(player.getName(), "activate")) + { + bm.setActive(!bm.isActive()); + if (bm.isActive()) + player.sendMessage(this.bmStr + "You activated BukkitMon"); + else + player.sendMessage(this.bmStr + "You deactivated BukkitMon"); + + } + else if (split[1].equalsIgnoreCase("list") && this.perms.canPlayerUseCommand(player.getName(), "list")) + { + player.sendMessage(ChatColor.RED + "BukkitMon commands:"); + player.sendMessage(ChatColor.RED + "/bm mobs [#]" + ChatColor.YELLOW + " - Define how many mobs to spawn"); + player.sendMessage(ChatColor.RED + "/bm mobtype [type]" + ChatColor.YELLOW + " - Define the mobtype to spawn"); + player.sendMessage(ChatColor.RED + "/bm maxamount" + ChatColor.YELLOW + " - Defines the max amount of mobs you can spawn"); + player.sendMessage(ChatColor.RED + "/bm randomamount" + ChatColor.YELLOW + " - Toggle random amount on/off"); + player.sendMessage(ChatColor.RED + "/bm randommob" + ChatColor.YELLOW + " - Toggle random mob type on/off"); + player.sendMessage(ChatColor.RED + "/bm activate" + ChatColor.YELLOW + " - Activates the BukkitMon eggs"); + player.sendMessage(ChatColor.RED + "/bm list" + ChatColor.YELLOW + " - Shows a list of avaliable BukkitMon commands"); + } + else + player.sendMessage(this.bmStr + "Unknown BukkitMon command"); + } + else + player.sendMessage(this.bmStr + "Define a function please!"); + } + } +} diff --git a/src/org/bukkitmon/BukkitMon.java b/src/org/bukkitmon/BukkitMon.java new file mode 100644 index 0000000..691e6e4 --- /dev/null +++ b/src/org/bukkitmon/BukkitMon.java @@ -0,0 +1,56 @@ +package org.bukkitmon; + +import java.io.File; +import java.util.logging.Logger; + +import org.bukkit.Server; +import org.bukkit.event.Event; +import org.bukkit.event.Event.Priority; +import org.bukkit.plugin.PluginDescriptionFile; +import org.bukkit.plugin.PluginLoader; +import org.bukkit.plugin.java.JavaPlugin; + +public class BukkitMon extends JavaPlugin{ + + public static final Logger log = Logger.getLogger("Minecraft"); + private String name = "BukkitMon"; + private String version = "v0.1.0 (Bulbasaur)"; + private tPermissions perms = new tPermissions("BukkitMon" + File.separator + "bukkitmon.perms"); + private final BMPListener playerListener = new BMPListener(this, perms); + + public BukkitMon(PluginLoader pluginLoader, Server instance, + PluginDescriptionFile desc, File folder, File plugin, + ClassLoader cLoader) { + super(pluginLoader, instance, desc, folder, plugin, cLoader); + this.registerEvents(); + this.addCommands(); + this.perms.loadPermissions(); + this.perms.savePermissions(); + } + + public void onDisable() { + log.info(name + " " + version + " disabled!"); + } + + public void onEnable() { + log.info(name + " " + version + " enabled!"); + } + + public void addCommands() + { + perms.addCmd("mobs"); + perms.addCmd("mobtype"); + perms.addCmd("randomamount"); + perms.addCmd("randomtype"); + perms.addCmd("maxamount"); + perms.addCmd("activate"); + perms.addCmd("list"); + } + + public void registerEvents() + { + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_EGG_THROW, playerListener, Priority.Normal, this); + getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, playerListener, Priority.Normal, this); + } + +} diff --git a/src/org/bukkitmon/tPermissions.java b/src/org/bukkitmon/tPermissions.java new file mode 100644 index 0000000..e8fe94b --- /dev/null +++ b/src/org/bukkitmon/tPermissions.java @@ -0,0 +1,143 @@ +package org.bukkitmon; + + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Enumeration; +import java.util.Hashtable; + +public class tPermissions { + + private File file = new File("Guilds" + File.separator + "permissions.txt"); + private Hashtable> hashPerms = new Hashtable>(); + + public tPermissions(String filePath) + { + file = new File(filePath); + } + + public boolean canPlayerUseCommand(String player, String command) + { + if (this.hashPerms.containsKey(command)) + { + ArrayList alstr = this.hashPerms.get(command); + if (alstr.contains(player) || alstr.contains("*")) + return true; + else + return false; + } + return false; + } + + public void addCmd(String cmd) + { + if (!this.hashPerms.containsKey(cmd)) + { + ArrayList als = new ArrayList(); + als.add("---"); // Hashtables doesn't allow nulls + this.hashPerms.put(cmd, als); + } + } + + public void loadPermissions() + { + if (this.file.exists()) + { + if (this.file.isFile()) + { + try + { + BufferedReader br = new BufferedReader(new FileReader(this.file)); + try + { + String line = br.readLine(); + if (line != null) + { + while (line != null) + { + if (!line.startsWith("//") && !line.startsWith(";") && !line.startsWith("#") && !line.equals("")) + { + String[] splitted = line.split("="); + if (splitted.length >= 2) + { + String[] pSplit = splitted[1].split(","); + if (hashPerms.containsKey(splitted[0])) + { + this.hashPerms.put(splitted[0], new ArrayList(Arrays.asList(pSplit))); + } + } + } + line = br.readLine(); + } + } + } + finally + { + br.close(); + } + } + catch (IOException ioe) + { + System.out.println("[tPermissions] Failed to read " + this.file.getPath() + "!"); + } + } + else + System.out.println("[tPermissions] " + this.file.getPath() + " is not a file!"); + } + else + { + savePermissions(); + } + } + + public void savePermissions() + { + File dirs = new File(this.file.getPath().replace(this.file.getName(), "")); + if (!dirs.exists()) + dirs.mkdirs(); + try + { + BufferedWriter bw = new BufferedWriter(new FileWriter(this.file)); + + try + { + for (Enumeration e = this.hashPerms.keys(); e.hasMoreElements();) + { + String key = (String)e.nextElement(); + ArrayList alstr = this.hashPerms.get(key); + bw.write(key + "="); + boolean f = true; + for (String str : alstr) + { + if (!str.equals("---")) + { + if (f) + { + bw.write(str); + f = false; + } + else + bw.write("," + str); + } + } + bw.newLine(); + } + } + finally + { + bw.flush(); + bw.close(); + } + } + catch (IOException ioe) + { + System.out.println("[tPermissions] Unable to save file " + this.file.getPath()); + } + } +} diff --git a/src/plugin.yml b/src/plugin.yml new file mode 100644 index 0000000..967d0a1 --- /dev/null +++ b/src/plugin.yml @@ -0,0 +1,3 @@ +name: BukkitMon +main: org.bukkitmon.BukkitMon +version: 0.1 \ No newline at end of file