Skip to content

Commit

Permalink
Java 17 and minecraft 1.17 support (#80)
Browse files Browse the repository at this point in the history
* Updated methods for 1

* update

* update

* updated for 1.17

* Fixed renamming

* 1.18 support

* Update ci.yml

Added java 16 and 17

* pom local update

* typo

* updated java tested versions (only major one)

* Update src/main/java/fr/zcraft/quartzlib/components/gui/PromptGui.java

removed commented code

Co-authored-by: Amaury Carrade <amaury@carrade.eu>

* Done changed according to the reviews made by amauryPi

* removed unused tileentitysign

* removed origin file

Co-authored-by: Vlammar <valentin.jabre@gmail.com>
Co-authored-by: Amaury Carrade <amaury@carrade.eu>
  • Loading branch information
3 people authored Dec 18, 2021
1 parent 126db0c commit 13963da
Show file tree
Hide file tree
Showing 11 changed files with 538 additions and 217 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
java: [8, 9, 10, 11, 12, 13, 14, 15]
java: [8, 11, 16, 17]
steps:
- uses: actions/checkout@v2
- name: Set up JDK 1.8
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

<groupId>fr.zcraft</groupId>
<artifactId>quartz</artifactId>
<version>0.0.1</version>
<version>0.0.5</version>

<packaging>pom</packaging>

Expand All @@ -50,6 +50,6 @@

<modules>
<module>quartzlib</module>
<module>ztoaster</module>

</modules>
</project>
2 changes: 1 addition & 1 deletion quartzlib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.13.1</junit.version>
<revision>0.0.1-SNAPSHOT</revision>
<revision>0.0.5-SNAPSHOT</revision>
</properties>

<build>
Expand Down
66 changes: 48 additions & 18 deletions src/main/java/fr/zcraft/quartzlib/components/gui/PromptGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,38 @@ public class PromptGui extends GuiBase {
private Location signLocation;
private String contents;


public PromptGui(Callback<String> callback, String contents) {
this(callback);
this.contents = contents;
}

/**
* Creates a new prompt GUI, using the given callback.
*
* @param callback The callback to be given the input text to.
*/
public PromptGui(Callback<String> callback) {
super();

if (!isAvailable()) {
throw new IllegalStateException("Sign-based prompt GUI are not available");
}

this.callback = callback;
}


/**
* Checks if Prompt GUIs can be correctly used on this Minecraft versions.
*/
public static boolean isAvailable() {

if (!isInitialized) {
init();
}
return fieldTileEntitySign != null;

}

public static void prompt(Player owner, Callback<String> callback) {
Expand All @@ -103,32 +109,54 @@ private static void init() {
isInitialized = true;

try {
final Class<?> CraftBlockEntityState = Reflection.getBukkitClassByName("block.CraftBlockEntityState");
final Class<?> CraftSign = Reflection.getBukkitClassByName("block.CraftSign");
final Class<?> classTileEntitySign = Reflection.getMinecraftClassByName("TileEntitySign");
final Class<?> CraftBlockEntityState =
Reflection.getBukkitClassByName("block.CraftBlockEntityState");
final Class<?> classTileEntitySign
= Reflection.getMinecraft1_17ClassByName("world.level.block.entity.TileEntitySign");
final Class<?> CraftPlayer = Reflection.getBukkitClassByName("entity.CraftPlayer");
final Class<?> EntityHuman = Reflection.getMinecraftClassByName("EntityHuman");

final Class<?> EntityHuman = Reflection.getMinecraft1_17ClassByName("world.entity.player.EntityHuman");
fieldTileEntitySign = Reflection.getField(CraftBlockEntityState, "tileEntity");
fieldTileEntitySignEditable = Reflection.getField(classTileEntitySign, "f");//isEditable new name
methodGetHandle = CraftPlayer.getDeclaredMethod("getHandle");
try {
fieldTileEntitySign = Reflection.getField(CraftSign, "sign");
} catch (NoSuchFieldException e) { // 1.12+
fieldTileEntitySign = Reflection.getField(CraftBlockEntityState, "tileEntity");
//1.18+
methodOpenSign = EntityHuman.getDeclaredMethod("a", classTileEntitySign);
//doesn't work because despite the name found in the jar, this may be an issue from Mojang with a bad
//mapping. The correct name is a and not openTextEdit.
} catch (Exception e) {
methodOpenSign = EntityHuman.getDeclaredMethod("openSign", classTileEntitySign);
}

} catch (Exception ex) {
try {
fieldTileEntitySignEditable = Reflection.getField(classTileEntitySign, "isEditable");
} catch (NoSuchFieldException e) { // 1.11.2 or below
fieldTileEntitySignEditable = null;
}
final Class<?> CraftBlockEntityState =
Reflection.getBukkitClassByName("block.CraftBlockEntityState");
final Class<?> CraftSign = Reflection.getBukkitClassByName("block.CraftSign");
final Class<?> classTileEntitySign = Reflection.getMinecraftClassByName("TileEntitySign");
final Class<?> CraftPlayer = Reflection.getBukkitClassByName("entity.CraftPlayer");
final Class<?> EntityHuman = Reflection.getMinecraftClassByName("EntityHuman");

try {
fieldTileEntitySign = Reflection.getField(CraftSign, "sign");
} catch (NoSuchFieldException exc) { // 1.12+
fieldTileEntitySign = Reflection.getField(CraftBlockEntityState, "tileEntity");
}

methodGetHandle = CraftPlayer.getDeclaredMethod("getHandle");
methodOpenSign = EntityHuman.getDeclaredMethod("openSign", classTileEntitySign);
} catch (Exception e) {
PluginLogger.error("Unable to initialize Sign Prompt API", e);
fieldTileEntitySign = null;
try {
fieldTileEntitySignEditable = Reflection.getField(classTileEntitySign, "isEditable");
} catch (NoSuchFieldException exc) { // 1.11.2 or below
fieldTileEntitySignEditable = null;
}

methodGetHandle = CraftPlayer.getDeclaredMethod("getHandle");
methodOpenSign = EntityHuman.getDeclaredMethod("openSign", classTileEntitySign);
} catch (Exception exc) {
PluginLogger.error("Unable to initialize Sign Prompt API", exc);
fieldTileEntitySign = null;
}
}
}


private static String getSignContents(String[] lines) {
StringBuilder content = new StringBuilder(lines[0].trim());

Expand Down Expand Up @@ -236,6 +264,7 @@ protected void open(final Player player) {

RunTask.later(() -> {
try {

final Object signTileEntity = fieldTileEntitySign.get(sign);
final Object playerEntity = methodGetHandle.invoke(player);

Expand All @@ -246,6 +275,7 @@ protected void open(final Player player) {
}

methodOpenSign.invoke(playerEntity, signTileEntity);

} catch (final Throwable e) {
PluginLogger.error("Error while opening Sign prompt", e);
}
Expand Down
76 changes: 54 additions & 22 deletions src/main/java/fr/zcraft/quartzlib/components/nbt/NBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

package fr.zcraft.quartzlib.components.nbt;

import fr.zcraft.quartzlib.tools.PluginLogger;
import fr.zcraft.quartzlib.tools.items.ItemUtils;
import fr.zcraft.quartzlib.tools.reflection.NMSException;
import fr.zcraft.quartzlib.tools.reflection.Reflection;
Expand Down Expand Up @@ -176,10 +177,10 @@ public static byte fromItemFlags(Set<ItemFlag> itemFlags) {
* @param item The ItemStack to change.
* @param tags The tags to place inside the stack.
* @return An item stack with the modification applied. It may (if you given
* a CraftItemStack) or may not (else) be the same instance as the given one.
* a CraftItemStack) or may not (else) be the same instance as the given one.
* @throws NMSException if the operation cannot be executed.
* @see #addToItemStack(ItemStack, Map, boolean) This method is equivalent
* to this one with replace = true.
* to this one with replace = true.
*/
public static ItemStack addToItemStack(ItemStack item, Map<String, Object> tags) throws NMSException {
return addToItemStack(item, tags, true);
Expand All @@ -197,7 +198,7 @@ public static ItemStack addToItemStack(ItemStack item, Map<String, Object> tags)
* @param replace {@code true} to replace the whole set of tags. If {@code
* false}, tags will be added.
* @return An item stack with the modification applied. It may (if you given
* a CraftItemStack) or may not (else) be the same instance as the given one.
* a CraftItemStack) or may not (else) be the same instance as the given one.
* @throws NMSException if the operation cannot be executed.
*/
public static ItemStack addToItemStack(final ItemStack item, final Map<String, Object> tags, final boolean replace)
Expand Down Expand Up @@ -241,11 +242,23 @@ public static ItemStack addToItemStack(final ItemStack item, final Map<String, O
}
}

/**
* Older version, the new addition of prefixes has been made mandatory by 1.17
*/
static Class<?> getMinecraftClass(String className) throws NMSException {
return getMinecraftClass("", className);
}

static Class<?> getMinecraftClass(String prefix, String className) throws NMSException {
try {
return Reflection.getMinecraftClassByName(className);
return Reflection
.getMinecraft1_17ClassByName(prefix.equals("") ? className : prefix + "." + className); //1.17+
} catch (ClassNotFoundException ex) {
throw new NMSException("Unable to find class: " + className, ex);
try {
return Reflection.getMinecraftClassByName(className);//Legacy for older version than 1.17
} catch (ClassNotFoundException e) {
throw new NMSException("Unable to find class: " + prefix + className, e);
}
}
}

Expand All @@ -262,8 +275,8 @@ private static void init() throws NMSException {
return; // Already initialized
}

MC_ITEM_STACK = getMinecraftClass("ItemStack");
MC_NBT_TAG_COMPOUND = getMinecraftClass("NBTTagCompound");
MC_ITEM_STACK = getMinecraftClass("world.item", "ItemStack");
MC_NBT_TAG_COMPOUND = getMinecraftClass("nbt", "NBTTagCompound");
CB_CRAFT_ITEM_META = getCraftBukkitClass("inventory.CraftMetaItem");
}

Expand All @@ -279,29 +292,49 @@ private static void init() throws NMSException {
* @throws NMSException If something goes wrong while extracting the tag.
*/
private static Object getMcNBTCompound(ItemStack item) throws NMSException {

Object mcItemStack = ItemUtils.getNMSItemStack(item);
if (mcItemStack == null) {
return null;
}

try {
Object tag = Reflection.getFieldValue(MC_ITEM_STACK, mcItemStack, "tag");

if (tag == null) {
tag = Reflection.instantiate(MC_NBT_TAG_COMPOUND);
Object tagCompound;
try {
//1.18
tagCompound = Reflection.call(mcItemStack.getClass(), mcItemStack, "t");
} catch (Exception e) {
//1.17
tagCompound = Reflection.call(mcItemStack.getClass(), mcItemStack, "a");
}

try {
Reflection.call(MC_ITEM_STACK, mcItemStack, "setTag", tag);
} catch (NoSuchMethodException e) {
// If the set method change—more resilient,
// as the setTag will only update the field without any kind of callback.
Reflection.setFieldValue(MC_ITEM_STACK, mcItemStack, "tag", tag);
}
if (tagCompound == null) {
tagCompound = Reflection.instantiate(MC_NBT_TAG_COMPOUND);
Reflection.call(MC_ITEM_STACK, mcItemStack, "setTag", tagCompound);
}
return tagCompound;

} catch (Exception exc) {
//Older method
try {
Object tag = Reflection.getFieldValue(MC_ITEM_STACK, mcItemStack, "tag");

if (tag == null) {
tag = Reflection.instantiate(MC_NBT_TAG_COMPOUND);

try {
Reflection.call(MC_ITEM_STACK, mcItemStack, "setTag", tag);
} catch (NoSuchMethodException e) {
// If the set method change—more resilient,
// as the setTag will only update the field without any kind of callback.
Reflection.setFieldValue(MC_ITEM_STACK, mcItemStack, "tag", tag);
}
}

return tag;
} catch (Exception ex) {
throw new NMSException("Unable to retrieve NBT tag from item", ex);
return tag;
} catch (Exception ex) {
throw new NMSException("Unable to retrieve NBT tag from item", ex);
}
}
}

Expand All @@ -316,7 +349,6 @@ static Object fromNativeValue(Object value) {
if (value == null) {
return null;
}

NBTType type = NBTType.fromClass(value.getClass());
return type.newTag(value);
}
Expand Down
Loading

0 comments on commit 13963da

Please sign in to comment.