2014-07-09 21:14:17 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2018-08-15 04:02:51 +00:00
|
|
|
import com.google.gson.*;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
|
import com.sk89q.worldedit.Vector;
|
2018-08-12 14:03:07 +00:00
|
|
|
import com.sk89q.worldedit.blocks.BlockMaterial;
|
2014-07-09 21:14:17 +00:00
|
|
|
import com.sk89q.worldedit.util.gson.VectorAdapter;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2018-08-15 04:02:51 +00:00
|
|
|
import java.lang.reflect.Type;
|
2014-07-09 21:14:17 +00:00
|
|
|
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;
|
|
|
|
|
2018-07-17 07:31:07 +00:00
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
/**
|
|
|
|
* Provides block data based on the built-in block 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 blocks.</p>
|
|
|
|
*/
|
|
|
|
public class BundledBlockData {
|
|
|
|
|
|
|
|
private static final Logger log = Logger.getLogger(BundledBlockData.class.getCanonicalName());
|
2018-07-17 14:42:09 +00:00
|
|
|
private static BundledBlockData INSTANCE;
|
2014-07-09 21:14:17 +00:00
|
|
|
|
2018-06-14 06:35:56 +00:00
|
|
|
private final Map<String, BlockEntry> idMap = new HashMap<>();
|
2014-07-09 21:14:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance.
|
|
|
|
*/
|
|
|
|
private BundledBlockData() {
|
|
|
|
try {
|
|
|
|
loadFromResource();
|
2018-07-15 14:21:32 +00:00
|
|
|
} catch (Throwable e) {
|
2014-07-09 21:14:17 +00:00
|
|
|
log.log(Level.WARNING, "Failed to load the built-in block 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());
|
2018-08-15 04:02:51 +00:00
|
|
|
gsonBuilder.registerTypeAdapter(int.class, new JsonDeserializer<Integer>() {
|
|
|
|
@Override
|
|
|
|
public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
2014-07-09 21:14:17 +00:00
|
|
|
Gson gson = gsonBuilder.create();
|
|
|
|
URL url = BundledBlockData.class.getResource("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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the entry for the given block ID.
|
|
|
|
*
|
|
|
|
* @param id the ID
|
|
|
|
* @return the entry, or null
|
|
|
|
*/
|
|
|
|
@Nullable
|
2018-06-17 05:42:47 +00:00
|
|
|
public BlockEntry findById(String id) {
|
2018-06-14 01:55:02 +00:00
|
|
|
// If it has no namespace, assume minecraft.
|
|
|
|
if (!id.contains(":")) {
|
|
|
|
id = "minecraft:" + id;
|
|
|
|
}
|
2014-07-09 21:14:17 +00:00
|
|
|
return idMap.get(id);
|
|
|
|
}
|
|
|
|
|
2018-01-03 05:35:51 +00:00
|
|
|
/**
|
|
|
|
* Get the material properties for the given block.
|
|
|
|
*
|
|
|
|
* @param id the string ID
|
|
|
|
* @return the material's properties, or null
|
|
|
|
*/
|
|
|
|
@Nullable
|
|
|
|
public BlockMaterial getMaterialById(String id) {
|
|
|
|
BlockEntry entry = findById(id);
|
|
|
|
if (entry != null) {
|
|
|
|
return entry.material;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-09 21:14:17 +00:00
|
|
|
/**
|
|
|
|
* Get a singleton instance of this object.
|
|
|
|
*
|
|
|
|
* @return the instance
|
|
|
|
*/
|
|
|
|
public static BundledBlockData getInstance() {
|
2018-07-17 14:42:09 +00:00
|
|
|
if (INSTANCE == null) {
|
|
|
|
INSTANCE = new BundledBlockData();
|
|
|
|
}
|
2014-07-09 21:14:17 +00:00
|
|
|
return INSTANCE;
|
|
|
|
}
|
|
|
|
|
2018-06-17 05:42:47 +00:00
|
|
|
public static class BlockEntry {
|
2014-07-09 21:14:17 +00:00
|
|
|
private String id;
|
2018-06-17 05:42:47 +00:00
|
|
|
public String localizedName;
|
2014-07-09 21:14:17 +00:00
|
|
|
private SimpleBlockMaterial material = new SimpleBlockMaterial();
|
|
|
|
}
|
|
|
|
|
2018-08-12 14:03:07 +00:00
|
|
|
}
|