Skip to content
Permalink
Browse files

Merge pull request #541 from EngineHub/feature/item-registry-enchance…

…ments

Add a ItemMaterial for use by CraftBook & CommandBook
  • Loading branch information
octylFractal committed Jan 2, 2020
2 parents c5fd531 + b0ba9e0 commit 39d3ad0e771401f57c3fab3fc67d1c028f2cdd3f
@@ -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);
}
@@ -21,182 +21,119 @@

import javax.annotation.Nullable;

import static com.sk89q.worldedit.util.GuavaUtil.firstNonNull;

public class PassthroughBlockMaterial implements BlockMaterial {

@Nullable private final BlockMaterial blockMaterial;
private static final SimpleBlockMaterial DEFAULT_MATERIAL = new SimpleBlockMaterial();
static {
DEFAULT_MATERIAL.setFullCube(true);
DEFAULT_MATERIAL.setOpaque(true);
DEFAULT_MATERIAL.setSolid(true);
DEFAULT_MATERIAL.setTicksRandomly(true);
DEFAULT_MATERIAL.setMovementBlocker(true);
DEFAULT_MATERIAL.setBurnable(true);
DEFAULT_MATERIAL.setToolRequired(true);
}

private final BlockMaterial blockMaterial;

public PassthroughBlockMaterial(@Nullable BlockMaterial material) {
this.blockMaterial = material;
this.blockMaterial = firstNonNull(material, DEFAULT_MATERIAL);
}

@Override
public boolean isAir() {
if (blockMaterial == null) {
return false;
} else {
return blockMaterial.isAir();
}
return blockMaterial.isAir();
}

@Override
public boolean isFullCube() {
if (blockMaterial == null) {
return true;
} else {
return blockMaterial.isFullCube();
}
return blockMaterial.isFullCube();
}

@Override
public boolean isOpaque() {
if (blockMaterial == null) {
return true;
} else {
return blockMaterial.isOpaque();
}
return blockMaterial.isOpaque();
}

@Override
public boolean isPowerSource() {
if (blockMaterial == null) {
return false;
} else {
return blockMaterial.isPowerSource();
}
return blockMaterial.isPowerSource();
}

@Override
public boolean isLiquid() {
if (blockMaterial == null) {
return false;
} else {
return blockMaterial.isLiquid();
}
return blockMaterial.isLiquid();
}

@Override
public boolean isSolid() {
if (blockMaterial == null) {
return true;
} else {
return blockMaterial.isSolid();
}
return blockMaterial.isSolid();
}

@Override
public float getHardness() {
if (blockMaterial == null) {
return 0;
} else {
return blockMaterial.getHardness();
}
return blockMaterial.getHardness();
}

@Override
public float getResistance() {
if (blockMaterial == null) {
return 0;
} else {
return blockMaterial.getResistance();
}
return blockMaterial.getResistance();
}

@Override
public float getSlipperiness() {
if (blockMaterial == null) {
return 0;
} else {
return blockMaterial.getSlipperiness();
}
return blockMaterial.getSlipperiness();
}

@Override
public int getLightValue() {
if (blockMaterial == null) {
return 0;
} else {
return blockMaterial.getLightValue();
}
return blockMaterial.getLightValue();
}

@Override
public boolean isFragileWhenPushed() {
if (blockMaterial == null) {
return false;
} else {
return blockMaterial.isFragileWhenPushed();
}
return blockMaterial.isFragileWhenPushed();
}

@Override
public boolean isUnpushable() {
if (blockMaterial == null) {
return false;
} else {
return blockMaterial.isUnpushable();
}
return blockMaterial.isUnpushable();
}

@Override
public boolean isTicksRandomly() {
if (blockMaterial == null) {
return true;
} else {
return blockMaterial.isTicksRandomly();
}
return blockMaterial.isTicksRandomly();
}

@Override
public boolean isMovementBlocker() {
if (blockMaterial == null) {
return true;
} else {
return blockMaterial.isMovementBlocker();
}
return blockMaterial.isMovementBlocker();
}

@Override
public boolean isBurnable() {
if (blockMaterial == null) {
return true;
} else {
return blockMaterial.isBurnable();
}
return blockMaterial.isBurnable();
}

@Override
public boolean isToolRequired() {
if (blockMaterial == null) {
return true;
} else {
return blockMaterial.isToolRequired();
}
return blockMaterial.isToolRequired();
}

@Override
public boolean isReplacedDuringPlacement() {
if (blockMaterial == null) {
return false;
} else {
return blockMaterial.isReplacedDuringPlacement();
}
return blockMaterial.isReplacedDuringPlacement();
}

@Override
public boolean isTranslucent() {
if (blockMaterial == null) {
return !isOpaque();
} else {
return blockMaterial.isTranslucent();
}
return blockMaterial.isTranslucent();
}

@Override
public boolean hasContainer() {
if (blockMaterial == null) {
return false;
} else {
return blockMaterial.hasContainer();
}
return blockMaterial.hasContainer();
}
}

0 comments on commit 39d3ad0

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