Skip to content
Permalink
Browse files

Add a ItemMaterial for use by CraftBook & CommandBook

  • Loading branch information
DarkArc committed Dec 31, 2019
1 parent 79c11c5 commit 1c3277648fd63f060032c5de7256dcd59a489b9d
@@ -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 @@

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();
@@ -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.
*
@@ -29,19 +29,29 @@
*/
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);
}
return localized;
}
return null;
}

@Nullable
@Override
public ItemMaterial getMaterial(ItemType itemType) {
return new PassthroughItemMaterial(BundledItemData.getInstance().getMaterialById(itemType.getId()));
}
}
@@ -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();
}
@@ -34,4 +34,12 @@
@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);
}
@@ -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();
}
}
}
@@ -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;
}
}

0 comments on commit 1c32776

Please sign in to comment.
You can’t perform that action at this time.