Skip to content

Commit a0edc4d

Browse files
committed
fix: 1.20.6 support and Paper support
1 parent 8756644 commit a0edc4d

File tree

3 files changed

+97
-61
lines changed

3 files changed

+97
-61
lines changed

modules/SilkSpawners/src/main/java/de/dustplanet/silkspawners/SilkSpawners.java

+31-4
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,21 @@
55
import java.io.IOException;
66
import java.io.InputStream;
77
import java.io.OutputStream;
8+
import java.lang.reflect.InvocationTargetException;
89
import java.util.Arrays;
910
import java.util.HashMap;
1011
import java.util.List;
1112
import java.util.Locale;
13+
import java.util.Map;
1214
import java.util.logging.Level;
1315

1416
import org.apache.commons.lang.StringUtils;
1517
import org.bstats.bukkit.Metrics;
18+
import org.bukkit.Bukkit;
1619
import org.bukkit.Material;
1720
import org.bukkit.NamespacedKey;
21+
import org.bukkit.Server;
22+
import org.bukkit.UnsafeValues;
1823
import org.bukkit.configuration.file.FileConfiguration;
1924
import org.bukkit.entity.Player;
2025
import org.bukkit.inventory.ItemStack;
@@ -24,6 +29,8 @@
2429
import org.bukkit.plugin.Plugin;
2530
import org.bukkit.plugin.java.JavaPlugin;
2631

32+
import com.vdurmont.semver4j.Semver;
33+
2734
import de.dustplanet.silkspawners.commands.SilkSpawnersTabCompleter;
2835
import de.dustplanet.silkspawners.commands.SpawnerCommand;
2936
import de.dustplanet.silkspawners.configs.Config;
@@ -56,6 +63,12 @@ public class SilkSpawners extends JavaPlugin {
5663
private static final String[] COMPATIBLE_MINECRAFT_VERSIONS = { "v1_8_R3", "v1_11_R1", "v1_12_R1", "v1_13_R2", "v1_14_R1", "v1_15_R1",
5764
"v1_16_R1", "v1_16_R2", "v1_16_R3", "v1_17_R1", "v1_18_R1", "v1_18_R2", "v1_19_R1", "v1_19_R2", "v1_19_R3", "v1_20_R1",
5865
"v1_20_R2", "v1_20_R3", "v1_20_R4" };
66+
public static final Map<Integer, String> PROTOCOL_VERSION_PACKAGE_MAP = new HashMap<Integer, String>() {
67+
private static final long serialVersionUID = -5188779509588704507L;
68+
{
69+
put(766, "v1_20_R4");
70+
}
71+
};
5972
public CommentedConfiguration config;
6073
public CommentedConfiguration localization;
6174
@Getter
@@ -78,7 +91,21 @@ public void onEnable() {
7891
final String packageName = getServer().getClass().getPackage().getName();
7992
// org.bukkit.craftbukkit.version
8093
// Get the last element of the package
81-
setNmsVersion(packageName.substring(packageName.lastIndexOf('.') + 1));
94+
String _nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
95+
if (_nmsVersion.equals("craftbukkit")) {
96+
try {
97+
String minecraftVersion = (String) Server.class.getDeclaredMethod("getMinecraftVersion").invoke(Bukkit.getServer());
98+
Semver semver = new Semver(minecraftVersion);
99+
if (semver.isGreaterThanOrEqualTo("1.20.5")) {
100+
@SuppressWarnings("deprecation")
101+
int protocolVersion = (Integer) UnsafeValues.class.getDeclaredMethod("getProtocolVersion").invoke(Bukkit.getUnsafe());
102+
_nmsVersion = PROTOCOL_VERSION_PACKAGE_MAP.get(protocolVersion);
103+
}
104+
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
105+
e.printStackTrace();
106+
}
107+
}
108+
setNmsVersion(_nmsVersion);
82109

83110
// Test for right Minecraft version
84111
if (config.getBoolean("testMCVersion", true)) {
@@ -403,7 +430,7 @@ private void loadRecipes() {
403430
// If the custom recipe fails, we have a fallback
404431
getLogger().log(Level.WARNING, "Could not add the recipe of {0}!", entityID);
405432
getLogger().log(Level.WARNING, "Error:", e);
406-
recipe.shape(new String[] { "AAA", "ABA", "AAA" });
433+
recipe.shape("AAA", "ABA", "AAA");
407434
recipe.setIngredient('A', su.nmsProvider.getIronFenceMaterial());
408435
if (legacySpawnEggs) {
409436
recipe.setIngredient('X', su.nmsProvider.getSpawnEggMaterial(), 0);
@@ -496,7 +523,7 @@ private void loadBaseEggRecipe() {
496523
} catch (final IllegalArgumentException e) {
497524
// If the custom recipe fails, we have a fallback
498525
getLogger().log(Level.WARNING, "Could not add the default recipe!", e);
499-
baseSpawnerRecipe.shape(new String[] { "AAA", "ABA", "AAA" });
526+
baseSpawnerRecipe.shape("AAA", "ABA", "AAA");
500527
baseSpawnerRecipe.setIngredient('A', su.nmsProvider.getIronFenceMaterial());
501528
// Use the right egg!
502529
baseSpawnerRecipe.setIngredient('B', su.nmsProvider.getSpawnEggMaterial());
@@ -529,7 +556,7 @@ private boolean shapeContainsIngredient(final List<String> shape, final char c)
529556
/**
530557
* Sends a message to the player if the 'silkspawners.info' permission is granted. Empty messages are ignored and not are not sent.
531558
*
532-
* @param player the player to message
559+
* @param player the player to message
533560
* @param message the message to send
534561
*/
535562
public void informPlayer(final Player player, final String message) {

modules/SilkSpawners/src/main/java/de/dustplanet/util/SilkUtil.java

+38-17
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import org.bukkit.ChatColor;
2121
import org.bukkit.Location;
2222
import org.bukkit.Material;
23+
import org.bukkit.Server;
24+
import org.bukkit.UnsafeValues;
2325
import org.bukkit.block.Block;
2426
import org.bukkit.block.BlockState;
2527
import org.bukkit.block.CreatureSpawner;
@@ -41,6 +43,7 @@
4143
import com.sk89q.worldguard.protection.flags.Flags;
4244
import com.sk89q.worldguard.protection.regions.RegionContainer;
4345
import com.sk89q.worldguard.protection.regions.RegionQuery;
46+
import com.vdurmont.semver4j.Semver;
4447

4548
import de.dustplanet.silkspawners.SilkSpawners;
4649
import de.dustplanet.silkspawners.compat.api.NMSProvider;
@@ -160,7 +163,21 @@ private boolean setupNMSProvider() {
160163
// Rare cases might trigger API usage before SilkSpawners
161164
if (version == null) {
162165
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
163-
version = (packageName.substring(packageName.lastIndexOf('.') + 1));
166+
String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
167+
if (nmsVersion.equals("craftbukkit")) {
168+
try {
169+
String minecraftVersion = (String) Server.class.getDeclaredMethod("getMinecraftVersion").invoke(Bukkit.getServer());
170+
Semver semver = new Semver(minecraftVersion);
171+
if (semver.isGreaterThanOrEqualTo("1.20.5")) {
172+
@SuppressWarnings("deprecation")
173+
int protocolVersion = (Integer) UnsafeValues.class.getDeclaredMethod("getProtocolVersion")
174+
.invoke(Bukkit.getUnsafe());
175+
version = SilkSpawners.PROTOCOL_VERSION_PACKAGE_MAP.get(protocolVersion);
176+
}
177+
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
178+
e.printStackTrace();
179+
}
180+
}
164181
}
165182

166183
final FileConfiguration config = plugin.getConfig();
@@ -320,7 +337,7 @@ public boolean isVanillaBossBar() {
320337
*
321338
* @deprecated Use {@link SilkUtil#newEggItem(String, int, String)} instead.
322339
* @param entityID which mob should be spawned
323-
* @param amount the amount of spawn eggs
340+
* @param amount the amount of spawn eggs
324341
* @return the ItemStack
325342
*/
326343
@Deprecated
@@ -331,8 +348,8 @@ public ItemStack newEggItem(final String entityID, final int amount) {
331348
/**
332349
* Returns a new ItemStack of a spawn egg with the specified amount and mob.
333350
*
334-
* @param entityID which mob should be spawned
335-
* @param amount the amount of spawn eggs
351+
* @param entityID which mob should be spawned
352+
* @param amount the amount of spawn eggs
336353
* @param displayName the display name of the egg in case of unknown entities
337354
* @return the ItemStack
338355
*/
@@ -344,10 +361,10 @@ public ItemStack newEggItem(final String entityID, final int amount, final Strin
344361
/**
345362
* This method will make a new MobSpawner with a custom entityID, name and amount.
346363
*
347-
* @param entityID the mob
364+
* @param entityID the mob
348365
* @param customName if the MobSpawner should be named different
349-
* @param amount the wanted amount
350-
* @param forceLore whether the lore tag should be forces
366+
* @param amount the wanted amount
367+
* @param forceLore whether the lore tag should be forces
351368
* @return the ItemStack with the configured options
352369
*/
353370
public ItemStack newSpawnerItem(final String entityID, final String customName, final int amount, final boolean forceLore) {
@@ -395,10 +412,12 @@ public String getStoredEggEntityID(final ItemStack item) {
395412
if (isUsingReflection()) {
396413
// Now try reflection for NBT tag
397414
entityID = nmsProvider.getSilkSpawnersNBTEntityID(item);
415+
plugin.getLogger().log(Level.FINE, "EntityID from egg item stack (custom tag) is {0}", entityID);
398416
if (entityID != null) {
399417
return entityID;
400418
}
401419
entityID = nmsProvider.getVanillaEggNBTEntityID(item);
420+
plugin.getLogger().log(Level.FINE, "EntityID from egg item stack (vanilla tag) is {0}", entityID);
402421
if (entityID != null) {
403422
return entityID;
404423
}
@@ -423,10 +442,12 @@ public String getStoredEggEntityID(final ItemStack item) {
423442
public String getStoredSpawnerItemEntityID(final ItemStack item) {
424443
if (isUsingReflection()) {
425444
String entityID = nmsProvider.getSilkSpawnersNBTEntityID(item);
445+
plugin.getLogger().log(Level.FINE, "EntityID from item stack (custom tag) is {0}", entityID);
426446
if (StringUtils.isNotBlank(entityID)) {
427447
return entityID;
428448
}
429449
entityID = nmsProvider.getVanillaNBTEntityID(item);
450+
plugin.getLogger().log(Level.FINE, "EntityID from item stack (vanilla tag) is {0}", entityID);
430451
if (StringUtils.isNotBlank(entityID)) {
431452
return entityID.replace("minecraft:", "");
432453
}
@@ -505,7 +526,7 @@ public String getSpawnerEntityID(final Block block) {
505526
/**
506527
* Set the specified MonterSpawner to another entity ID.
507528
*
508-
* @param block MonsterSpawner
529+
* @param block MonsterSpawner
509530
* @param entity the wanted entity
510531
*/
511532
public void setSpawnerEntityID(final Block block, final String entity) {
@@ -539,9 +560,9 @@ public void setSpawnerEntityID(final Block block, final String entity) {
539560
/**
540561
* Set a spawner (if allowed) to a new mob.
541562
*
542-
* @param block the MonsterSpawner
543-
* @param entityID the new entity ID
544-
* @param player the player
563+
* @param block the MonsterSpawner
564+
* @param entityID the new entity ID
565+
* @param player the player
545566
* @param messageDenied the message which is shown, when the player can't build here see {@link #canBuildHere(Player, Location)}
546567
* @return whether the operation was successful or not
547568
*/
@@ -559,8 +580,8 @@ public boolean setSpawnerType(final Block block, final String entityID, final Pl
559580
/**
560581
* Sets a spawner item or egg to a new ID.
561582
*
562-
* @param item ItemStack (Egg or Spawner)
563-
* @param entityID wanted entity ID
583+
* @param item ItemStack (Egg or Spawner)
584+
* @param entityID wanted entity ID
564585
* @param customName if a custom name should be used (null for none)
565586
* @return the updated ItemStack
566587
*/
@@ -706,7 +727,7 @@ public List<String> scanEntityMap() {
706727
/**
707728
* Notify a player about the spawner.
708729
*
709-
* @param player the player
730+
* @param player the player
710731
* @param spawnerName the creature name
711732
*/
712733
@SuppressWarnings("deprecation")
@@ -892,7 +913,7 @@ private void getWorldGuard() {
892913
/**
893914
* Checks if a player can build here (WorldGuard).
894915
*
895-
* @param player the player
916+
* @param player the player
896917
* @param location the location to check
897918
* @return the result, true or false
898919
*/
@@ -934,9 +955,9 @@ public boolean isLegacySpawnEggs() {
934955
/**
935956
* Helper methods to check if a player has any of the aliases permissions for a given mobID.
936957
*
937-
* @param permissible - the permissible to check the permission for
958+
* @param permissible - the permissible to check the permission for
938959
* @param basePermission - the basis permission without the specific mob
939-
* @param entityID - the internal mob ID (not display name)
960+
* @param entityID - the internal mob ID (not display name)
940961
* @return the permission check result, true if the player has got the permission, false otherwise
941962
*/
942963
public boolean hasPermission(final Permissible permissible, final String basePermission, final String entityID) {

modules/v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4/NMSHandler.java

+28-40
Original file line numberDiff line numberDiff line change
@@ -231,21 +231,16 @@ public ItemStack setNBTEntityID(final ItemStack item, final String entity) {
231231
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
232232
itemStack = CraftItemStack.asNMSCopy(craftStack);
233233
final CustomData blockData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
234+
final CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
234235
CompoundTag tag = blockData.copyTag();
236+
CompoundTag customTag = customData.copyTag();
235237

236238
// Check for SilkSpawners key
237-
if (!tag.contains("SilkSpawners")) {
238-
tag.put("SilkSpawners", new CompoundTag());
239-
}
240-
241-
tag.getCompound("SilkSpawners").putString("entity", entity);
242-
243-
// Check for Vanilla keys
244-
if (!tag.contains("BlockEntityTag")) {
245-
tag.put("BlockEntityTag", new CompoundTag());
239+
if (!customTag.contains("SilkSpawners")) {
240+
customTag.put("SilkSpawners", new CompoundTag());
246241
}
247242

248-
tag = tag.getCompound("BlockEntityTag");
243+
customTag.getCompound("SilkSpawners").putString("entity", entity);
249244

250245
// EntityId - Deprecated in 1.9
251246
tag.putString("EntityId", entity);
@@ -273,6 +268,7 @@ public ItemStack setNBTEntityID(final ItemStack item, final String entity) {
273268
tag.getCompound("EntityTag").putString("id", prefixedEntity);
274269

275270
itemStack.set(DataComponents.BLOCK_ENTITY_DATA, CustomData.of(tag));
271+
itemStack.set(DataComponents.CUSTOM_DATA, CustomData.of(customTag));
276272
return CraftItemStack.asCraftMirror(itemStack);
277273
}
278274

@@ -282,10 +278,10 @@ public String getSilkSpawnersNBTEntityID(final ItemStack item) {
282278
net.minecraft.world.item.ItemStack itemStack = null;
283279
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
284280
itemStack = CraftItemStack.asNMSCopy(craftStack);
285-
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
281+
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
286282
final CompoundTag tag = blockEntityData.copyTag();
287283

288-
if (tag == null || !tag.contains("SilkSpawners")) {
284+
if (!tag.contains("SilkSpawners")) {
289285
return null;
290286
}
291287
return tag.getCompound("SilkSpawners").getString("entity");
@@ -300,11 +296,6 @@ public String getVanillaNBTEntityID(final ItemStack item) {
300296
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
301297
CompoundTag tag = blockEntityData.copyTag();
302298

303-
if (tag == null || !tag.contains("BlockEntityTag")) {
304-
return null;
305-
}
306-
307-
tag = tag.getCompound("BlockEntityTag");
308299
if (tag.contains("EntityId")) {
309300
return tag.getString("EntityId");
310301
} else if (tag.contains("SpawnData") && tag.getCompound("SpawnData").contains("id")) {
@@ -352,28 +343,27 @@ public ItemStack newEggItem(final String entityID, final int amount, final Strin
352343
net.minecraft.world.item.ItemStack itemStack = null;
353344
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
354345
itemStack = CraftItemStack.asNMSCopy(craftStack);
355-
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
356-
final CompoundTag tag = blockEntityData.copyTag();
346+
final CustomData blockData = itemStack.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY);
347+
final CustomData customData = itemStack.getOrDefault(DataComponents.CUSTOM_DATA, CustomData.EMPTY);
348+
CompoundTag tag = blockData.copyTag();
349+
CompoundTag customTag = customData.copyTag();
357350

358-
if (!tag.contains("SilkSpawners")) {
359-
tag.put("SilkSpawners", new CompoundTag());
351+
if (!customTag.contains("SilkSpawners")) {
352+
customTag.put("SilkSpawners", new CompoundTag());
360353
}
361354

362-
tag.getCompound("SilkSpawners").putString("entity", entityID);
363-
364-
if (!tag.contains("EntityTag")) {
365-
tag.put("EntityTag", new CompoundTag());
366-
}
355+
customTag.getCompound("SilkSpawners").putString("entity", entityID);
367356

368357
String prefixedEntity;
369358
if (!entityID.startsWith("minecraft:")) {
370359
prefixedEntity = "minecraft:" + entityID;
371360
} else {
372361
prefixedEntity = entityID;
373362
}
374-
tag.getCompound("EntityTag").putString("id", prefixedEntity);
363+
tag.putString("id", prefixedEntity);
375364

376-
itemStack.set(DataComponents.BLOCK_ENTITY_DATA, CustomData.of(tag));
365+
itemStack.set(DataComponents.ENTITY_DATA, CustomData.of(tag));
366+
itemStack.set(DataComponents.CUSTOM_DATA, CustomData.of(customTag));
377367
return CraftItemStack.asCraftMirror(itemStack);
378368
}
379369

@@ -382,21 +372,19 @@ public String getVanillaEggNBTEntityID(final ItemStack item) {
382372
net.minecraft.world.item.ItemStack itemStack = null;
383373
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
384374
itemStack = CraftItemStack.asNMSCopy(craftStack);
385-
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.BLOCK_ENTITY_DATA, CustomData.EMPTY);
375+
final CustomData blockEntityData = itemStack.getOrDefault(DataComponents.ENTITY_DATA, CustomData.EMPTY);
386376
CompoundTag tag = blockEntityData.copyTag();
387377

388-
if (tag == null || !tag.contains("EntityTag")) {
389-
final Registry<Item> itemRegistry = BuiltInRegistries.ITEM;
390-
final ResourceLocation vanillaKey = itemRegistry.getKey(itemStack.getItem());
391-
if (vanillaKey != null) {
392-
return vanillaKey.getPath().replace("minecraft:", "").replace("_spawn_egg", "");
393-
}
394-
} else {
395-
tag = tag.getCompound("EntityTag");
396-
if (tag.contains("id")) {
397-
return tag.getString("id").replace("minecraft:", "");
398-
}
378+
if (tag.contains("id")) {
379+
return tag.getString("id").replace("minecraft:", "");
380+
}
381+
382+
final Registry<Item> itemRegistry = BuiltInRegistries.ITEM;
383+
final ResourceLocation vanillaKey = itemRegistry.getKey(itemStack.getItem());
384+
if (vanillaKey != null) {
385+
return vanillaKey.getPath().replace("minecraft:", "").replace("_spawn_egg", "");
399386
}
387+
400388
return null;
401389
}
402390

0 commit comments

Comments
 (0)