use of com.sk89q.worldedit.util.gson.VectorAdapter in project FastAsyncWorldEdit by IntellectualSites.
the class BundledBlockData method loadFromResource.
/**
* Attempt to load the data from file.
*
* @throws IOException thrown on I/O error
*/
private void loadFromResource() throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
// FAWE start
gsonBuilder.registerTypeAdapter(int.class, (JsonDeserializer<Integer>) (json, typeOfT, context) -> {
JsonPrimitive primitive = (JsonPrimitive) json;
if (primitive.isString()) {
String value = primitive.getAsString();
if (value.charAt(0) == '#') {
return Integer.parseInt(value.substring(1), 16);
}
return Integer.parseInt(value);
}
return primitive.getAsInt();
});
// FAWE end
Gson gson = gsonBuilder.create();
URL url = null;
final int dataVersion = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion();
if (dataVersion >= Constants.DATA_VERSION_MC_1_17) {
url = resourceLoader.getResource(BundledBlockData.class, "blocks.117.json");
} else if (dataVersion >= Constants.DATA_VERSION_MC_1_16) {
url = resourceLoader.getResource(BundledBlockData.class, "blocks.116.json");
} else if (dataVersion >= Constants.DATA_VERSION_MC_1_15) {
url = resourceLoader.getResource(BundledBlockData.class, "blocks.115.json");
}
if (url == null) {
url = resourceLoader.getResource(BundledBlockData.class, "blocks.json");
}
if (url == null) {
throw new IOException("Could not find blocks.json");
}
String data = Resources.toString(url, Charset.defaultCharset());
List<BlockEntry> entries = gson.fromJson(data, new TypeToken<List<BlockEntry>>() {
}.getType());
for (BlockEntry entry : entries) {
idMap.put(entry.id, entry);
}
}
use of com.sk89q.worldedit.util.gson.VectorAdapter in project FastAsyncWorldEdit by IntellectualSites.
the class BundledItemData method loadFromResource.
/**
* Attempt to load the data from file.
*
* @throws IOException thrown on I/O error
*/
private void loadFromResource() throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
Gson gson = gsonBuilder.create();
URL url = null;
final int dataVersion = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion();
if (dataVersion >= Constants.DATA_VERSION_MC_1_17) {
url = resourceLoader.getResource(BundledBlockData.class, "items.117.json");
} else if (dataVersion >= Constants.DATA_VERSION_MC_1_16) {
url = resourceLoader.getResource(BundledBlockData.class, "items.116.json");
} else if (dataVersion >= Constants.DATA_VERSION_MC_1_15) {
url = resourceLoader.getResource(BundledBlockData.class, "items.115.json");
}
if (url == null) {
url = resourceLoader.getResource(BundledBlockData.class, "items.json");
}
if (url == null) {
throw new IOException("Could not find items.json");
}
String data = Resources.toString(url, Charset.defaultCharset());
List<ItemEntry> entries = gson.fromJson(data, new TypeToken<List<ItemEntry>>() {
}.getType());
for (ItemEntry entry : entries) {
idMap.put(entry.id, entry);
}
}
use of com.sk89q.worldedit.util.gson.VectorAdapter in project FastAsyncWorldEdit by IntellectualSites.
the class LegacyMapper method loadFromResource.
/**
* Attempt to load the data from file.
*
* @throws IOException thrown on I/O error
*/
private void loadFromResource() throws IOException {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
Gson gson = gsonBuilder.disableHtmlEscaping().create();
URL url = resourceLoader.getResource(LegacyMapper.class, "legacy.json");
if (url == null) {
throw new IOException("Could not find legacy.json");
}
String data = Resources.toString(url, Charset.defaultCharset());
LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
}.getType());
DataFixer fixer = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataFixer();
ParserContext parserContext = new ParserContext();
parserContext.setPreferringWildcard(false);
parserContext.setRestricted(false);
// This is legacy. Don't match itself.
parserContext.setTryLegacy(false);
for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
String id = blockEntry.getKey();
final String value = blockEntry.getValue();
// FAWE start
Integer combinedId = getCombinedId(blockEntry.getKey());
blockEntries.put(id, value);
// FAWE end
BlockState state = null;
// FAWE start
try {
state = BlockState.get(null, blockEntry.getValue());
BlockType type = state.getBlockType();
if (type.hasProperty(PropertyKey.WATERLOGGED)) {
state = state.with(PropertyKey.WATERLOGGED, false);
}
} catch (InputParseException f) {
BlockFactory blockFactory = WorldEdit.getInstance().getBlockFactory();
// if fixer is available, try using that first, as some old blocks that were renamed share names with new blocks
if (fixer != null) {
try {
String newEntry = fixer.fixUp(DataFixer.FixTypes.BLOCK_STATE, value, Constants.DATA_VERSION_MC_1_13_2);
state = blockFactory.parseFromInput(newEntry, parserContext).toImmutableState();
} catch (InputParseException ignored) {
}
}
// if it's still null, the fixer was unavailable or failed
if (state == null) {
try {
state = blockFactory.parseFromInput(value, parserContext).toImmutableState();
} catch (InputParseException ignored) {
}
}
// if it's still null, both fixer and default failed
if (state == null) {
LOGGER.error("Unknown block: {}. Neither the DataFixer nor defaulting worked to recognize this block.", value);
} else {
// it's not null so one of them succeeded, now use it
blockToStringMap.put(state, id);
stringToBlockMap.put(id, state);
}
}
// FAWE start
if (state != null) {
blockArr[combinedId] = state.getInternalId();
blockStateToLegacyId4Data.put(state.getInternalId(), combinedId);
blockStateToLegacyId4Data.putIfAbsent(state.getInternalBlockTypeId(), combinedId);
}
}
for (int id = 0; id < 256; id++) {
int combinedId = id << 4;
int base = blockArr[combinedId];
if (base != 0) {
for (int data_ = 0; data_ < 16; data_++, combinedId++) {
if (blockArr[combinedId] == 0) {
blockArr[combinedId] = base;
}
}
}
}
for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
String id = itemEntry.getKey();
String value = itemEntry.getValue();
ItemType type = ItemTypes.get(value);
if (type == null && fixer != null) {
value = fixer.fixUp(DataFixer.FixTypes.ITEM_TYPE, value, Constants.DATA_VERSION_MC_1_13_2);
type = ItemTypes.get(value);
}
if (type == null) {
LOGGER.error("Unknown item: {}. Neither the DataFixer nor defaulting worked to recognize this item.", value);
} else {
try {
itemMap.put(getCombinedId(id), type);
} catch (Exception ignored) {
}
}
}
}
Aggregations