Skip to content

Commit 3d0e54f

Browse files
committed
feat: adds method to read other plugins NBT data (support for MineableSpawners added)
1 parent 50674c3 commit 3d0e54f

File tree

22 files changed

+375
-43
lines changed
  • modules
    • API/src/main/java/de/dustplanet/silkspawners/compat/api
    • SilkSpawners/src/main/java/de/dustplanet/util
    • v1_11_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_11_R1
    • v1_12_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_12_R1
    • v1_13_R2/src/main/java/de/dustplanet/silkspawners/compat/v1_13_R2
    • v1_14_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_14_R1
    • v1_15_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_15_R1
    • v1_16_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_16_R1
    • v1_16_R2/src/main/java/de/dustplanet/silkspawners/compat/v1_16_R2
    • v1_16_R3/src/main/java/de/dustplanet/silkspawners/compat/v1_16_R3
    • v1_17_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_17_R1
    • v1_18_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_18_R1
    • v1_18_R2/src/main/java/de/dustplanet/silkspawners/compat/v1_18_R2
    • v1_19_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_19_R1
    • v1_19_R2/src/main/java/de/dustplanet/silkspawners/compat/v1_19_R2
    • v1_19_R3/src/main/java/de/dustplanet/silkspawners/compat/v1_19_R3
    • v1_20_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R1
    • v1_20_R2/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R2
    • v1_20_R3/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R3
    • v1_20_R4/src/main/java/de/dustplanet/silkspawners/compat/v1_20_R4
    • v1_21_R1/src/main/java/de/dustplanet/silkspawners/compat/v1_21_R1
    • v1_8_R3/src/main/java/de/dustplanet/silkspawners/compat/v1_8_R3

22 files changed

+375
-43
lines changed

modules/API/src/main/java/de/dustplanet/silkspawners/compat/api/NMSProvider.java

+4
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ default SortedMap<Integer, String> legacyRawEntityMap() {
4545

4646
String getVanillaNBTEntityID(ItemStack item);
4747

48+
default String getOtherPluginsNBTEntityID(@SuppressWarnings("unused") final ItemStack item) {
49+
return null;
50+
}
51+
4852
Block getSpawnerFacing(Player player, int distance);
4953

5054
default Collection<? extends Player> getOnlinePlayers() {

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

+26-20
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,15 @@ private boolean setupNMSProvider() {
170170
// Rare cases might trigger API usage before SilkSpawners
171171
if (version == null) {
172172
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
173-
String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
173+
final String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
174174
if (nmsVersion.equals("craftbukkit")) {
175175
try {
176-
String minecraftVersion = (String) Server.class.getDeclaredMethod("getMinecraftVersion").invoke(Bukkit.getServer());
177-
Semver semver = new Semver(minecraftVersion);
176+
final String minecraftVersion = (String) Server.class.getDeclaredMethod("getMinecraftVersion")
177+
.invoke(Bukkit.getServer());
178+
final Semver semver = new Semver(minecraftVersion);
178179
if (semver.isGreaterThanOrEqualTo("1.20.5")) {
179180
@SuppressWarnings("deprecation")
180-
int protocolVersion = (Integer) UnsafeValues.class.getDeclaredMethod("getProtocolVersion")
181+
final int protocolVersion = (Integer) UnsafeValues.class.getDeclaredMethod("getProtocolVersion")
181182
.invoke(Bukkit.getUnsafe());
182183
version = SilkSpawners.PROTOCOL_VERSION_PACKAGE_MAP.get(protocolVersion);
183184
}
@@ -344,7 +345,7 @@ public boolean isVanillaBossBar() {
344345
*
345346
* @deprecated Use {@link SilkUtil#newEggItem(String, int, String)} instead.
346347
* @param entityID which mob should be spawned
347-
* @param amount the amount of spawn eggs
348+
* @param amount the amount of spawn eggs
348349
* @return the ItemStack
349350
*/
350351
@Deprecated
@@ -355,8 +356,8 @@ public ItemStack newEggItem(final String entityID, final int amount) {
355356
/**
356357
* Returns a new ItemStack of a spawn egg with the specified amount and mob.
357358
*
358-
* @param entityID which mob should be spawned
359-
* @param amount the amount of spawn eggs
359+
* @param entityID which mob should be spawned
360+
* @param amount the amount of spawn eggs
360361
* @param displayName the display name of the egg in case of unknown entities
361362
* @return the ItemStack
362363
*/
@@ -368,10 +369,10 @@ public ItemStack newEggItem(final String entityID, final int amount, final Strin
368369
/**
369370
* This method will make a new MobSpawner with a custom entityID, name and amount.
370371
*
371-
* @param entityID the mob
372+
* @param entityID the mob
372373
* @param customName if the MobSpawner should be named different
373-
* @param amount the wanted amount
374-
* @param forceLore whether the lore tag should be forces
374+
* @param amount the wanted amount
375+
* @param forceLore whether the lore tag should be forces
375376
* @return the ItemStack with the configured options
376377
*/
377378
public ItemStack newSpawnerItem(final String entityID, final String customName, final int amount, final boolean forceLore) {
@@ -458,6 +459,11 @@ public String getStoredSpawnerItemEntityID(final ItemStack item) {
458459
if (StringUtils.isNotBlank(entityID)) {
459460
return entityID.replace("minecraft:", "");
460461
}
462+
entityID = nmsProvider.getOtherPluginsNBTEntityID(item);
463+
plugin.getLogger().log(Level.FINE, "EntityID from item stack (other plugin tags) is {0}", entityID);
464+
if (StringUtils.isNotBlank(entityID)) {
465+
return entityID.replace("minecraft:", "");
466+
}
461467
}
462468
if (item.hasItemMeta()) {
463469
final String metaEntityID = searchItemMeta(item.getItemMeta());
@@ -533,7 +539,7 @@ public String getSpawnerEntityID(final Block block) {
533539
/**
534540
* Set the specified MonterSpawner to another entity ID.
535541
*
536-
* @param block MonsterSpawner
542+
* @param block MonsterSpawner
537543
* @param entity the wanted entity
538544
*/
539545
public void setSpawnerEntityID(final Block block, final String entity) {
@@ -567,9 +573,9 @@ public void setSpawnerEntityID(final Block block, final String entity) {
567573
/**
568574
* Set a spawner (if allowed) to a new mob.
569575
*
570-
* @param block the MonsterSpawner
571-
* @param entityID the new entity ID
572-
* @param player the player
576+
* @param block the MonsterSpawner
577+
* @param entityID the new entity ID
578+
* @param player the player
573579
* @param messageDenied the message which is shown, when the player can't build here see {@link #canBuildHere(Player, Location)}
574580
* @return whether the operation was successful or not
575581
*/
@@ -587,8 +593,8 @@ public boolean setSpawnerType(final Block block, final String entityID, final Pl
587593
/**
588594
* Sets a spawner item or egg to a new ID.
589595
*
590-
* @param item ItemStack (Egg or Spawner)
591-
* @param entityID wanted entity ID
596+
* @param item ItemStack (Egg or Spawner)
597+
* @param entityID wanted entity ID
592598
* @param customName if a custom name should be used (null for none)
593599
* @return the updated ItemStack
594600
*/
@@ -734,7 +740,7 @@ public List<String> scanEntityMap() {
734740
/**
735741
* Notify a player about the spawner.
736742
*
737-
* @param player the player
743+
* @param player the player
738744
* @param spawnerName the creature name
739745
*/
740746
@SuppressWarnings("deprecation")
@@ -938,7 +944,7 @@ private void getMimic() {
938944
/**
939945
* Checks if a player can build here (WorldGuard).
940946
*
941-
* @param player the player
947+
* @param player the player
942948
* @param location the location to check
943949
* @return the result, true or false
944950
*/
@@ -980,9 +986,9 @@ public boolean isLegacySpawnEggs() {
980986
/**
981987
* Helper methods to check if a player has any of the aliases permissions for a given mobID.
982988
*
983-
* @param permissible - the permissible to check the permission for
989+
* @param permissible - the permissible to check the permission for
984990
* @param basePermission - the basis permission without the specific mob
985-
* @param entityID - the internal mob ID (not display name)
991+
* @param entityID - the internal mob ID (not display name)
986992
* @return the permission check result, true if the player has got the permission, false otherwise
987993
*/
988994
public boolean hasPermission(final Permissible permissible, final String basePermission, final String entityID) {

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

+16
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,22 @@ public String getVanillaNBTEntityID(final ItemStack item) {
300300
}
301301
}
302302

303+
@Override
304+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
305+
net.minecraft.server.v1_11_R1.ItemStack itemStack = null;
306+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
307+
itemStack = CraftItemStack.asNMSCopy(craftStack);
308+
final NBTTagCompound tag = itemStack.getTag();
309+
310+
if (tag == null) {
311+
return null;
312+
}
313+
if (tag.hasKey("ms_mob")) {
314+
return tag.getString("ms_mob");
315+
}
316+
return null;
317+
}
318+
303319
/**
304320
* Return the spawner block the player is looking at, or null if isn't.
305321
*

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

+16
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,22 @@ public String getVanillaNBTEntityID(final ItemStack item) {
304304
}
305305
}
306306

307+
@Override
308+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
309+
net.minecraft.server.v1_12_R1.ItemStack itemStack = null;
310+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
311+
itemStack = CraftItemStack.asNMSCopy(craftStack);
312+
final NBTTagCompound tag = itemStack.getTag();
313+
314+
if (tag == null) {
315+
return null;
316+
}
317+
if (tag.hasKey("ms_mob")) {
318+
return tag.getString("ms_mob");
319+
}
320+
return null;
321+
}
322+
307323
/**
308324
* Return the spawner block the player is looking at, or null if isn't.
309325
*

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
274274
}
275275
}
276276

277+
@Override
278+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
279+
net.minecraft.server.v1_13_R2.ItemStack itemStack = null;
280+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
281+
itemStack = CraftItemStack.asNMSCopy(craftStack);
282+
final NBTTagCompound tag = itemStack.getTag();
283+
284+
if (tag == null) {
285+
return null;
286+
}
287+
if (tag.hasKey("ms_mob")) {
288+
return tag.getString("ms_mob");
289+
}
290+
return null;
291+
}
292+
277293
/**
278294
* Return the spawner block the player is looking at, or null if isn't.
279295
*
280-
* @param player the player
296+
* @param player the player
281297
* @param distance the reach distance
282298
* @return the found block or null
283299
*/

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public List<String> rawEntityMap() {
124124
final List<String> entities = new ArrayList<>();
125125
try {
126126
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
127-
for (EntityTypes<?> next : entityTypeRegistry) {
127+
for (final EntityTypes<?> next : entityTypeRegistry) {
128128
entities.add(EntityTypes.getName(next).getKey());
129129
}
130130
} catch (SecurityException | IllegalArgumentException e) {
@@ -271,10 +271,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
271271
}
272272
}
273273

274+
@Override
275+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
276+
net.minecraft.server.v1_14_R1.ItemStack itemStack = null;
277+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
278+
itemStack = CraftItemStack.asNMSCopy(craftStack);
279+
final NBTTagCompound tag = itemStack.getTag();
280+
281+
if (tag == null) {
282+
return null;
283+
}
284+
if (tag.hasKey("ms_mob")) {
285+
return tag.getString("ms_mob");
286+
}
287+
return null;
288+
}
289+
274290
/**
275291
* Return the spawner block the player is looking at, or null if isn't.
276292
*
277-
* @param player the player
293+
* @param player the player
278294
* @param distance the reach distance
279295
* @return the found block or null
280296
*/

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public List<String> rawEntityMap() {
139139
final List<String> entities = new ArrayList<>();
140140
try {
141141
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
142-
for (EntityTypes<?> next : entityTypeRegistry) {
142+
for (final EntityTypes<?> next : entityTypeRegistry) {
143143
entities.add(EntityTypes.getName(next).getKey());
144144
}
145145
} catch (SecurityException | IllegalArgumentException e) {
@@ -287,10 +287,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
287287
}
288288
}
289289

290+
@Override
291+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
292+
net.minecraft.server.v1_15_R1.ItemStack itemStack = null;
293+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
294+
itemStack = CraftItemStack.asNMSCopy(craftStack);
295+
final NBTTagCompound tag = itemStack.getTag();
296+
297+
if (tag == null) {
298+
return null;
299+
}
300+
if (tag.hasKey("ms_mob")) {
301+
return tag.getString("ms_mob");
302+
}
303+
return null;
304+
}
305+
290306
/**
291307
* Return the spawner block the player is looking at, or null if isn't.
292308
*
293-
* @param player the player
309+
* @param player the player
294310
* @param distance the reach distance
295311
* @return the found block or null
296312
*/

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public List<String> rawEntityMap() {
138138
final List<String> entities = new ArrayList<>();
139139
try {
140140
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
141-
for (EntityTypes<?> next : entityTypeRegistry) {
141+
for (final EntityTypes<?> next : entityTypeRegistry) {
142142
entities.add(EntityTypes.getName(next).getKey());
143143
}
144144
} catch (SecurityException | IllegalArgumentException e) {
@@ -286,10 +286,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
286286
}
287287
}
288288

289+
@Override
290+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
291+
net.minecraft.server.v1_16_R1.ItemStack itemStack = null;
292+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
293+
itemStack = CraftItemStack.asNMSCopy(craftStack);
294+
final NBTTagCompound tag = itemStack.getTag();
295+
296+
if (tag == null) {
297+
return null;
298+
}
299+
if (tag.hasKey("ms_mob")) {
300+
return tag.getString("ms_mob");
301+
}
302+
return null;
303+
}
304+
289305
/**
290306
* Return the spawner block the player is looking at, or null if isn't.
291307
*
292-
* @param player the player
308+
* @param player the player
293309
* @param distance the reach distance
294310
* @return the found block or null
295311
*/

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public List<String> rawEntityMap() {
138138
final List<String> entities = new ArrayList<>();
139139
try {
140140
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
141-
for (EntityTypes<?> next : entityTypeRegistry) {
141+
for (final EntityTypes<?> next : entityTypeRegistry) {
142142
entities.add(EntityTypes.getName(next).getKey());
143143
}
144144
} catch (SecurityException | IllegalArgumentException e) {
@@ -286,10 +286,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
286286
}
287287
}
288288

289+
@Override
290+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
291+
net.minecraft.server.v1_16_R2.ItemStack itemStack = null;
292+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
293+
itemStack = CraftItemStack.asNMSCopy(craftStack);
294+
final NBTTagCompound tag = itemStack.getTag();
295+
296+
if (tag == null) {
297+
return null;
298+
}
299+
if (tag.hasKey("ms_mob")) {
300+
return tag.getString("ms_mob");
301+
}
302+
return null;
303+
}
304+
289305
/**
290306
* Return the spawner block the player is looking at, or null if isn't.
291307
*
292-
* @param player the player
308+
* @param player the player
293309
* @param distance the reach distance
294310
* @return the found block or null
295311
*/

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

+18-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public List<String> rawEntityMap() {
138138
final List<String> entities = new ArrayList<>();
139139
try {
140140
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
141-
for (EntityTypes<?> next : entityTypeRegistry) {
141+
for (final EntityTypes<?> next : entityTypeRegistry) {
142142
entities.add(EntityTypes.getName(next).getKey());
143143
}
144144
} catch (SecurityException | IllegalArgumentException e) {
@@ -286,10 +286,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
286286
}
287287
}
288288

289+
@Override
290+
public String getOtherPluginsNBTEntityID(final ItemStack item) {
291+
net.minecraft.server.v1_16_R3.ItemStack itemStack = null;
292+
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
293+
itemStack = CraftItemStack.asNMSCopy(craftStack);
294+
final NBTTagCompound tag = itemStack.getTag();
295+
296+
if (tag == null) {
297+
return null;
298+
}
299+
if (tag.hasKey("ms_mob")) {
300+
return tag.getString("ms_mob");
301+
}
302+
return null;
303+
}
304+
289305
/**
290306
* Return the spawner block the player is looking at, or null if isn't.
291307
*
292-
* @param player the player
308+
* @param player the player
293309
* @param distance the reach distance
294310
* @return the found block or null
295311
*/

0 commit comments

Comments
 (0)