use of com.bergerkiller.generated.net.minecraft.resources.MinecraftKeyHandle in project BKCommonLib by bergerhealer.
the class PlayerRespawnPoint method fromNBT.
/**
* Decodes the player respawn point from NBT, formatted in the
* same way player data is stored. If no player respawn point
* is configured in the NBT, then the {@link #NONE} constant is returned.
*
* @param nbt NBT Tag Compound to decode from
* @return Player Respawn Point, {@link #NONE} if none or incomplete data is available
*/
public static PlayerRespawnPoint fromNBT(CommonTagCompound nbt) {
World world = null;
// Read the SpawnDimension in the new format
if (CommonCapabilities.PLAYER_SPAWN_WORLD_IS_DIMENSION_KEY) {
MinecraftKeyHandle spawnDimensionName = nbt.getMinecraftKey("SpawnDimension");
if (spawnDimensionName != null) {
world = WorldUtil.getWorldByDimensionKey(ResourceCategory.dimension.createKey(spawnDimensionName));
}
}
// Migrate "SpawnWorld" name to "SpawnDimension" if needed when dimension key is used
if (world == null && nbt.containsKey("SpawnWorld")) {
world = Bukkit.getWorld(nbt.getValue("SpawnWorld", String.class));
}
// If world or other essential fields are missing, return NONE
if (world == null || !nbt.containsKey("SpawnX") || !nbt.containsKey("SpawnY") || !nbt.containsKey("SpawnZ")) {
return NONE;
}
// Decode further
int x = nbt.getValue("SpawnX", 0);
int y = nbt.getValue("SpawnY", 0);
int z = nbt.getValue("SpawnZ", 0);
float angle = nbt.getValue("SpawnAngle", 0.0f);
return new PlayerRespawnPoint(world, x, y, z, angle);
}
use of com.bergerkiller.generated.net.minecraft.resources.MinecraftKeyHandle in project BKCommonLib by bergerhealer.
the class TileEntityTypesSerializedIds_1_8_to_1_17_1 method toMinecraftKeyFromLegacyName.
// Only for MC 1.10.2 and before
public static MinecraftKeyHandle toMinecraftKeyFromLegacyName(String legacyName) {
MinecraftKeyHandle minecraftKey = legacyNameToMinecraftKey.get(legacyName);
if (minecraftKey == null && legacyName != null) {
// Find all uppercase characters, put a _ in front except for first char
// EnderChest -> ender_chest
StringBuilder str = new StringBuilder();
for (int i = 0; i < legacyName.length(); i++) {
char c = legacyName.charAt(i);
if (i > 0 && Character.isUpperCase(c)) {
str.append('_');
}
str.append(Character.toLowerCase(c));
}
// Convert to a key
minecraftKey = MinecraftKeyHandle.createNew(str.toString());
}
return minecraftKey;
}
use of com.bergerkiller.generated.net.minecraft.resources.MinecraftKeyHandle in project BKCommonLib by bergerhealer.
the class TileEntityTypesSerializedIds_1_8_to_1_17_1 method register.
private static void register(int id, String name, String pre_1_10_2_Name) {
MinecraftKeyHandle keyHandle = MinecraftKeyHandle.createNew(name);
Object minecraftKey = keyHandle.getRaw();
if (id != -1) {
idsToMinecraftKey.put(id, minecraftKey);
}
minecraftKeyToLegacyName.put(keyHandle, pre_1_10_2_Name);
}
use of com.bergerkiller.generated.net.minecraft.resources.MinecraftKeyHandle in project BKCommonLib by bergerhealer.
the class BlockDataSerializer_1_8_to_1_12_2 method deserialize.
@Override
public BlockData deserialize(String text) {
// Read block name and (optional) block states (or legacy data)
String blockName;
String stateText = null;
int legacyDataValue = -1;
{
int index;
if ((index = text.indexOf('[')) != -1) {
blockName = text.substring(0, index);
int endIndex = text.indexOf(']', index + 1);
if (endIndex != -1) {
stateText = text.substring(index + 1, endIndex).trim();
} else {
stateText = text.substring(index + 1).trim();
}
} else {
blockName = text;
// If last element after : is a data value, parse legacy data
if ((index = text.lastIndexOf(':')) != -1) {
String dataText = text.substring(index + 1).trim();
if (ParseUtil.isNumeric(dataText)) {
try {
legacyDataValue = Integer.parseInt(stateText);
if (legacyDataValue < 0 || legacyDataValue > 15) {
// invalid data value
return null;
}
blockName = text.substring(0, index);
} catch (NumberFormatException ex) {
// Ignore
}
}
}
}
}
// Parse Block name to MinecraftKey, and find the Block with this name
MinecraftKeyHandle key = MinecraftKeyHandle.createNew(blockName);
if (key == null) {
// invalid key name format
return null;
}
Object nmsBlock = findBlockByNameMethod.invoke(null, key.getRaw());
if (nmsBlock == null) {
// block not found
return null;
}
// If state text can be parsed as a number, parse from legacy data
if (legacyDataValue != -1) {
return BlockData.fromBlockData(createLegacyBlockDataMethod.invoke(null, nmsBlock, legacyDataValue));
}
// Create default BlockData from Block
Object nmsIBlockData = BlockHandle.T.getBlockData.raw.invoke(nmsBlock);
// Split the state text by , and each element by =
if (stateText != null && !stateText.isEmpty()) {
int index = 0;
do {
int nextElement = stateText.indexOf(',', index);
int nextValue = stateText.indexOf('=', index);
if (nextValue == -1 || (nextElement != -1 && nextValue > nextElement)) {
// Invalid syntax (missing =)
return null;
}
// Retrieve key and value text
String keyText = stateText.substring(index, nextValue);
String valueText;
if (nextElement == -1) {
valueText = stateText.substring(nextValue + 1);
} else {
valueText = stateText.substring(nextValue + 1, nextElement);
}
// Attempt setting the key-value. If key or value cannot be parsed, returns null.
if ((nmsIBlockData = setBlockDataKeyValueMethod.invoke(null, nmsIBlockData, keyText, valueText)) == null) {
// Could not be parsed
return null;
}
// Next element
index = nextElement + 1;
} while (index != 0);
}
// To wrapper and done!
return BlockData.fromBlockData(nmsIBlockData);
}
use of com.bergerkiller.generated.net.minecraft.resources.MinecraftKeyHandle in project BKCommonLib by bergerhealer.
the class CommonTagCompound method putGetRemove.
// implements get/put/remove with a simplified single implementation function
private final <T> T putGetRemove(PutGetRemoveOp op, String key, Class<T> type, T value) {
Object rawNBTResult = null;
if (type == UUID.class) {
// == UUID ==
UUID uuid = (UUID) value;
Long uuidMost = putGetRemove(op, key + "UUIDMost", Long.class, (uuid == null) ? null : uuid.getMostSignificantBits());
Long uuidLeast = putGetRemove(op, key + "UUIDLeast", Long.class, (uuid == null) ? null : uuid.getLeastSignificantBits());
if (uuidMost != null && uuidLeast != null) {
return (T) new UUID(uuidMost.longValue(), uuidLeast.longValue());
}
} else if (type == BlockLocation.class) {
// == BlockLocation ==
BlockLocation pos = (BlockLocation) value;
String world = putGetRemove(op, key + "World", String.class, (pos == null) ? null : pos.world);
Integer x = putGetRemove(op, key + "X", Integer.class, (pos == null) ? null : pos.x);
Integer y = putGetRemove(op, key + "Y", Integer.class, (pos == null) ? null : pos.y);
Integer z = putGetRemove(op, key + "Z", Integer.class, (pos == null) ? null : pos.z);
if (world != null && !world.isEmpty() && x != null && y != null && z != null) {
return (T) new BlockLocation(world, x.intValue(), y.intValue(), z.intValue());
}
} else if (type == IntVector3.class) {
// == IntVector3 ==
IntVector3 pos = (IntVector3) value;
Integer x = putGetRemove(op, key + "X", Integer.class, (pos == null) ? null : pos.x);
Integer y = putGetRemove(op, key + "Y", Integer.class, (pos == null) ? null : pos.y);
Integer z = putGetRemove(op, key + "Z", Integer.class, (pos == null) ? null : pos.z);
if (x != null && y != null && z != null) {
return (T) new IntVector3(x.intValue(), y.intValue(), z.intValue());
}
} else if (type == MinecraftKeyHandle.class) {
// == MinecraftKeyHandle ==
String v = putGetRemove(op, key, String.class, (value == null) ? null : ((MinecraftKeyHandle) value).toString());
if (v != null) {
return (T) MinecraftKeyHandle.createNew(v);
}
} else if (type == boolean.class || type == Boolean.class) {
// == Booleans (serialized as Byte) ==
Byte v = putGetRemove(op, key, Byte.class, (value == null) ? null : ((Boolean) value) ? (byte) 1 : (byte) 0);
if (v != null) {
return (T) ((v.byteValue() != (byte) 0) ? Boolean.TRUE : Boolean.FALSE);
}
} else if (op == PutGetRemoveOp.GET) {
// Get other types of values
rawNBTResult = NBTTagCompoundHandle.T.get.raw.invoke(getRawHandle(), key);
} else if (op == PutGetRemoveOp.REMOVE || (op == PutGetRemoveOp.PUT && value == null)) {
// Remove other types of values
rawNBTResult = getRawData().remove(key);
} else if (op == PutGetRemoveOp.PUT) {
// Put other types of values
Object putValueNBT = NBTBaseHandle.createRawHandleForData(value);
rawNBTResult = NBTTagCompoundHandle.T.put.raw.invoke(getRawHandle(), key, putValueNBT);
}
// Failure fallback + convert raw NBT tags to their converted values
if (rawNBTResult == null) {
// failure or no previous value
return null;
} else if (type == null) {
return (T) wrapGetDataForHandle(rawNBTResult);
} else if (NBTBaseHandle.class.isAssignableFrom(type)) {
return Conversion.convert(NBTBaseHandle.createHandleForData(rawNBTResult), type, null);
} else if (CommonTag.class.isAssignableFrom(type)) {
return Conversion.convert(NBTBaseHandle.createHandleForData(rawNBTResult).toCommonTag(), type, null);
} else if (NBTBaseHandle.T.isAssignableFrom(type)) {
return Conversion.convert(rawNBTResult, type, null);
} else if (NBTTagCompoundHandle.T.isAssignableFrom(rawNBTResult)) {
return Conversion.convert(wrapRawData(NBTTagCompoundHandle.T.data.get(rawNBTResult)), type, null);
} else if (NBTTagListHandle.T.isAssignableFrom(rawNBTResult)) {
return Conversion.convert(wrapRawData(NBTTagListHandle.T.data.get(rawNBTResult)), type, null);
} else {
return Conversion.convert(NBTBaseHandle.getDataForHandle(rawNBTResult), type, null);
}
}
Aggregations