diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e56e04 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin diff --git a/.project b/.project new file mode 100644 index 0000000..35ab245 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + bottleO + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/src/com/untamedears/bottleO/EventListener.java b/src/com/untamedears/bottleO/EventListener.java index 04bc76b..bcb3fef 100644 --- a/src/com/untamedears/bottleO/EventListener.java +++ b/src/com/untamedears/bottleO/EventListener.java @@ -100,7 +100,7 @@ public void onPlayerExpChangeEvent(PlayerExpChangeEvent e) { } - //generate xp bottles + //generate xp bottles, or degenerate xp bottles @EventHandler(priority=EventPriority.HIGHEST) public void onPlayerInteractEvent(PlayerInteractEvent e) { //check the event isn't cancelled and they have clicked on an enchanting table @@ -211,9 +211,11 @@ public void onPlayerInteractEvent(PlayerInteractEvent e) { ItemStack i = p.getItemInHand(); Material m = e.getMaterial(); int amount = i.getAmount(); + int numberOfBottles = amount*BOTTLES_PER_EMERALD; int xp = amount*BOTTLES_PER_EMERALD*XP_PER_BOTTLE; if (m == Material.EMERALD_BLOCK) { xp *= EMERALDS_PER_BLOCK; + numberOfBottles *= EMERALDS_PER_BLOCK; } PlayerExpChangeEvent event = new PlayerExpChangeEvent(p, xp); Bukkit.getPluginManager().callEvent(event); @@ -223,10 +225,67 @@ public void onPlayerInteractEvent(PlayerInteractEvent e) { p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 40, 3)); + // Add empty bottles back to inventory + HashMap hash = p.getInventory().addItem(new ItemStack(Material.GLASS_BOTTLE, numberOfBottles)); + //otherwise replace air in hand and drop glass bottles + if (!hash.isEmpty()) { + Iterator it = hash.keySet().iterator(); + while (it.hasNext()) { + ItemStack glassStack = hash.get(it.next()); + p.getWorld().dropItem(p.getLocation(), glassStack); + } + } //restart cool-down timer playerWaitHash.put(p.getName(), System.currentTimeMillis()); bottleO.log.info("xp recovered! "+m.toString()+", "+amount+", "+xp); + } else if (e.getMaterial() == Material.EXP_BOTTLE ) { + Player p = e.getPlayer(); + //check player has waited for the required amount of time + if (!playerWaitHash.containsKey(p.getName())) { + //if there is no time recorded, add the current time + bottleO.log.info("no record of "+p.getName()+" logging in!"); + playerWaitHash.put(p.getName(), System.currentTimeMillis()); + } + long loginTime = playerWaitHash.get(p.getName()); + long timeDiff = System.currentTimeMillis() - (loginTime+WAIT_TIME_MILLIS); + if (timeDiff < 0) { + bottleO.log.info(p.getName()+" must wait "+(float)(-timeDiff)/1000+" seconds!"); + p.sendMessage(ChatColor.RED+"["+bottleO.pluginName+"]"+ChatColor.WHITE+" you must wait "+ChatColor.RED+(float)(-timeDiff)/1000+ChatColor.WHITE+" seconds to make more exp bottles."); + return; + } + + //calculate amount of xp, refund it. + ItemStack i = p.getItemInHand(); + int amount = i.getAmount(); + int xp = amount*XP_PER_BOTTLE; + + int totalXP = getLegacyTotalXP(p.getLevel(),p.getExp()); + int newTotalXP = totalXP + xp; + int legacyLevel = getLegacyLevel(newTotalXP); + p.setTotalExperience(0); + p.setLevel(legacyLevel); + p.setExp(getLegacyExp(legacyLevel, newTotalXP)); + + //Remove item in hand (empties go to inventory first) + p.setItemInHand(new ItemStack(Material.AIR)); + + p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 40, 3)); + + // Add empty bottles back to inventory + HashMap hash = p.getInventory().addItem(new ItemStack(Material.GLASS_BOTTLE, amount)); + //otherwise replace air in hand and drop glass bottles + if (!hash.isEmpty()) { + Iterator it = hash.keySet().iterator(); + while (it.hasNext()) { + ItemStack glassStack = hash.get(it.next()); + p.getWorld().dropItem(p.getLocation(), glassStack); + } + } + //restart cool-down timer + playerWaitHash.put(p.getName(), System.currentTimeMillis()); + + bottleO.log.info("xp recovered! Experience Bottle, "+amount+", "+xp); } } }