Skip to content

Commit

Permalink
feat: adds method to read other plugins NBT data (support for Mineabl…
Browse files Browse the repository at this point in the history
…eSpawners added)
  • Loading branch information
timbru31 committed Jul 15, 2024
1 parent 50674c3 commit 3d0e54f
Show file tree
Hide file tree
Showing 22 changed files with 375 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ default SortedMap<Integer, String> legacyRawEntityMap() {

String getVanillaNBTEntityID(ItemStack item);

default String getOtherPluginsNBTEntityID(@SuppressWarnings("unused") final ItemStack item) {
return null;
}

Block getSpawnerFacing(Player player, int distance);

default Collection<? extends Player> getOnlinePlayers() {
Expand Down
46 changes: 26 additions & 20 deletions modules/SilkSpawners/src/main/java/de/dustplanet/util/SilkUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,15 @@ private boolean setupNMSProvider() {
// Rare cases might trigger API usage before SilkSpawners
if (version == null) {
final String packageName = Bukkit.getServer().getClass().getPackage().getName();
String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
final String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
if (nmsVersion.equals("craftbukkit")) {
try {
String minecraftVersion = (String) Server.class.getDeclaredMethod("getMinecraftVersion").invoke(Bukkit.getServer());
Semver semver = new Semver(minecraftVersion);
final String minecraftVersion = (String) Server.class.getDeclaredMethod("getMinecraftVersion")
.invoke(Bukkit.getServer());
final Semver semver = new Semver(minecraftVersion);
if (semver.isGreaterThanOrEqualTo("1.20.5")) {
@SuppressWarnings("deprecation")
int protocolVersion = (Integer) UnsafeValues.class.getDeclaredMethod("getProtocolVersion")
final int protocolVersion = (Integer) UnsafeValues.class.getDeclaredMethod("getProtocolVersion")
.invoke(Bukkit.getUnsafe());
version = SilkSpawners.PROTOCOL_VERSION_PACKAGE_MAP.get(protocolVersion);
}
Expand Down Expand Up @@ -344,7 +345,7 @@ public boolean isVanillaBossBar() {
*
* @deprecated Use {@link SilkUtil#newEggItem(String, int, String)} instead.
* @param entityID which mob should be spawned
* @param amount the amount of spawn eggs
* @param amount the amount of spawn eggs
* @return the ItemStack
*/
@Deprecated
Expand All @@ -355,8 +356,8 @@ public ItemStack newEggItem(final String entityID, final int amount) {
/**
* Returns a new ItemStack of a spawn egg with the specified amount and mob.
*
* @param entityID which mob should be spawned
* @param amount the amount of spawn eggs
* @param entityID which mob should be spawned
* @param amount the amount of spawn eggs
* @param displayName the display name of the egg in case of unknown entities
* @return the ItemStack
*/
Expand All @@ -368,10 +369,10 @@ public ItemStack newEggItem(final String entityID, final int amount, final Strin
/**
* This method will make a new MobSpawner with a custom entityID, name and amount.
*
* @param entityID the mob
* @param entityID the mob
* @param customName if the MobSpawner should be named different
* @param amount the wanted amount
* @param forceLore whether the lore tag should be forces
* @param amount the wanted amount
* @param forceLore whether the lore tag should be forces
* @return the ItemStack with the configured options
*/
public ItemStack newSpawnerItem(final String entityID, final String customName, final int amount, final boolean forceLore) {
Expand Down Expand Up @@ -458,6 +459,11 @@ public String getStoredSpawnerItemEntityID(final ItemStack item) {
if (StringUtils.isNotBlank(entityID)) {
return entityID.replace("minecraft:", "");
}
entityID = nmsProvider.getOtherPluginsNBTEntityID(item);
plugin.getLogger().log(Level.FINE, "EntityID from item stack (other plugin tags) is {0}", entityID);
if (StringUtils.isNotBlank(entityID)) {
return entityID.replace("minecraft:", "");
}
}
if (item.hasItemMeta()) {
final String metaEntityID = searchItemMeta(item.getItemMeta());
Expand Down Expand Up @@ -533,7 +539,7 @@ public String getSpawnerEntityID(final Block block) {
/**
* Set the specified MonterSpawner to another entity ID.
*
* @param block MonsterSpawner
* @param block MonsterSpawner
* @param entity the wanted entity
*/
public void setSpawnerEntityID(final Block block, final String entity) {
Expand Down Expand Up @@ -567,9 +573,9 @@ public void setSpawnerEntityID(final Block block, final String entity) {
/**
* Set a spawner (if allowed) to a new mob.
*
* @param block the MonsterSpawner
* @param entityID the new entity ID
* @param player the player
* @param block the MonsterSpawner
* @param entityID the new entity ID
* @param player the player
* @param messageDenied the message which is shown, when the player can't build here see {@link #canBuildHere(Player, Location)}
* @return whether the operation was successful or not
*/
Expand All @@ -587,8 +593,8 @@ public boolean setSpawnerType(final Block block, final String entityID, final Pl
/**
* Sets a spawner item or egg to a new ID.
*
* @param item ItemStack (Egg or Spawner)
* @param entityID wanted entity ID
* @param item ItemStack (Egg or Spawner)
* @param entityID wanted entity ID
* @param customName if a custom name should be used (null for none)
* @return the updated ItemStack
*/
Expand Down Expand Up @@ -734,7 +740,7 @@ public List<String> scanEntityMap() {
/**
* Notify a player about the spawner.
*
* @param player the player
* @param player the player
* @param spawnerName the creature name
*/
@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -938,7 +944,7 @@ private void getMimic() {
/**
* Checks if a player can build here (WorldGuard).
*
* @param player the player
* @param player the player
* @param location the location to check
* @return the result, true or false
*/
Expand Down Expand Up @@ -980,9 +986,9 @@ public boolean isLegacySpawnEggs() {
/**
* Helper methods to check if a player has any of the aliases permissions for a given mobID.
*
* @param permissible - the permissible to check the permission for
* @param permissible - the permissible to check the permission for
* @param basePermission - the basis permission without the specific mob
* @param entityID - the internal mob ID (not display name)
* @param entityID - the internal mob ID (not display name)
* @return the permission check result, true if the player has got the permission, false otherwise
*/
public boolean hasPermission(final Permissible permissible, final String basePermission, final String entityID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,22 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_11_R1.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,22 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_12_R1.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_13_R2.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
* @param player the player
* @param player the player
* @param distance the reach distance
* @return the found block or null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public List<String> rawEntityMap() {
final List<String> entities = new ArrayList<>();
try {
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
for (EntityTypes<?> next : entityTypeRegistry) {
for (final EntityTypes<?> next : entityTypeRegistry) {
entities.add(EntityTypes.getName(next).getKey());
}
} catch (SecurityException | IllegalArgumentException e) {
Expand Down Expand Up @@ -271,10 +271,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_14_R1.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
* @param player the player
* @param player the player
* @param distance the reach distance
* @return the found block or null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public List<String> rawEntityMap() {
final List<String> entities = new ArrayList<>();
try {
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
for (EntityTypes<?> next : entityTypeRegistry) {
for (final EntityTypes<?> next : entityTypeRegistry) {
entities.add(EntityTypes.getName(next).getKey());
}
} catch (SecurityException | IllegalArgumentException e) {
Expand Down Expand Up @@ -287,10 +287,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_15_R1.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
* @param player the player
* @param player the player
* @param distance the reach distance
* @return the found block or null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public List<String> rawEntityMap() {
final List<String> entities = new ArrayList<>();
try {
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
for (EntityTypes<?> next : entityTypeRegistry) {
for (final EntityTypes<?> next : entityTypeRegistry) {
entities.add(EntityTypes.getName(next).getKey());
}
} catch (SecurityException | IllegalArgumentException e) {
Expand Down Expand Up @@ -286,10 +286,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_16_R1.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
* @param player the player
* @param player the player
* @param distance the reach distance
* @return the found block or null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public List<String> rawEntityMap() {
final List<String> entities = new ArrayList<>();
try {
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
for (EntityTypes<?> next : entityTypeRegistry) {
for (final EntityTypes<?> next : entityTypeRegistry) {
entities.add(EntityTypes.getName(next).getKey());
}
} catch (SecurityException | IllegalArgumentException e) {
Expand Down Expand Up @@ -286,10 +286,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_16_R2.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
* @param player the player
* @param player the player
* @param distance the reach distance
* @return the found block or null
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public List<String> rawEntityMap() {
final List<String> entities = new ArrayList<>();
try {
final IRegistry<EntityTypes<?>> entityTypeRegistry = IRegistry.ENTITY_TYPE;
for (EntityTypes<?> next : entityTypeRegistry) {
for (final EntityTypes<?> next : entityTypeRegistry) {
entities.add(EntityTypes.getName(next).getKey());
}
} catch (SecurityException | IllegalArgumentException e) {
Expand Down Expand Up @@ -286,10 +286,26 @@ public String getVanillaNBTEntityID(final ItemStack item) {
}
}

@Override
public String getOtherPluginsNBTEntityID(final ItemStack item) {
net.minecraft.server.v1_16_R3.ItemStack itemStack = null;
final CraftItemStack craftStack = CraftItemStack.asCraftCopy(item);
itemStack = CraftItemStack.asNMSCopy(craftStack);
final NBTTagCompound tag = itemStack.getTag();

if (tag == null) {
return null;
}
if (tag.hasKey("ms_mob")) {
return tag.getString("ms_mob");
}
return null;
}

/**
* Return the spawner block the player is looking at, or null if isn't.
*
* @param player the player
* @param player the player
* @param distance the reach distance
* @return the found block or null
*/
Expand Down
Loading

0 comments on commit 3d0e54f

Please sign in to comment.