use of net.minecraft.server.v1_9_R2.MinecraftKey in project SilkSpawners by timbru31.
the class NMSHandler method getMobNameOfSpawner.
@Override
public String getMobNameOfSpawner(final BlockState blockState) {
final CraftCreatureSpawner spawner = (CraftCreatureSpawner) blockState;
try {
final TileEntityMobSpawner tile = (TileEntityMobSpawner) tileField.get(spawner);
final MinecraftKey minecraftKey = tile.getSpawner().getMobName();
return minecraftKey != null ? minecraftKey.getKey() : "";
} catch (IllegalArgumentException | IllegalAccessException e) {
Bukkit.getLogger().warning("[SilkSpawners] Reflection failed: " + e.getMessage());
e.printStackTrace();
}
return "";
}
use of net.minecraft.server.v1_9_R2.MinecraftKey in project SilkSpawners by timbru31.
the class NMSHandler method setMobNameOfSpawner.
@Override
public boolean setMobNameOfSpawner(final BlockState blockState, final String mobID) {
// Prevent ResourceKeyInvalidException: Non [a-z0-9/._-] character in path of location
final String safeMobID = caseFormatOf(mobID.replace(" ", "_")).to(CaseFormat.LOWER_UNDERSCORE, mobID.replace(" ", "_")).toLowerCase(Locale.ENGLISH);
final CraftCreatureSpawner spawner = (CraftCreatureSpawner) blockState;
try {
final TileEntityMobSpawner tile = (TileEntityMobSpawner) tileField.get(spawner);
tile.getSpawner().setMobName(IRegistry.ENTITY_TYPE.get(new MinecraftKey(safeMobID)));
return true;
} catch (IllegalArgumentException | IllegalAccessException e) {
Bukkit.getLogger().warning("[SilkSpawners] Reflection failed: " + e.getMessage());
e.printStackTrace();
}
return false;
}
use of net.minecraft.server.v1_9_R2.MinecraftKey in project SilkSpawners by timbru31.
the class NMSHandler method getMobNameOfSpawner.
@Override
@Nullable
public String getMobNameOfSpawner(final BlockState blockState) {
final CraftCreatureSpawner spawner = (CraftCreatureSpawner) blockState;
try {
final TileEntityMobSpawner tile = (TileEntityMobSpawner) tileField.get(spawner);
final MinecraftKey minecraftKey = tile.getSpawner().getMobName();
return minecraftKey != null ? minecraftKey.getKey() : "";
} catch (IllegalArgumentException | IllegalAccessException e) {
Bukkit.getLogger().warning("[SilkSpawners] Reflection failed: " + e.getMessage());
e.printStackTrace();
}
return null;
}
use of net.minecraft.server.v1_9_R2.MinecraftKey in project SilkSpawners by timbru31.
the class NMSHandler method legacyRawEntityMap.
@Override
public SortedMap<Integer, String> legacyRawEntityMap() {
// This bypasses Bukkit's wrappers, so it works with mods
try {
// TODO Needs 1.12 source
final Field field = EntityTypes.class.getDeclaredField("g");
final Field field2 = EntityTypes.class.getDeclaredField("b");
field.setAccessible(true);
@SuppressWarnings("unchecked") final List<String> list = (List<String>) field.get(null);
@SuppressWarnings("unchecked") final RegistryMaterials<MinecraftKey, Class<? extends Entity>> registry = (RegistryMaterials<MinecraftKey, Class<? extends Entity>>) field2.get(null);
// For each entry in our name -- ID map but it into the sortedMap
for (int entityID = 0; entityID < list.size(); entityID++) {
final String displayName = list.get(entityID);
if (displayName == null) {
continue;
}
final Class<? extends Entity> entity = registry.getId(entityID);
if (entity == null) {
Bukkit.getLogger().severe("[SilkSpawners] Failed to dump entity map: entity is null, entityID: " + entityID);
continue;
}
MinecraftKey minecraftKey = null;
try {
minecraftKey = registry.b(entity);
} catch (@SuppressWarnings("unused") final ClassCastException e) {
Bukkit.getLogger().severe("[SilkSpawners] Failed to dump entity map: entity is invalid, entityID: " + entityID);
Bukkit.getLogger().severe("[SilkSpawners] Failed to dump entity map: entity is invalid, entity: " + entity.getSimpleName());
continue;
}
if (minecraftKey == null) {
Bukkit.getLogger().severe("[SilkSpawners] Failed to dump entity map: minecraftKey is null, entityID: " + entityID);
Bukkit.getLogger().severe("[SilkSpawners] Failed to dump entity map: minecraftKey is null, entity: " + entity.getSimpleName());
continue;
}
sortedMap.put(entityID, minecraftKey.getKey());
}
} catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
Bukkit.getLogger().severe("[SilkSpawners] Failed to dump entity map: " + e.getMessage());
e.printStackTrace();
}
return sortedMap;
}
use of net.minecraft.server.v1_9_R2.MinecraftKey in project SilkSpawners by timbru31.
the class NMSHandler method setMobNameOfSpawner.
@Override
public boolean setMobNameOfSpawner(final BlockState blockState, final String mobID) {
// Prevent ResourceKeyInvalidException: Non [a-z0-9/._-] character in path of location
final String safeMobID = caseFormatOf(mobID.replace(" ", "_")).to(CaseFormat.LOWER_UNDERSCORE, mobID.replace(" ", "_")).toLowerCase(Locale.ENGLISH);
final CraftCreatureSpawner spawner = (CraftCreatureSpawner) blockState;
try {
final TileEntityMobSpawner tile = (TileEntityMobSpawner) tileField.get(spawner);
tile.getSpawner().setMobName(IRegistry.ENTITY_TYPE.get(new MinecraftKey(safeMobID)));
return true;
} catch (IllegalArgumentException | IllegalAccessException e) {
Bukkit.getLogger().warning("[SilkSpawners] Reflection failed: " + e.getMessage());
e.printStackTrace();
}
return false;
}
Aggregations