Skip to content
Permalink
Browse files

Added radius mode to the XP Storer.

  • Loading branch information...
me4502 committed Oct 18, 2016
1 parent 65f321b commit c2b495a939e94a41ca8cd51c9c935f7046bbfddb
@@ -252,6 +252,7 @@ public static String getPlayersLanguage(Player p) {
put("mech.teleport.invalidcoords", "The entered coordinates are invalid!");
put("mech.teleport.obstruct","Your destination is obstructed!");

put("mech.xp-storer.create", "XP Storer Created!");
put("mech.xp-storer.bottle", "You need a bottle to perform this mechanic!");
put("mech.xp-storer.success", "You package your experience into a bottle!");
put("mech.xp-storer.not-enough-xp", "You do not have enough experience to fill a bottle!");
@@ -1,19 +1,28 @@
package com.sk89q.craftbook.mechanics;

import com.sk89q.craftbook.ChangedSign;
import com.sk89q.craftbook.bukkit.util.BukkitUtil;
import com.sk89q.craftbook.util.*;
import com.sk89q.craftbook.util.events.SelfTriggerPingEvent;
import com.sk89q.craftbook.util.events.SelfTriggerThinkEvent;
import com.sk89q.worldedit.Vector;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.*;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

import com.sk89q.craftbook.AbstractCraftBookMechanic;
import com.sk89q.craftbook.LocalPlayer;
import com.sk89q.craftbook.bukkit.CraftBookPlugin;
import com.sk89q.craftbook.util.EventUtil;
import com.sk89q.craftbook.util.ItemInfo;
import com.sk89q.craftbook.util.ProtectionUtil;
import com.sk89q.craftbook.util.TernaryState;
import com.sk89q.util.yaml.YAMLProcessor;

public class XPStorer extends AbstractCraftBookMechanic {
@@ -133,10 +142,124 @@ else if(block.getType() != Material.AIR)
event.setCancelled(true);
}

@EventHandler(priority = EventPriority.HIGH)
public void onSignChange(SignChangeEvent event) {

if (!autonomousMode) return;
if(!EventUtil.passesFilter(event)) return;

if (!event.getLine(1).equalsIgnoreCase("[XP]")) return;

LocalPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());

if (!player.hasPermission("craftbook.mech.xpstore")) {
if(CraftBookPlugin.inst().getConfiguration().showPermissionMessages)
player.printError("mech.create-permission");
SignUtil.cancelSign(event);
return;
}

int signRadius = radius;
try {
signRadius = Math.max(radius, Integer.parseInt(event.getLine(2)));
} catch (Exception e) {
}

event.setLine(1, "[XP]");
event.setLine(2, String.valueOf(signRadius));
player.print("mech.xp-storer.create");

CraftBookPlugin.inst().getSelfTriggerManager().registerSelfTrigger(event.getBlock().getLocation());
}

@EventHandler(priority = EventPriority.HIGH)
public void onPing(SelfTriggerPingEvent event) {

if (!autonomousMode) return;
if(!EventUtil.passesFilter(event)) return;

if(!SignUtil.isSign(event.getBlock())) return;

ChangedSign sign = BukkitUtil.toChangedSign(event.getBlock());

if(!sign.getLine(1).equals("[XP]")) return;

CraftBookPlugin.inst().getSelfTriggerManager().registerSelfTrigger(event.getBlock().getLocation());
}

@EventHandler(priority = EventPriority.HIGH)
public void onThink(SelfTriggerThinkEvent event) {

if (!EventUtil.passesFilter(event)) return;

if (!SignUtil.isSign(event.getBlock())) return;

ChangedSign sign = BukkitUtil.toChangedSign(event.getBlock());

if (!sign.getLine(1).equals("[XP]")) return;

event.setHandled(true);

int xp = 0;

for (Entity entity : LocationUtil.getNearbyEntities(event.getBlock().getLocation(), new Vector(radius,radius,radius))) {
if (entity instanceof ExperienceOrb && entity.getTicksLived() > 20) {
xp += ((ExperienceOrb) entity).getExperience();
entity.remove();
}
}

int max = Integer.MAX_VALUE;
Inventory inventory = null;

if (InventoryUtil.doesBlockHaveInventory(event.getBlock().getRelative(BlockFace.UP))) {
inventory = ((InventoryHolder) event.getBlock().getState()).getInventory();
if (requireBottle) {
max = 0;
for (ItemStack stack : inventory.getContents()) {
if (ItemUtil.isStackValid(stack) && stack.getType() == Material.GLASS_BOTTLE) {
max += stack.getAmount();
}
}
}
} else if (requireBottle) {
return;
}

int bottleCount = (int) Math.min(max, Math.floor(xp / xpPerBottle));

int tempBottles = bottleCount;

while(tempBottles > 0) {
ItemStack bottles = new ItemStack(Material.EXP_BOTTLE, Math.min(tempBottles, 64));
if (inventory != null) {
for (ItemStack leftover : inventory.addItem(bottles).values()) {
event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), leftover);
}
} else {
event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), bottles);
}

tempBottles -= 64;
}

if(requireBottle && inventory != null) {
inventory.removeItem(new ItemStack(Material.GLASS_BOTTLE, bottleCount));
}

int remainingXP = xp - bottleCount * xpPerBottle;
if (remainingXP > 0) {
ExperienceOrb orb = (ExperienceOrb) event.getBlock().getWorld().spawnEntity(event.getBlock().getLocation(), EntityType.EXPERIENCE_ORB);
orb.setExperience(remainingXP);
}
}

private boolean requireBottle;
private int xpPerBottle;
private ItemInfo block;
private TernaryState sneakingState;
private boolean autonomousMode;
private int radius;

@Override
public void loadConfiguration(YAMLProcessor config, String path) {
@@ -152,5 +275,11 @@ public void loadConfiguration(YAMLProcessor config, String path) {

config.setComment(path + "require-sneaking-state", "Sets how the player must be sneaking in order to use the XP Storer.");
sneakingState = TernaryState.getFromString(config.getString(path + "require-sneaking-state", "no"));

config.setComment(path + "radius-mode", "Allows XP Storer mechanics with a sign attached to work in a radius.");
autonomousMode = config.getBoolean(path + "radius-mode", false);

config.setComment(path + "radius", "The radius for radius-mode.");
radius = config.getInt(path + "radius", 5);
}
}

0 comments on commit c2b495a

Please sign in to comment.
You can’t perform that action at this time.