Skip to content

Commit 13963da

Browse files
VlammarVlammarAmauryCarrade
authored
Java 17 and minecraft 1.17 support (#80)
* 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 <[email protected]> * Done changed according to the reviews made by amauryPi * removed unused tileentitysign * removed origin file Co-authored-by: Vlammar <[email protected]> Co-authored-by: Amaury Carrade <[email protected]>
1 parent 126db0c commit 13963da

File tree

11 files changed

+538
-217
lines changed

11 files changed

+538
-217
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
java: [8, 9, 10, 11, 12, 13, 14, 15]
17+
java: [8, 11, 16, 17]
1818
steps:
1919
- uses: actions/checkout@v2
2020
- name: Set up JDK 1.8

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
<groupId>fr.zcraft</groupId>
3939
<artifactId>quartz</artifactId>
40-
<version>0.0.1</version>
40+
<version>0.0.5</version>
4141

4242
<packaging>pom</packaging>
4343

@@ -50,6 +50,6 @@
5050

5151
<modules>
5252
<module>quartzlib</module>
53-
<module>ztoaster</module>
53+
5454
</modules>
5555
</project>

quartzlib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<maven.compiler.source>1.8</maven.compiler.source>
1616
<maven.compiler.target>1.8</maven.compiler.target>
1717
<junit.version>4.13.1</junit.version>
18-
<revision>0.0.1-SNAPSHOT</revision>
18+
<revision>0.0.5-SNAPSHOT</revision>
1919
</properties>
2020

2121
<build>

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

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,38 @@ public class PromptGui extends GuiBase {
6363
private Location signLocation;
6464
private String contents;
6565

66+
6667
public PromptGui(Callback<String> callback, String contents) {
6768
this(callback);
6869
this.contents = contents;
6970
}
7071

7172
/**
7273
* Creates a new prompt GUI, using the given callback.
74+
*
7375
* @param callback The callback to be given the input text to.
7476
*/
7577
public PromptGui(Callback<String> callback) {
7678
super();
79+
7780
if (!isAvailable()) {
7881
throw new IllegalStateException("Sign-based prompt GUI are not available");
7982
}
8083

8184
this.callback = callback;
8285
}
8386

87+
8488
/**
8589
* Checks if Prompt GUIs can be correctly used on this Minecraft versions.
8690
*/
8791
public static boolean isAvailable() {
92+
8893
if (!isInitialized) {
8994
init();
9095
}
9196
return fieldTileEntitySign != null;
97+
9298
}
9399

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

105111
try {
106-
final Class<?> CraftBlockEntityState = Reflection.getBukkitClassByName("block.CraftBlockEntityState");
107-
final Class<?> CraftSign = Reflection.getBukkitClassByName("block.CraftSign");
108-
final Class<?> classTileEntitySign = Reflection.getMinecraftClassByName("TileEntitySign");
112+
final Class<?> CraftBlockEntityState =
113+
Reflection.getBukkitClassByName("block.CraftBlockEntityState");
114+
final Class<?> classTileEntitySign
115+
= Reflection.getMinecraft1_17ClassByName("world.level.block.entity.TileEntitySign");
109116
final Class<?> CraftPlayer = Reflection.getBukkitClassByName("entity.CraftPlayer");
110-
final Class<?> EntityHuman = Reflection.getMinecraftClassByName("EntityHuman");
111-
117+
final Class<?> EntityHuman = Reflection.getMinecraft1_17ClassByName("world.entity.player.EntityHuman");
118+
fieldTileEntitySign = Reflection.getField(CraftBlockEntityState, "tileEntity");
119+
fieldTileEntitySignEditable = Reflection.getField(classTileEntitySign, "f");//isEditable new name
120+
methodGetHandle = CraftPlayer.getDeclaredMethod("getHandle");
112121
try {
113-
fieldTileEntitySign = Reflection.getField(CraftSign, "sign");
114-
} catch (NoSuchFieldException e) { // 1.12+
115-
fieldTileEntitySign = Reflection.getField(CraftBlockEntityState, "tileEntity");
122+
//1.18+
123+
methodOpenSign = EntityHuman.getDeclaredMethod("a", classTileEntitySign);
124+
//doesn't work because despite the name found in the jar, this may be an issue from Mojang with a bad
125+
//mapping. The correct name is a and not openTextEdit.
126+
} catch (Exception e) {
127+
methodOpenSign = EntityHuman.getDeclaredMethod("openSign", classTileEntitySign);
116128
}
117-
129+
} catch (Exception ex) {
118130
try {
119-
fieldTileEntitySignEditable = Reflection.getField(classTileEntitySign, "isEditable");
120-
} catch (NoSuchFieldException e) { // 1.11.2 or below
121-
fieldTileEntitySignEditable = null;
122-
}
131+
final Class<?> CraftBlockEntityState =
132+
Reflection.getBukkitClassByName("block.CraftBlockEntityState");
133+
final Class<?> CraftSign = Reflection.getBukkitClassByName("block.CraftSign");
134+
final Class<?> classTileEntitySign = Reflection.getMinecraftClassByName("TileEntitySign");
135+
final Class<?> CraftPlayer = Reflection.getBukkitClassByName("entity.CraftPlayer");
136+
final Class<?> EntityHuman = Reflection.getMinecraftClassByName("EntityHuman");
137+
138+
try {
139+
fieldTileEntitySign = Reflection.getField(CraftSign, "sign");
140+
} catch (NoSuchFieldException exc) { // 1.12+
141+
fieldTileEntitySign = Reflection.getField(CraftBlockEntityState, "tileEntity");
142+
}
123143

124-
methodGetHandle = CraftPlayer.getDeclaredMethod("getHandle");
125-
methodOpenSign = EntityHuman.getDeclaredMethod("openSign", classTileEntitySign);
126-
} catch (Exception e) {
127-
PluginLogger.error("Unable to initialize Sign Prompt API", e);
128-
fieldTileEntitySign = null;
144+
try {
145+
fieldTileEntitySignEditable = Reflection.getField(classTileEntitySign, "isEditable");
146+
} catch (NoSuchFieldException exc) { // 1.11.2 or below
147+
fieldTileEntitySignEditable = null;
148+
}
149+
150+
methodGetHandle = CraftPlayer.getDeclaredMethod("getHandle");
151+
methodOpenSign = EntityHuman.getDeclaredMethod("openSign", classTileEntitySign);
152+
} catch (Exception exc) {
153+
PluginLogger.error("Unable to initialize Sign Prompt API", exc);
154+
fieldTileEntitySign = null;
155+
}
129156
}
130157
}
131158

159+
132160
private static String getSignContents(String[] lines) {
133161
StringBuilder content = new StringBuilder(lines[0].trim());
134162

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

237265
RunTask.later(() -> {
238266
try {
267+
239268
final Object signTileEntity = fieldTileEntitySign.get(sign);
240269
final Object playerEntity = methodGetHandle.invoke(player);
241270

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

248277
methodOpenSign.invoke(playerEntity, signTileEntity);
278+
249279
} catch (final Throwable e) {
250280
PluginLogger.error("Error while opening Sign prompt", e);
251281
}

src/main/java/fr/zcraft/quartzlib/components/nbt/NBT.java

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
package fr.zcraft.quartzlib.components.nbt;
3232

33+
import fr.zcraft.quartzlib.tools.PluginLogger;
3334
import fr.zcraft.quartzlib.tools.items.ItemUtils;
3435
import fr.zcraft.quartzlib.tools.reflection.NMSException;
3536
import fr.zcraft.quartzlib.tools.reflection.Reflection;
@@ -176,10 +177,10 @@ public static byte fromItemFlags(Set<ItemFlag> itemFlags) {
176177
* @param item The ItemStack to change.
177178
* @param tags The tags to place inside the stack.
178179
* @return An item stack with the modification applied. It may (if you given
179-
* a CraftItemStack) or may not (else) be the same instance as the given one.
180+
* a CraftItemStack) or may not (else) be the same instance as the given one.
180181
* @throws NMSException if the operation cannot be executed.
181182
* @see #addToItemStack(ItemStack, Map, boolean) This method is equivalent
182-
* to this one with replace = true.
183+
* to this one with replace = true.
183184
*/
184185
public static ItemStack addToItemStack(ItemStack item, Map<String, Object> tags) throws NMSException {
185186
return addToItemStack(item, tags, true);
@@ -197,7 +198,7 @@ public static ItemStack addToItemStack(ItemStack item, Map<String, Object> tags)
197198
* @param replace {@code true} to replace the whole set of tags. If {@code
198199
* false}, tags will be added.
199200
* @return An item stack with the modification applied. It may (if you given
200-
* a CraftItemStack) or may not (else) be the same instance as the given one.
201+
* a CraftItemStack) or may not (else) be the same instance as the given one.
201202
* @throws NMSException if the operation cannot be executed.
202203
*/
203204
public static ItemStack addToItemStack(final ItemStack item, final Map<String, Object> tags, final boolean replace)
@@ -241,11 +242,23 @@ public static ItemStack addToItemStack(final ItemStack item, final Map<String, O
241242
}
242243
}
243244

245+
/**
246+
* Older version, the new addition of prefixes has been made mandatory by 1.17
247+
*/
244248
static Class<?> getMinecraftClass(String className) throws NMSException {
249+
return getMinecraftClass("", className);
250+
}
251+
252+
static Class<?> getMinecraftClass(String prefix, String className) throws NMSException {
245253
try {
246-
return Reflection.getMinecraftClassByName(className);
254+
return Reflection
255+
.getMinecraft1_17ClassByName(prefix.equals("") ? className : prefix + "." + className); //1.17+
247256
} catch (ClassNotFoundException ex) {
248-
throw new NMSException("Unable to find class: " + className, ex);
257+
try {
258+
return Reflection.getMinecraftClassByName(className);//Legacy for older version than 1.17
259+
} catch (ClassNotFoundException e) {
260+
throw new NMSException("Unable to find class: " + prefix + className, e);
261+
}
249262
}
250263
}
251264

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

265-
MC_ITEM_STACK = getMinecraftClass("ItemStack");
266-
MC_NBT_TAG_COMPOUND = getMinecraftClass("NBTTagCompound");
278+
MC_ITEM_STACK = getMinecraftClass("world.item", "ItemStack");
279+
MC_NBT_TAG_COMPOUND = getMinecraftClass("nbt", "NBTTagCompound");
267280
CB_CRAFT_ITEM_META = getCraftBukkitClass("inventory.CraftMetaItem");
268281
}
269282

@@ -279,29 +292,49 @@ private static void init() throws NMSException {
279292
* @throws NMSException If something goes wrong while extracting the tag.
280293
*/
281294
private static Object getMcNBTCompound(ItemStack item) throws NMSException {
295+
282296
Object mcItemStack = ItemUtils.getNMSItemStack(item);
283297
if (mcItemStack == null) {
284298
return null;
285299
}
286300

287301
try {
288-
Object tag = Reflection.getFieldValue(MC_ITEM_STACK, mcItemStack, "tag");
289-
290-
if (tag == null) {
291-
tag = Reflection.instantiate(MC_NBT_TAG_COMPOUND);
302+
Object tagCompound;
303+
try {
304+
//1.18
305+
tagCompound = Reflection.call(mcItemStack.getClass(), mcItemStack, "t");
306+
} catch (Exception e) {
307+
//1.17
308+
tagCompound = Reflection.call(mcItemStack.getClass(), mcItemStack, "a");
309+
}
292310

293-
try {
294-
Reflection.call(MC_ITEM_STACK, mcItemStack, "setTag", tag);
295-
} catch (NoSuchMethodException e) {
296-
// If the set method change—more resilient,
297-
// as the setTag will only update the field without any kind of callback.
298-
Reflection.setFieldValue(MC_ITEM_STACK, mcItemStack, "tag", tag);
299-
}
311+
if (tagCompound == null) {
312+
tagCompound = Reflection.instantiate(MC_NBT_TAG_COMPOUND);
313+
Reflection.call(MC_ITEM_STACK, mcItemStack, "setTag", tagCompound);
300314
}
315+
return tagCompound;
316+
317+
} catch (Exception exc) {
318+
//Older method
319+
try {
320+
Object tag = Reflection.getFieldValue(MC_ITEM_STACK, mcItemStack, "tag");
321+
322+
if (tag == null) {
323+
tag = Reflection.instantiate(MC_NBT_TAG_COMPOUND);
324+
325+
try {
326+
Reflection.call(MC_ITEM_STACK, mcItemStack, "setTag", tag);
327+
} catch (NoSuchMethodException e) {
328+
// If the set method change—more resilient,
329+
// as the setTag will only update the field without any kind of callback.
330+
Reflection.setFieldValue(MC_ITEM_STACK, mcItemStack, "tag", tag);
331+
}
332+
}
301333

302-
return tag;
303-
} catch (Exception ex) {
304-
throw new NMSException("Unable to retrieve NBT tag from item", ex);
334+
return tag;
335+
} catch (Exception ex) {
336+
throw new NMSException("Unable to retrieve NBT tag from item", ex);
337+
}
305338
}
306339
}
307340

@@ -316,7 +349,6 @@ static Object fromNativeValue(Object value) {
316349
if (value == null) {
317350
return null;
318351
}
319-
320352
NBTType type = NBTType.fromClass(value.getClass());
321353
return type.newTag(value);
322354
}

0 commit comments

Comments
 (0)