Add a ItemMaterial for use by CraftBook & CommandBook

This commit is contained in:
Wyatt Childers 2019-12-30 22:39:59 -05:00
parent 79c11c5ee9
commit 1c3277648f
7 changed files with 178 additions and 2 deletions

View File

@ -23,8 +23,11 @@
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.util.concurrency.LazyReference;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.ItemMaterial;
import javax.annotation.Nullable;
@ -34,6 +37,9 @@ public class ItemType implements Keyed {
private String id;
private String name;
private final LazyReference<ItemMaterial> itemMaterial
= LazyReference.from(() -> WorldEdit.getInstance().getPlatformManager()
.queryCapability(Capability.GAME_HOOKS).getRegistries().getItemRegistry().getMaterial(this));
public ItemType(String id) {
// If it has no namespace, assume minecraft.
@ -84,6 +90,15 @@ public BlockType getBlockType() {
return BlockTypes.get(this.id);
}
/**
* Get the material for this ItemType.
*
* @return The material
*/
public ItemMaterial getMaterial() {
return itemMaterial.getValue();
}
@Override
public String toString() {
return getId();

View File

@ -114,6 +114,23 @@ public ItemEntry findById(String id) {
return idMap.get(id);
}
/**
* Get the material properties for the given item.
*
* @param id the string ID
* @return the material's properties, or null
*/
@Nullable
public ItemMaterial getMaterialById(String id) {
ItemEntry entry = findById(id);
if (entry != null) {
// FIXME: This should probably just be part of the JSON itself
return new SimpleItemMaterial(entry.maxStackSize, entry.maxDamage);
} else {
return null;
}
}
/**
* Get a singleton instance of this object.
*

View File

@ -29,14 +29,18 @@
*/
public class BundledItemRegistry implements ItemRegistry {
private BundledItemData.ItemEntry getEntryById(ItemType itemType) {
return BundledItemData.getInstance().findById(itemType.getId());
}
@Nullable
@Override
public String getName(ItemType itemType) {
String id = itemType.getId();
BundledItemData.ItemEntry itemEntry = BundledItemData.getInstance().findById(id);
BundledItemData.ItemEntry itemEntry = getEntryById(itemType);
if (itemEntry != null) {
String localized = itemEntry.localizedName;
if (localized.equals("Air")) {
String id = itemType.getId();
int c = id.indexOf(':');
return c < 0 ? id : id.substring(c + 1);
}
@ -44,4 +48,10 @@ public String getName(ItemType itemType) {
}
return null;
}
@Nullable
@Override
public ItemMaterial getMaterial(ItemType itemType) {
return new PassthroughItemMaterial(BundledItemData.getInstance().getMaterialById(itemType.getId()));
}
}

View File

@ -0,0 +1,36 @@
/*
* 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;
public interface ItemMaterial {
/**
* Gets the the maximum quantity of this item that can be in a single stack.
*
* @return the maximum quantity
*/
int getMaxStackSize();
/**
* Gets the the maximum damage this item can take before being broken.
*
* @return the maximum damage, or 0 if not applicable
*/
int getMaxDamage();
}

View File

@ -34,4 +34,12 @@ public interface ItemRegistry {
@Nullable
String getName(ItemType itemType);
/**
* Get the material for the given item.
*
* @param itemType the item
* @return the material, or null if the material information is not known
*/
@Nullable
ItemMaterial getMaterial(ItemType itemType);
}

View File

@ -0,0 +1,49 @@
/*
* 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 javax.annotation.Nullable;
public class PassthroughItemMaterial implements ItemMaterial {
@Nullable private final ItemMaterial itemMaterial;
public PassthroughItemMaterial(@Nullable ItemMaterial material) {
this.itemMaterial = material;
}
@Override
public int getMaxStackSize() {
if (itemMaterial == null) {
return 0;
} else {
return itemMaterial.getMaxStackSize();
}
}
@Override
public int getMaxDamage() {
if (itemMaterial == null) {
return 0;
} else {
return itemMaterial.getMaxDamage();
}
}
}

View File

@ -0,0 +1,41 @@
/*
* 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;
public class SimpleItemMaterial implements ItemMaterial {
private int maxStackSize;
private int maxDamage;
public SimpleItemMaterial(int maxStackSize, int maxDamage) {
this.maxStackSize = maxStackSize;
this.maxDamage = maxDamage;
}
@Override
public int getMaxStackSize() {
return maxStackSize;
}
@Override
public int getMaxDamage() {
return maxDamage;
}
}