Permalink
Browse files

Fixed a few errors with BetterLeads.

  • Loading branch information...
me4502 committed Apr 8, 2017
1 parent f3da9d5 commit 8c87d622ca280bde1a7f667505c1222a7dbd8f38
View
@@ -58,7 +58,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
- <version>1.11-R0.1-SNAPSHOT</version>
+ <version>1.11.2-R0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sk89q</groupId>
@@ -4,6 +4,7 @@
import com.sk89q.craftbook.LocalPlayer;
import com.sk89q.craftbook.bukkit.CraftBookPlugin;
import com.sk89q.craftbook.util.EventUtil;
+import com.sk89q.craftbook.util.InventoryUtil;
import com.sk89q.craftbook.util.ItemUtil;
import com.sk89q.util.yaml.YAMLProcessor;
import org.bukkit.Bukkit;
@@ -25,12 +26,11 @@
public class BetterLeads extends AbstractCraftBookMechanic {
@EventHandler(priority = EventPriority.HIGH)
- public void onPlayerClick(PlayerInteractEntityEvent event) {
-
- if(!ItemUtil.isStackValid(event.getPlayer().getItemInHand())) return;
+ public void onPlayerClick(final PlayerInteractEntityEvent event) {
+ if(!ItemUtil.isStackValid(InventoryUtil.getItemInHand(event.getPlayer(), event.getHand()))) return;
if(!(event.getRightClicked() instanceof LivingEntity)) return;
LocalPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
- if(player.getHeldItemInfo().getType() != Material.LEASH) return;
+ if(InventoryUtil.getItemInHand(event.getPlayer(), event.getHand()).getType() != Material.LEASH) return;
if (!EventUtil.passesFilter(event)) return;
@@ -64,21 +64,25 @@ else if (typeName == null)
}
CraftBookPlugin.logDebugMessage("Leashing entity!", "betterleads.allowed-mobs");
- if(!((LivingEntity) event.getRightClicked()).setLeashHolder(event.getPlayer()))
- CraftBookPlugin.logDebugMessage("Failed to leash entity!", "betterleads.allowed-mobs");
- else {
- if(event.getRightClicked() instanceof Creature && ((Creature) event.getRightClicked()).getTarget() != null && ((Creature) event.getRightClicked()).getTarget().equals(event.getPlayer()))
- ((Creature) event.getRightClicked()).setTarget(null); //Rescan for a new target.
- event.setCancelled(true);
- if(event.getPlayer().getGameMode() == GameMode.CREATIVE)
- return;
- if(event.getPlayer().getItemInHand().getAmount() == 1)
- event.getPlayer().setItemInHand(null);
- else {
- ItemStack newStack = event.getPlayer().getItemInHand();
- newStack.setAmount(newStack.getAmount() - 1);
- event.getPlayer().setItemInHand(newStack);
+ if(event.getRightClicked() instanceof Creature && ((Creature) event.getRightClicked()).getTarget() != null && ((Creature) event.getRightClicked()).getTarget().equals(event.getPlayer()))
+ ((Creature) event.getRightClicked()).setTarget(null); //Rescan for a new target.
+ event.setCancelled(true);
+ Bukkit.getScheduler().runTask(CraftBookPlugin.inst(), new Runnable() {
+ @Override
+ public void run() {
+ if(!((LivingEntity) event.getRightClicked()).setLeashHolder(event.getPlayer())) {
+ CraftBookPlugin.logDebugMessage("Failed to leash entity!", "betterleads.allowed-mobs");
+ }
}
+ });
+ if(event.getPlayer().getGameMode() == GameMode.CREATIVE)
+ return;
+ if(InventoryUtil.getItemInHand(event.getPlayer(), event.getHand()).getAmount() == 1)
+ InventoryUtil.setItemInHand(event.getPlayer(), event.getHand(), null);
+ else {
+ ItemStack newStack = InventoryUtil.getItemInHand(event.getPlayer(), event.getHand());
+ newStack.setAmount(newStack.getAmount() - 1);
+ InventoryUtil.setItemInHand(event.getPlayer(), event.getHand(), newStack);
}
}
@@ -98,6 +102,7 @@ public void onEntityTarget(EntityTargetEvent event) {
if(((LivingEntity) event.getEntity()).getLeashHolder().equals(event.getTarget())) {
event.setTarget(null);
event.setCancelled(true);
+ return;
}
}
@@ -109,7 +114,7 @@ public void onEntityTarget(EntityTargetEvent event) {
if(((LivingEntity) ent).getLeashHolder().equals(event.getTarget())) {
event.setTarget(null);
event.setCancelled(true);
- break;
+ return;
}
}
}
@@ -9,7 +9,9 @@
import org.bukkit.block.BlockState;
import org.bukkit.block.BrewingStand;
import org.bukkit.block.Furnace;
+import org.bukkit.entity.Player;
import org.bukkit.inventory.BrewerInventory;
+import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
@@ -217,4 +219,22 @@ public static boolean doesBlockHaveInventory(Block block) {
return false;
}
}
+
+ public static ItemStack getItemInHand(Player player, EquipmentSlot slot) {
+ if (slot == EquipmentSlot.HAND) {
+ return player.getInventory().getItemInMainHand();
+ } else if (slot == EquipmentSlot.OFF_HAND) {
+ return player.getInventory().getItemInOffHand();
+ }
+
+ return null;
+ }
+
+ public static void setItemInHand(Player player, EquipmentSlot slot, ItemStack itemStack) {
+ if (slot == EquipmentSlot.HAND) {
+ player.getInventory().setItemInMainHand(itemStack);
+ } else if (slot == EquipmentSlot.OFF_HAND) {
+ player.getInventory().setItemInOffHand(itemStack);
+ }
+ }
}

0 comments on commit 8c87d62

Please sign in to comment.