else if(block.getType() != Material.AIR)
if(!block.isSame(event.getClickedBlock())) return;
if (!EventUtil.passesFilter(event)) return;
LocalPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
if (!sneakingState.doesPass(player.isSneaking()) || event.getPlayer().getLevel() < 1)
return;
int max = Integer.MAX_VALUE;
if(requireBottle) {
if(player.getHeldItemInfo().getType() != Material.GLASS_BOTTLE && block.getType() != Material.AIR) {
player.printError("mech.xp-storer.bottle");
return;
}
max = event.getPlayer().getItemInHand().getAmount();
}
if(!player.hasPermission("craftbook.mech.xpstore.use")) {
if(CraftBookPlugin.inst().getConfiguration().showPermissionMessages)
player.printError("mech.use-permission");
return;
}
if(event.getClickedBlock() != null && !ProtectionUtil.canUse(event.getPlayer(), event.getClickedBlock().getLocation(), event.getBlockFace(), event.getAction())) {
if(CraftBookPlugin.inst().getConfiguration().showPermissionMessages)
player.printError("area.use-permissions");
return;
}
int xp = 0;
float pcnt = event.getPlayer().getExp();
int level = event.getPlayer().getLevel();
event.getPlayer().setExp(0);
xp += (int)(event.getPlayer().getExpToLevel()*pcnt);
while (event.getPlayer().getLevel() > 0) {
event.getPlayer().setLevel(event.getPlayer().getLevel() - 1);
xp += event.getPlayer().getExpToLevel();
}
event.getPlayer().setLevel(level);
event.getPlayer().setExp(pcnt);
if (xp < xpPerBottle) {
player.print("mech.xp-storer.not-enough-xp");
return;
}
int bottleCount = (int) Math.min(max, Math.floor(xp / xpPerBottle));
event.getPlayer().getInventory().removeItem(new ItemStack(Material.GLASS_BOTTLE, bottleCount));
if(event.getClickedBlock() == null)
for(ItemStack leftOver : event.getPlayer().getInventory().addItem(new ItemStack(Material.EXP_BOTTLE, bottleCount)).values())
event.getPlayer().getWorld().dropItemNaturally(event.getPlayer().getLocation(), leftOver);
else
event.getClickedBlock().getWorld().dropItemNaturally(event.getClickedBlock().getLocation(), new ItemStack(Material.EXP_BOTTLE, bottleCount));
event.getPlayer().setLevel(0);
event.getPlayer().setExp(0);
float levelPercentage = 0;
int remainingXP = xp - bottleCount*xpPerBottle;
do {
levelPercentage = (float)remainingXP / event.getPlayer().getExpToLevel();
if(levelPercentage > 1) {
remainingXP -= event.getPlayer().getExpToLevel();
event.getPlayer().setLevel(event.getPlayer().getLevel() + 1);
} else if(levelPercentage == 1) {
event.getPlayer().setLevel(event.getPlayer().getLevel() + 1);
event.getPlayer().setExp(0f);
remainingXP = 0;
} else {
event.getPlayer().setExp(levelPercentage);
remainingXP = 0;
}
} while(levelPercentage > 1);
player.print("mech.xp-storer.success");
event.setCancelled(true);
}