Implement making a fox sleep with shift right click with an empty hand
This commit is contained in:
SeanOMik 2021-06-07 00:36:46 -05:00
parent b026691586
commit 90f1c58670
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
8 changed files with 45 additions and 38 deletions

View File

@ -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.

View File

@ -6,7 +6,7 @@
<groupId>net.seanomik</groupId>
<artifactId>tamablefoxes</artifactId>
<version>1.9.1-SNAPSHOT</version>
<version>1.9.2-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Tamablefoxes</name>
@ -47,8 +47,9 @@
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.2_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.3_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.4_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.5_\plugins\TamableFoxes_v${project.version}.jar</outputFile>
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_1.16.5_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<!--<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.4_\plugins\TamableFoxes_v${project.version}.jar</outputFile>-->
<outputFile>D:\Code\java\spigotPlugins\_TEST_SERVER_PAPER_1.16.5_\plugins\TamableFoxes_v${project.version}.jar</outputFile>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</execution>

View File

@ -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();
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;