From 90f1c5867087dd23684b43634dc083c648aa2579 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Mon, 7 Jun 2021 00:36:46 -0500 Subject: [PATCH] Close #38 Implement making a fox sleep with shift right click with an empty hand --- README.md | 1 + pom.xml | 5 +- .../tamablefoxes/versions/FieldHelper.java | 46 ++++--------------- .../version_1_14_R1/EntityTamableFox.java | 6 +++ .../version_1_15_R1/EntityTamableFox.java | 6 +++ .../version_1_16_R1/EntityTamableFox.java | 6 +++ .../version_1_16_R2/EntityTamableFox.java | 6 +++ .../version_1_16_R3/EntityTamableFox.java | 7 +++ 8 files changed, 45 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 4604060..fd12f13 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Features: * Foxes follow owner * You can shift + right-click to let the fox hold items * Right-click to make the fox sit +* Shift Right-click with an empty hand to make the fox sleep * If the fox is holding a totem of undying, the fox will consume it and be reborn. * Foxes attack the owner's target * Foxes attack the thing that attacked the owner. diff --git a/pom.xml b/pom.xml index 7a3f624..a6c3dc9 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ net.seanomik tamablefoxes - 1.9.1-SNAPSHOT + 1.9.2-SNAPSHOT jar Tamablefoxes @@ -47,8 +47,9 @@ - D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.5_\plugins\TamableFoxes_v${project.version}.jar + + D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.5_\plugins\TamableFoxes_v${project.version}.jar false diff --git a/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java b/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java index fdd80e4..52a28b1 100644 --- a/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java +++ b/src/main/java/net/seanomik/tamablefoxes/versions/FieldHelper.java @@ -6,48 +6,22 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; -import java.lang.invoke.VarHandle; +//import java.lang.invoke.VarHandle; import java.lang.reflect.Field; import java.lang.reflect.Modifier; public final class FieldHelper { - - private static final VarHandle MODIFIERS; - - static { - String version = System.getProperty("java.version"); - if (!version.startsWith("1.8")) { - try { - MethodHandles.Lookup lookup = MethodHandles.privateLookupIn(Field.class, MethodHandles.lookup()); - MODIFIERS = lookup.findVarHandle(Field.class, "modifiers", int.class); - } catch (IllegalAccessException | NoSuchFieldException ex) { - throw new RuntimeException(ex); - } - } else { - MODIFIERS = null; - } - } - public static void makeNonFinal(Field field) { - // Check if we're running a supported java version for this new method. - if (MODIFIERS == null) { - try { - if ((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL) { - Field fieldMutable = field.getClass().getDeclaredField("modifiers"); - fieldMutable.setAccessible(true); - fieldMutable.set(field, fieldMutable.getInt(field) & ~Modifier.FINAL); - fieldMutable.setAccessible(false); - } - } catch (NoSuchFieldException | IllegalAccessException e) { - Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace()); - e.printStackTrace(); - } - } else { - int mods = field.getModifiers(); - if (Modifier.isFinal(mods)) { - MODIFIERS.set(field, mods & ~Modifier.FINAL); + try { + if ((field.getModifiers() & Modifier.FINAL) == Modifier.FINAL) { + Field fieldMutable = field.getClass().getDeclaredField("modifiers"); + fieldMutable.setAccessible(true); + fieldMutable.set(field, fieldMutable.getInt(field) & ~Modifier.FINAL); + fieldMutable.setAccessible(false); } + } catch (NoSuchFieldException | IllegalAccessException e) { + Bukkit.getServer().getConsoleSender().sendMessage(Utils.getPrefix() + ChatColor.RED + LanguageConfig.getFailureReplace()); + e.printStackTrace(); } } diff --git a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_14_R1/EntityTamableFox.java b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_14_R1/EntityTamableFox.java index b3e6635..1a11507 100644 --- a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_14_R1/EntityTamableFox.java +++ b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_14_R1/EntityTamableFox.java @@ -274,6 +274,7 @@ public class EntityTamableFox extends EntityFox { // If the player is not sneaking and the fox cannot breed, then make the fox sit. if (!entityhuman.isSneaking() && (!flag || this.isBaby())) { + this.setSleeping(false); this.goalSit.setSitting(!this.isSitting()); return flag; } else if (entityhuman.isSneaking()) { // Swap/Put/Take item from fox. @@ -300,6 +301,11 @@ public class EntityTamableFox extends EntityFox { this.setSlot(EnumItemSlot.MAINHAND, c); } + // If the player doesn't have anything in their hand, make the fox sleep or wakeup. + else { + this.goalSit.setSitting(false); + this.setSleeping(!this.isSleeping()); + } }, (long) 0.1); return true; diff --git a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_15_R1/EntityTamableFox.java b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_15_R1/EntityTamableFox.java index 2026e52..da33406 100644 --- a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_15_R1/EntityTamableFox.java +++ b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_15_R1/EntityTamableFox.java @@ -273,6 +273,7 @@ public class EntityTamableFox extends EntityFox { // If the player is not sneaking and the fox cannot breed, then make the fox sit. if (!entityhuman.isSneaking() && (!flag || this.isBaby())) { + this.setSleeping(false); this.goalSit.setSitting(!this.isSitting()); return flag; } else if (entityhuman.isSneaking()) { // Swap/Put/Take item from fox. @@ -299,6 +300,11 @@ public class EntityTamableFox extends EntityFox { this.setSlot(EnumItemSlot.MAINHAND, c); } + // If the player doesn't have anything in their hand, make the fox sleep or wakeup. + else { + this.goalSit.setSitting(false); + this.setSleeping(!this.isSleeping()); + } }, (long) 0.1); return true; diff --git a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R1/EntityTamableFox.java b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R1/EntityTamableFox.java index 64107ca..08c17b8 100644 --- a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R1/EntityTamableFox.java +++ b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R1/EntityTamableFox.java @@ -273,6 +273,7 @@ public class EntityTamableFox extends EntityFox { // If the player is not sneaking and the fox cannot breed, then make the fox sit. // @TODO: Do I need to use this.eQ() instead of flag != EnumInteractionResult.SUCCESS? if (!entityhuman.isSneaking() && (flag != EnumInteractionResult.SUCCESS || this.isBaby())) { + this.setSleeping(false); this.goalSit.setSitting(!this.isSitting()); return flag; } else if (entityhuman.isSneaking()) { // Swap/Put/Take item from fox. @@ -301,6 +302,11 @@ public class EntityTamableFox extends EntityFox { this.setSlot(EnumItemSlot.MAINHAND, c); } + // If the player doesn't have anything in their hand, make the fox sleep or wakeup. + else { + this.goalSit.setSitting(false); + this.setSleeping(!this.isSleeping()); + } }, (long) 0.1); return EnumInteractionResult.SUCCESS; diff --git a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R2/EntityTamableFox.java b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R2/EntityTamableFox.java index 03d7742..474e1f9 100644 --- a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R2/EntityTamableFox.java +++ b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R2/EntityTamableFox.java @@ -270,6 +270,7 @@ public class EntityTamableFox extends EntityFox { // If the player is not sneaking and the fox cannot breed, then make the fox sit. // @TODO: Do I need to use this.eQ() instead of flag != EnumInteractionResult.SUCCESS? if (!entityhuman.isSneaking() && (flag != EnumInteractionResult.SUCCESS || this.isBaby())) { + this.setSleeping(false); this.goalSit.setSitting(!this.isSitting()); return flag; } else if (entityhuman.isSneaking()) { // Swap/Put/Take item from fox. @@ -298,6 +299,11 @@ public class EntityTamableFox extends EntityFox { this.setSlot(EnumItemSlot.MAINHAND, c); } + // If the player doesn't have anything in their hand, make the fox sleep or wakeup. + else { + this.goalSit.setSitting(false); + this.setSleeping(!this.isSleeping()); + } }, (long) 0.1); return EnumInteractionResult.SUCCESS; diff --git a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R3/EntityTamableFox.java b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R3/EntityTamableFox.java index bb7776b..ac82927 100644 --- a/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R3/EntityTamableFox.java +++ b/src/main/java/net/seanomik/tamablefoxes/versions/version_1_16_R3/EntityTamableFox.java @@ -270,6 +270,7 @@ public class EntityTamableFox extends EntityFox { // If the player is not sneaking and the fox cannot breed, then make the fox sit. // @TODO: Do I need to use this.eQ() instead of flag != EnumInteractionResult.SUCCESS? if (!entityhuman.isSneaking() && (flag != EnumInteractionResult.SUCCESS || this.isBaby())) { + this.setSleeping(false); this.goalSit.setSitting(!this.isSitting()); return flag; } else if (entityhuman.isSneaking() && enumhand == EnumHand.MAIN_HAND) { // Swap/Put/Take item from fox. @@ -278,6 +279,7 @@ public class EntityTamableFox extends EntityFox { return EnumInteractionResult.PASS; } + // Check if the player has something in their main hand. if (!this.getEquipment(EnumItemSlot.MAINHAND).isEmpty()) { getBukkitEntity().getWorld().dropItem(getBukkitEntity().getLocation(), CraftItemStack.asBukkitCopy(this.getEquipment(EnumItemSlot.MAINHAND))); this.setSlot(EnumItemSlot.MAINHAND, new ItemStack(Items.AIR)); @@ -298,6 +300,11 @@ public class EntityTamableFox extends EntityFox { this.setSlot(EnumItemSlot.MAINHAND, c); } + // If the player doesn't have anything in their hand, make the fox sleep or wakeup. + else { + this.goalSit.setSitting(false); + this.setSleeping(!this.isSleeping()); + } }, (long) 0.1); return EnumInteractionResult.SUCCESS;