Skip to content

Commit e99a5ba

Browse files
committed
Fixed version selection for 1.21.3
1 parent e2ad781 commit e99a5ba

File tree

9 files changed

+70
-83
lines changed

9 files changed

+70
-83
lines changed

api/src/main/java/com/qualityplus/assistant/api/nms/NMS.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@
1818
* NMS Interface
1919
*/
2020
public interface NMS {
21-
/**
22-
* Set texture to item stack
23-
*
24-
* @param itemStack {@link ItemStack}
25-
* @param texture {@link String}
26-
* @return {@link ItemStack}
27-
*/
28-
public ItemStack setTexture(final ItemStack itemStack, final String texture);
2921
/**
3022
* Set block age
3123
*
@@ -205,6 +197,14 @@ public void sendParticles(final World world, final String particle,
205197
final float offsetY, final float offsetZ,
206198
final float data, final int amount);
207199

200+
/**
201+
*
202+
* @return if new nbt api resolver should be used
203+
*/
204+
public default boolean isNewNBTAPIResolver() {
205+
return false;
206+
}
207+
208208
/**
209209
* Ender Dragon Parts representation
210210
*/

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ plugins {
88
}
99

1010
group = "com.github.r3back"
11-
version = "3.2.24"
11+
version = "3.2.25"
1212

1313
ext.set("mojangUnmappedVersions", setOf("1_21_R2", "1_21_R1", "v1_20_R4", "v1_20_R3","v1_20_R2", "v1_20_R1", "v1_19_R1", "v1_19_R2", "v1_18_R1", "v1_18_R2", "v1_17_R1"))
1414

nms/nms-commons/src/main/java/com/qualityplus/assistant/base/nms/AbstractNMS.java

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.google.common.cache.Cache;
44
import com.google.common.cache.CacheBuilder;
5-
import com.mojang.authlib.GameProfile;
6-
import com.mojang.authlib.properties.Property;
75
import com.qualityplus.assistant.api.nms.NMS;
86
import com.qualityplus.assistant.api.util.CropUtil;
97
import com.qualityplus.assistant.base.event.ActionBarMessageEvent;
@@ -14,12 +12,6 @@
1412
import org.bukkit.boss.BossBar;
1513
import org.bukkit.entity.Player;
1614
import org.bukkit.event.Event;
17-
import org.bukkit.inventory.ItemStack;
18-
import org.bukkit.inventory.meta.SkullMeta;
19-
20-
import java.lang.reflect.Field;
21-
import java.lang.reflect.InvocationTargetException;
22-
import java.lang.reflect.Method;
2315
import java.util.HashMap;
2416
import java.util.Map;
2517
import java.util.UUID;
@@ -37,65 +29,6 @@ public abstract class AbstractNMS implements NMS {
3729
private final Map<UUID, Long> disabled = new HashMap<>();
3830
private final Map<UUID, Long> enabled = new HashMap<>();
3931
protected static BossBar bossBar;
40-
private static Method metaSetProfileMethod;
41-
private static Field metaProfileField;
42-
43-
/**
44-
*
45-
* @param itemStack {@link ItemStack}
46-
* @param texture {@link String}
47-
* @return {@link ItemStack}
48-
*/
49-
@Override
50-
public ItemStack setTexture(final ItemStack itemStack, final String texture) {
51-
try {
52-
final SkullMeta meta = (SkullMeta) itemStack.getItemMeta();
53-
54-
mutateItemMeta(meta, texture);
55-
itemStack.setItemMeta(meta);
56-
57-
return itemStack;
58-
} catch (final NullPointerException e) {
59-
e.printStackTrace();
60-
}
61-
62-
return itemStack;
63-
}
64-
65-
private void mutateItemMeta(final SkullMeta meta, final String b64) {
66-
try {
67-
if (metaSetProfileMethod == null) {
68-
metaSetProfileMethod = meta.getClass().getDeclaredMethod("setProfile", GameProfile.class);
69-
metaSetProfileMethod.setAccessible(true);
70-
}
71-
metaSetProfileMethod.invoke(meta, makeProfile(b64));
72-
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ex) {
73-
// if in an older API where there is no setProfile method,
74-
// we set the profile field directly.
75-
try {
76-
if (metaProfileField == null) {
77-
metaProfileField = meta.getClass().getDeclaredField("profile");
78-
metaProfileField.setAccessible(true);
79-
}
80-
metaProfileField.set(meta, makeProfile(b64));
81-
82-
} catch (NoSuchFieldException | IllegalAccessException ex2) {
83-
ex2.printStackTrace();
84-
}
85-
}
86-
}
87-
88-
89-
private static GameProfile makeProfile(final String b64) {
90-
// random uuid based on the b64 string
91-
final UUID id = new UUID(
92-
b64.substring(b64.length() - 20).hashCode(),
93-
b64.substring(b64.length() - 10).hashCode()
94-
);
95-
final GameProfile profile = new GameProfile(id, "Player");
96-
profile.getProperties().put("textures", new Property("textures", b64));
97-
return profile;
98-
}
9932

10033
@Override
10134
public void sendActionBar(final Player player, final String message) {

nms/v1_20_R3/src/main/java/com/qualityplus/assistant/base/nms/v1_20_R3.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
public final class v1_20_R3 extends AbstractNMS {
6161
private @Getter @Inject Plugin plugin;
6262

63+
@Override
64+
public boolean isNewNBTAPIResolver() {
65+
return true;
66+
}
67+
6368
@Override
6469
public void setBlockAge(final Block block, final int age) {
6570
if (block.getBlockData() instanceof Ageable) {

nms/v1_20_R4/src/main/java/com/qualityplus/assistant/base/nms/v1_20_R4.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
public final class v1_20_R4 extends AbstractNMS {
6161
private @Getter @Inject Plugin plugin;
6262

63+
@Override
64+
public boolean isNewNBTAPIResolver() {
65+
return true;
66+
}
67+
6368
@Override
6469
public void setBlockAge(final Block block, final int age) {
6570
if (block.getBlockData() instanceof Ageable) {

nms/v1_21_R1/src/main/java/com/qualityplus/assistant/base/nms/v1_21_R1.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@
6060
public final class v1_21_R1 extends AbstractNMS {
6161
private @Getter @Inject Plugin plugin;
6262

63+
@Override
64+
public boolean isNewNBTAPIResolver() {
65+
return true;
66+
}
67+
6368
@Override
6469
public void setBlockAge(final Block block, final int age) {
6570
if (block.getBlockData() instanceof Ageable) {

nms/v1_21_R2/src/main/java/com/qualityplus/assistant/base/nms/v1_21_R2.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import org.bukkit.plugin.Plugin;
4848
import org.jetbrains.annotations.NotNull;
4949

50+
import java.lang.reflect.Field;
51+
import java.lang.reflect.Method;
5052
import java.util.Collections;
5153
import java.util.List;
5254
import java.util.Objects;
@@ -59,6 +61,13 @@
5961
*/
6062
public final class v1_21_R2 extends AbstractNMS {
6163
private @Getter @Inject Plugin plugin;
64+
private static Method metaSetProfileMethod;
65+
private static Field metaProfileField;
66+
67+
@Override
68+
public boolean isNewNBTAPIResolver() {
69+
return true;
70+
}
6271

6372
@Override
6473
public void setBlockAge(final Block block, final int age) {

plugin/src/main/java/com/qualityplus/assistant/base/factory/NMSFactory.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public final class NMSFactory {
6363
.put("1.21.3", MinecraftVersion.V1_21_R2)
6464
.build();
6565

66+
private static final String RECOGNIZED_MC_VERSION_MESSAGE = "Successfully recognized MC Version %s mapped with version %s";
6667
private static final String RECOGNIZED_VERSION_MESSAGE = "Successfully recognized Version %s";
6768
private static final String UNSUPPORTED_VERSION_MESSAGE = "Unsupported Version %s";
6869
private static final String DISABLING_PLUGIN_MESSAGE = "Disabling Plugin...";
@@ -118,13 +119,18 @@ public TabHandler configureTab() {
118119
* @return {@link MinecraftVersion}
119120
*/
120121
private MinecraftVersion getMcVersion() {
121-
final String version = Bukkit.getServer().getVersion();
122+
final String[] versionSplit = Bukkit.getServer().getVersion().split("-");
122123

123-
for (final Map.Entry<String, MinecraftVersion> nmsEntry : NEW_NMS_VERSIONS.entrySet()) {
124-
if (!version.startsWith(nmsEntry.getKey())) {
125-
continue;
124+
if (versionSplit.length > 1) {
125+
for (final Map.Entry<String, MinecraftVersion> nmsEntry : NEW_NMS_VERSIONS.entrySet()) {
126+
final String version = versionSplit[0];
127+
if (!version.equals(nmsEntry.getKey())) {
128+
continue;
129+
}
130+
this.logger.info(String.format(RECOGNIZED_MC_VERSION_MESSAGE, version, nmsEntry.getValue()));
131+
132+
return nmsEntry.getValue();
126133
}
127-
return nmsEntry.getValue();
128134
}
129135

130136
final String nmsVersion = Bukkit.getServer().getClass()

plugin/src/main/java/com/qualityplus/assistant/util/itemstack/ItemStackUtils.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
import com.qualityplus.assistant.api.util.IPlaceholder;
88
import com.qualityplus.assistant.inventory.Item;
99
import com.qualityplus.assistant.util.StringUtils;
10+
import de.tr7zw.changeme.nbtapi.NBT;
11+
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
1012
import lombok.experimental.UtilityClass;
1113
import org.apache.commons.lang.WordUtils;
1214
import org.bukkit.enchantments.Enchantment;
@@ -20,6 +22,7 @@
2022
import java.util.Collections;
2123
import java.util.List;
2224
import java.util.Optional;
25+
import java.util.UUID;
2326
import java.util.stream.Collectors;
2427

2528
/**
@@ -356,12 +359,33 @@ public void parseWrappedLore(final ItemStack itemStack, final LoreWrapper loreWr
356359

357360
private ItemStack parseTexture(final ItemStack itemStack, final Item item) {
358361
try {
359-
return TheAssistantPlugin.getAPI().getNms().setTexture(itemStack, item.getHeadData());
362+
final String textureValue = item.getHeadData();
363+
final UUID uuid = UUID.randomUUID();
364+
if (TheAssistantPlugin.getAPI().getNms().isNewNBTAPIResolver()) {
365+
NBT.modifyComponents(itemStack, nbt -> {
366+
final ReadWriteNBT profileNbt = nbt.getOrCreateCompound("minecraft:profile");
367+
profileNbt.setUUID("id", uuid);
368+
final ReadWriteNBT propertiesNbt = profileNbt.getCompoundList("properties").addCompound();
369+
propertiesNbt.setString("name", "textures");
370+
propertiesNbt.setString("value", textureValue);
371+
});
372+
} else {
373+
NBT.modify(itemStack, nbt -> {
374+
final ReadWriteNBT skullOwnerCompound = nbt.getOrCreateCompound("SkullOwner");
375+
376+
skullOwnerCompound.setUUID("Id", UUID.randomUUID());
377+
378+
skullOwnerCompound.getOrCreateCompound("Properties")
379+
.getCompoundList("textures")
380+
.addCompound()
381+
.setString("Value", textureValue);
382+
});
383+
}
384+
return itemStack;
360385
} catch (final NullPointerException e) {
361386
e.printStackTrace();
362387
return itemStack;
363388
}
364-
365389
}
366390

367391
private void parseEnchantment(final ItemStack itemStack, final Item item) {

0 commit comments

Comments
 (0)