mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-04 03:56:41 +00:00
Setup a legacy mapper system. The file does not exist yet.
This commit is contained in:
@ -42,16 +42,6 @@ public interface BlockRegistry {
|
||||
@Nullable
|
||||
BlockState createFromId(String id);
|
||||
|
||||
/**
|
||||
* Create a new block using its legacy numeric ID.
|
||||
*
|
||||
* @param id the id
|
||||
* @return the block, which may be null if no block exists
|
||||
*/
|
||||
@Nullable
|
||||
@Deprecated
|
||||
BlockState createFromId(int id);
|
||||
|
||||
/**
|
||||
* Get the material for the given block.
|
||||
*
|
||||
|
@ -56,7 +56,6 @@ public class BundledBlockData {
|
||||
private static final BundledBlockData INSTANCE = new BundledBlockData();
|
||||
|
||||
private final Map<String, BlockEntry> idMap = new HashMap<>();
|
||||
private final Map<Integer, BlockEntry> legacyMap = new HashMap<>(); // Trove usage removed temporarily
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
@ -87,7 +86,7 @@ public class BundledBlockData {
|
||||
|
||||
for (BlockEntry entry : entries) {
|
||||
idMap.put(entry.id, entry);
|
||||
legacyMap.put(entry.legacyId, entry);
|
||||
entry.postDeserialization();
|
||||
}
|
||||
}
|
||||
|
||||
@ -106,49 +105,6 @@ public class BundledBlockData {
|
||||
return idMap.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the entry for the given block legacy numeric ID.
|
||||
*
|
||||
* @param id the ID
|
||||
* @return the entry, or null
|
||||
*/
|
||||
@Nullable
|
||||
private BlockEntry findById(int id) {
|
||||
return legacyMap.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given string ID to a legacy numeric ID.
|
||||
*
|
||||
* @param id the ID
|
||||
* @return the legacy ID, which may be null if the block does not have a legacy ID
|
||||
*/
|
||||
@Nullable
|
||||
public Integer toLegacyId(String id) {
|
||||
BlockEntry entry = findById(id);
|
||||
if (entry != null) {
|
||||
return entry.legacyId;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given legacy numeric ID to a string ID.
|
||||
*
|
||||
* @param id the legacy ID
|
||||
* @return the ID, which may be null if the block does not have a ID
|
||||
*/
|
||||
@Nullable
|
||||
public String fromLegacyId(Integer id) {
|
||||
BlockEntry entry = findById(id);
|
||||
if (entry != null) {
|
||||
return entry.id;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the material properties for the given block.
|
||||
*
|
||||
@ -191,13 +147,18 @@ public class BundledBlockData {
|
||||
}
|
||||
|
||||
public static class BlockEntry {
|
||||
private int legacyId;
|
||||
private String id;
|
||||
private String unlocalizedName;
|
||||
public String localizedName;
|
||||
private List<String> aliases;
|
||||
public Map<String, SimpleState> states = new HashMap<>();
|
||||
private SimpleBlockMaterial material = new SimpleBlockMaterial();
|
||||
|
||||
void postDeserialization() {
|
||||
for (Map.Entry<String, SimpleState> state : states.entrySet()) {
|
||||
state.getValue().setName(state.getKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -41,17 +41,6 @@ public class BundledBlockRegistry implements BlockRegistry {
|
||||
return BlockTypes.getBlockType(id).getDefaultState();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BlockState createFromId(int legacyId) {
|
||||
String id = BundledBlockData.getInstance().fromLegacyId(legacyId);
|
||||
if (id != null) {
|
||||
return createFromId(id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BlockMaterial getMaterial(BaseBlock block) {
|
||||
|
@ -54,7 +54,6 @@ public class BundledItemData {
|
||||
private static final BundledItemData INSTANCE = new BundledItemData();
|
||||
|
||||
private final Map<String, ItemEntry> idMap = new HashMap<>();
|
||||
private final Map<Integer, ItemEntry> legacyMap = new HashMap<>(); // Trove usage removed temporarily
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
@ -85,9 +84,6 @@ public class BundledItemData {
|
||||
|
||||
for (ItemEntry entry : entries) {
|
||||
idMap.put(entry.id, entry);
|
||||
if (entry.legacyId >= 0) {
|
||||
legacyMap.put(entry.legacyId, entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -97,7 +93,8 @@ public class BundledItemData {
|
||||
* @param id the ID
|
||||
* @return the entry, or null
|
||||
*/
|
||||
@Nullable public ItemEntry findById(String id) {
|
||||
@Nullable
|
||||
public ItemEntry findById(String id) {
|
||||
// If it has no namespace, assume minecraft.
|
||||
if (!id.contains(":")) {
|
||||
id = "minecraft:" + id;
|
||||
@ -105,49 +102,6 @@ public class BundledItemData {
|
||||
return idMap.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the entry for the given item legacy numeric ID.
|
||||
*
|
||||
* @param id the ID
|
||||
* @return the entry, or null
|
||||
*/
|
||||
@Nullable
|
||||
private ItemEntry findById(int id) {
|
||||
return legacyMap.get(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given string ID to a legacy numeric ID.
|
||||
*
|
||||
* @param id the ID
|
||||
* @return the legacy ID, which may be null if the item does not have a legacy ID
|
||||
*/
|
||||
@Nullable
|
||||
public Integer toLegacyId(String id) {
|
||||
ItemEntry entry = findById(id);
|
||||
if (entry != null) {
|
||||
return entry.legacyId;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the given legacy numeric ID to a string ID.
|
||||
*
|
||||
* @param id the legacy ID
|
||||
* @return the ID, which may be null if the item does not have a ID
|
||||
*/
|
||||
@Nullable
|
||||
public String fromLegacyId(Integer id) {
|
||||
ItemEntry entry = findById(id);
|
||||
if (entry != null) {
|
||||
return entry.id;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a singleton instance of this object.
|
||||
*
|
||||
@ -158,8 +112,6 @@ public class BundledItemData {
|
||||
}
|
||||
|
||||
public static class ItemEntry {
|
||||
private int legacyId; // -1 for items without legacy IDs.
|
||||
private short legacyData;
|
||||
private String id;
|
||||
private String unlocalizedName;
|
||||
public String localizedName;
|
||||
|
@ -35,15 +35,4 @@ public class BundledItemRegistry implements ItemRegistry {
|
||||
public BaseItem createFromId(String id) {
|
||||
return new BaseItem(ItemTypes.getItemType(id));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BaseItem createFromId(int legacyId) {
|
||||
String id = BundledItemData.getInstance().fromLegacyId(legacyId);
|
||||
if (id != null) {
|
||||
return createFromId(id);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,13 +34,4 @@ public interface ItemRegistry {
|
||||
@Nullable
|
||||
BaseItem createFromId(String id);
|
||||
|
||||
/**
|
||||
* Create a new item using its legacy numeric ID.
|
||||
*
|
||||
* @param id the id
|
||||
* @return the item, which may be null if no item exists
|
||||
*/
|
||||
@Nullable
|
||||
BaseItem createFromId(int id);
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,152 @@
|
||||
/*
|
||||
* WorldEdit, a Minecraft world manipulation toolkit
|
||||
* Copyright (C) sk89q <http://www.sk89q.com>
|
||||
* Copyright (C) WorldEdit team and contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU Lesser General Public License as published by the
|
||||
* Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sk89q.worldedit.world.registry;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.google.common.io.Resources;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.WorldEdit;
|
||||
import com.sk89q.worldedit.blocks.type.BlockState;
|
||||
import com.sk89q.worldedit.blocks.type.ItemType;
|
||||
import com.sk89q.worldedit.blocks.type.ItemTypes;
|
||||
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||
import com.sk89q.worldedit.util.gson.VectorAdapter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
public class LegacyMapper {
|
||||
|
||||
private static final Logger log = Logger.getLogger(LegacyMapper.class.getCanonicalName());
|
||||
private static final LegacyMapper INSTANCE = new LegacyMapper();
|
||||
|
||||
private BiMap<String, BlockState> blockMap = HashBiMap.create();
|
||||
private BiMap<String, ItemType> itemMap = HashBiMap.create();
|
||||
|
||||
/**
|
||||
* Create a new instance.
|
||||
*/
|
||||
private LegacyMapper() {
|
||||
try {
|
||||
loadFromResource();
|
||||
} catch (IOException e) {
|
||||
log.log(Level.WARNING, "Failed to load the built-in legacy id registry", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(Vector.class, new VectorAdapter());
|
||||
Gson gson = gsonBuilder.disableHtmlEscaping().create();
|
||||
URL url = LegacyMapper.class.getResource("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());
|
||||
|
||||
ParserContext parserContext = new ParserContext();
|
||||
parserContext.setPreferringWildcard(false);
|
||||
parserContext.setRestricted(false);
|
||||
|
||||
for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
|
||||
try {
|
||||
blockMap.put(blockEntry.getKey(),
|
||||
(BlockState) WorldEdit.getInstance().getBlockFactory().parseFromInput(blockEntry.getValue(), parserContext));
|
||||
} catch (Exception e) {
|
||||
log.warning("Unknown block: " + blockEntry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
|
||||
try {
|
||||
itemMap.put(itemEntry.getKey(), ItemTypes.getItemType(itemEntry.getValue()));
|
||||
} catch (Exception e) {
|
||||
log.warning("Unknown item: " + itemEntry.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ItemType getItemFromLegacy(int legacyId) {
|
||||
return itemMap.get(legacyId + ":0");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ItemType getItemFromLegacy(int legacyId, int data) {
|
||||
return itemMap.get(legacyId + ":" + data);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public int[] getLegacyFromItem(ItemType itemType) {
|
||||
if (!itemMap.inverse().containsKey(itemType)) {
|
||||
return null;
|
||||
} else {
|
||||
String value = itemMap.inverse().get(itemType);
|
||||
return Arrays.stream(value.split(":")).mapToInt(Integer::parseInt).toArray();
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BlockState getBlockFromLegacy(int legacyId) {
|
||||
return blockMap.get(legacyId + ":0");
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BlockState getBlockFromLegacy(int legacyId, int data) {
|
||||
return blockMap.get(legacyId + ":" + data);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public int[] getLegacyFromBlock(BlockState blockState) {
|
||||
if (!blockMap.inverse().containsKey(blockState)) {
|
||||
return null;
|
||||
} else {
|
||||
String value = blockMap.inverse().get(blockState);
|
||||
return Arrays.stream(value.split(":")).mapToInt(Integer::parseInt).toArray();
|
||||
}
|
||||
}
|
||||
|
||||
public static LegacyMapper getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@SuppressWarnings({"MismatchedQueryAndUpdateOfCollection", "unused"})
|
||||
private static class LegacyDataFile {
|
||||
private Map<String, String> blocks;
|
||||
private Map<String, String> items;
|
||||
}
|
||||
}
|
@ -29,6 +29,7 @@ import javax.annotation.Nullable;
|
||||
|
||||
public class SimpleState<T extends SimpleStateValue> implements State<T> {
|
||||
|
||||
private String name;
|
||||
private List<T> values;
|
||||
|
||||
/**
|
||||
@ -37,9 +38,21 @@ public class SimpleState<T extends SimpleStateValue> implements State<T> {
|
||||
* @param values The values
|
||||
*/
|
||||
public SimpleState(List<T> values) {
|
||||
this.name = "Unknown";
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method for name setting post-deserialise. Do not use.
|
||||
*/
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> getValues() {
|
||||
return Collections.unmodifiableList(values);
|
||||
|
@ -33,6 +33,13 @@ import javax.annotation.Nullable;
|
||||
*/
|
||||
public interface State<T extends SimpleStateValue> {
|
||||
|
||||
/**
|
||||
* Returns the name of this state.
|
||||
*
|
||||
* @return The state name
|
||||
*/
|
||||
String getName();
|
||||
|
||||
/**
|
||||
* Return a list of available values for this state.
|
||||
*
|
||||
|
Reference in New Issue
Block a user