Phase 1 of Item conversion

This commit is contained in:
Matthew Miller 2018-06-11 23:45:19 +10:00
parent d12ad2548a
commit 07ade0b083
8 changed files with 6151 additions and 2 deletions

View File

@ -1,13 +1,16 @@
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven'
repositories {
mavenLocal()
maven { url "https://hub.spigotmc.org/nexus/content/groups/public" }
}
dependencies {
compile project(':worldedit-core')
compile 'com.sk89q:dummypermscompat:1.8'
// compile 'org.bukkit:bukkit:18w15a-R0.1-SNAPSHOT' // zzz
compile 'org.bukkit:bukkit:1.9.4-R0.1-SNAPSHOT' // zzz
testCompile 'org.mockito:mockito-core:1.9.0-rc1'
}

View File

@ -31,7 +31,10 @@ import java.util.Set;
/**
* An enum of types of items.
*
* {@Deprecated Please use {@link com.sk89q.worldedit.blocks.type.ItemType}}
*/
@Deprecated
public enum ItemType {
// Blocks

View File

@ -0,0 +1,67 @@
/*
* 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.blocks.type;
import com.sk89q.worldedit.world.registry.BundledItemData;
public class ItemType {
private String id;
public ItemType(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
/**
* Gets the legacy ID. Needed for legacy reasons.
*
* DO NOT USE THIS.
*
* @return legacy id or 0, if unknown
*/
@Deprecated
public int getLegacyId() {
Integer id = BundledItemData.getInstance().toLegacyId(this.id);
if (id != null) {
return id;
} else {
return 0;
}
}
@Deprecated
public com.sk89q.worldedit.blocks.ItemType getLegacyType() {
return com.sk89q.worldedit.blocks.ItemType.fromID(getLegacyId());
}
@Override
public int hashCode() {
return this.id.hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof ItemType && this.id.equals(((ItemType) obj).id);
}
}

View File

@ -0,0 +1,61 @@
/*
* 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.blocks.type;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
public class ItemTypes {
private ItemTypes() {
}
// TODO Add items.
private static final Map<String, ItemType> itemMapping = new HashMap<>();
static {
for (Field field : ItemTypes.class.getFields()) {
if (field.getType() == ItemType.class) {
try {
registerItem((ItemType) field.get(null));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
public static void registerItem(ItemType itemType) {
if (itemMapping.containsKey(itemType.getId()) && !itemType.getId().startsWith("minecraft:")) {
throw new IllegalArgumentException("Existing item with this ID already registered");
}
itemMapping.put(itemType.getId(), itemType);
}
@Nullable
public static ItemType getItemType(String id) {
return itemMapping.get(id);
}
}

View File

@ -0,0 +1,167 @@
/*
* 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.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.util.gson.VectorAdapter;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nullable;
/**
* Provides item data based on the built-in item database that is bundled
* with WorldEdit.
*
* <p>A new instance cannot be created. Use {@link #getInstance()} to get
* an instance.</p>
*
* <p>The data is read from a JSON file that is bundled with WorldEdit. If
* reading fails (which occurs when this class is first instantiated), then
* the methods will return {@code null}s for all items.</p>
*/
public class BundledItemData {
private static final Logger log = Logger.getLogger(BundledItemData.class.getCanonicalName());
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.
*/
private BundledItemData() {
try {
loadFromResource();
} catch (IOException e) {
log.log(Level.WARNING, "Failed to load the built-in item 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.create();
URL url = BundledItemData.class.getResource("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);
if (entry.legacyId >= 0) {
legacyMap.put(entry.legacyId, entry);
}
}
}
/**
* Return the entry for the given item ID.
*
* @param id the ID
* @return the entry, or null
*/
@Nullable
private ItemEntry findById(String id) {
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.
*
* @return the instance
*/
public static BundledItemData getInstance() {
return INSTANCE;
}
private static class ItemEntry {
private int legacyId; // -1 for items without legacy IDs.
private short legacyData;
private String id;
private String unlocalizedName;
private String localizedName;
private int maxDamage;
private int maxStackSize;
}
}

View File

@ -0,0 +1,50 @@
/*
* 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.sk89q.worldedit.blocks.BaseItem;
import com.sk89q.worldedit.blocks.type.ItemTypes;
import javax.annotation.Nullable;
/**
* A item registry that uses {@link BundledItemRegistry} to serve information
* about items.
*/
public class BundledItemRegistry implements ItemRegistry {
@Nullable
@Override
public BaseItem createFromId(String id) {
// TODO Fix legacy ID usage
return new BaseItem(ItemTypes.getItemType(id).getLegacyId());
}
@Nullable
@Override
public BaseItem createFromId(int legacyId) {
String id = BundledItemData.getInstance().fromLegacyId(legacyId);
if (id != null) {
return createFromId(id);
} else {
return null;
}
}
}

View File

@ -21,13 +21,13 @@ package com.sk89q.worldedit.world.registry;
/**
* An implementation of {@link WorldData} that converts legacy numeric IDs and
* a contains a built-in block database.
* a contains a built-in block and item database.
*/
public class BundledWorldData implements WorldData {
private static final BundledWorldData INSTANCE = new BundledWorldData();
private final BundledBlockRegistry blockRegistry = new BundledBlockRegistry();
private final NullItemRegistry itemRegistry = new NullItemRegistry();
private final BundledItemRegistry itemRegistry = new BundledItemRegistry();
private final NullEntityRegistry entityRegistry = new NullEntityRegistry();
private final NullBiomeRegistry biomeRegistry = new NullBiomeRegistry();

File diff suppressed because it is too large Load Diff