Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bin
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>bottleO</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
61 changes: 60 additions & 1 deletion src/com/untamedears/bottleO/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -223,10 +225,67 @@ public void onPlayerInteractEvent(PlayerInteractEvent e) {

p.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 40, 3));

// Add empty bottles back to inventory
HashMap<Integer, ItemStack> hash = p.getInventory().addItem(new ItemStack(Material.GLASS_BOTTLE, numberOfBottles));
//otherwise replace air in hand and drop glass bottles
if (!hash.isEmpty()) {
Iterator<Integer> 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<Integer, ItemStack> hash = p.getInventory().addItem(new ItemStack(Material.GLASS_BOTTLE, amount));
//otherwise replace air in hand and drop glass bottles
if (!hash.isEmpty()) {
Iterator<Integer> 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);
}
}
}
Expand Down