This commit is contained in:
Jesse Boyd
2020-01-07 00:09:49 +00:00
42 changed files with 1592 additions and 906 deletions

View File

@ -20,7 +20,6 @@
package com.sk89q.worldedit.world.block;
import com.sk89q.worldedit.WorldEdit;
import com.google.common.collect.Iterables;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.extent.Extent;
@ -33,20 +32,21 @@ import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.state.AbstractProperty;
import com.sk89q.worldedit.registry.state.Property;
import com.sk89q.worldedit.registry.state.PropertyKey;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.util.concurrency.LazyReference;
import com.sk89q.worldedit.world.item.ItemType;
import com.sk89q.worldedit.world.item.ItemTypes;
import com.sk89q.worldedit.world.registry.BlockMaterial;
import com.sk89q.worldedit.world.registry.LegacyMapper;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static com.google.common.base.Preconditions.checkArgument;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
public class BlockType implements FawePattern, Keyed {
@ -54,7 +54,11 @@ public class BlockType implements FawePattern, Keyed {
private final String id;
private final BlockTypesCache.Settings settings;
/*
private final LazyReference<Integer> legacyId = LazyReference.from(() -> computeLgacy(0));
private final LazyReference<Integer> legacyData = LazyReference.from(() -> computeLegacy(1));
*/
private Integer legacyCombinedId;
private boolean initItemType;
private ItemType itemType;
@ -103,6 +107,7 @@ public class BlockType implements FawePattern, Keyed {
return name;
}
}
/*
private BlockState computeDefaultState() {
@ -212,7 +217,7 @@ public class BlockType implements FawePattern, Keyed {
*/
AbstractProperty btp = this.settings.propertiesMap.get(prop.getName());
checkArgument(btp != null, "%s has no property named %s", this, prop.getName());
id = btp.modify(id, btp.getValueFor((String)value));
id = btp.modify(id, btp.getValueFor((String) value));
}
return withStateId(id);
}
@ -233,7 +238,7 @@ public class BlockType implements FawePattern, Keyed {
*/
@Nullable
public ItemType getItemType() {
if(!initItemType) {
if (!initItemType) {
initItemType = true;
itemType = ItemTypes.get(this.id);
}
@ -251,7 +256,7 @@ public class BlockType implements FawePattern, Keyed {
/**
* Gets the legacy ID. Needed for legacy reasons.
*
* <p>
* DO NOT USE THIS.
*
* @return legacy id or 0, if unknown
@ -264,7 +269,7 @@ public class BlockType implements FawePattern, Keyed {
/**
* The internal index of this type.
*
* <p>
* This number is not necessarily consistent across restarts.
*
* @return internal id
@ -288,7 +293,6 @@ public class BlockType implements FawePattern, Keyed {
return obj == this; // stop changing this to a shitty string comparison
}
@Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
return set.setBlock(extent, getDefaultState());
@ -307,14 +311,27 @@ public class BlockType implements FawePattern, Keyed {
return new SingleBlockTypeMask(extent, this);
}
@Deprecated
public int getLegacyId() {
Integer id = LegacyMapper.getInstance().getLegacyCombined(this.getDefaultState());
if (id != null) {
return id >> 4;
} else {
return 0;
return computeLegacy(0);
}
/**
* Gets the legacy data. Needed for legacy reasons.
* <p>
* DO NOT USE THIS.
*
* @return legacy data or 0, if unknown
*/
@Deprecated
public int getLegacyData() {
return computeLegacy(1);
}
private int computeLegacy(int index) {
if (this.legacyCombinedId == null) {
this.legacyCombinedId = LegacyMapper.getInstance().getLegacyCombined(this.getDefaultState());
}
return index == 0 ? legacyCombinedId >> 4 : legacyCombinedId & 15;
}
}

View File

@ -26,8 +26,11 @@ import com.sk89q.worldedit.extension.platform.Capability;
import com.sk89q.worldedit.registry.Keyed;
import com.sk89q.worldedit.registry.NamespacedRegistry;
import com.sk89q.worldedit.registry.RegistryItem;
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;
@ -37,6 +40,9 @@ public class ItemType implements RegistryItem, 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));
private BlockType blockType;
private boolean initBlockType;
private BaseItem defaultState;
@ -113,6 +119,15 @@ public class ItemType implements RegistryItem, Keyed {
return this.defaultState;
}
/**
* 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 final class BundledItemData {
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 @@ import javax.annotation.Nullable;
*/
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 class BundledItemRegistry implements ItemRegistry {
}
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

@ -36,6 +36,15 @@ 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);
/**
* Register all items
*/

View File

@ -21,21 +21,31 @@ package com.sk89q.worldedit.world.registry;
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
@ -49,172 +59,96 @@ public class PassthroughBlockMaterial implements BlockMaterial {
@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 int getLightOpacity() {
if (blockMaterial == null) {
return 0;
} else {
return blockMaterial.getLightOpacity();
}
return blockMaterial.getLightOpacity();
}
@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();
}
}

View File

@ -0,0 +1,45 @@
/*
* 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 static com.sk89q.worldedit.util.GuavaUtil.firstNonNull;
import javax.annotation.Nullable;
public class PassthroughItemMaterial implements ItemMaterial {
private static final ItemMaterial DEFAULT_MATERIAL = new SimpleItemMaterial(0, 0);
private final ItemMaterial itemMaterial;
public PassthroughItemMaterial(@Nullable ItemMaterial material) {
this.itemMaterial = firstNonNull(material, DEFAULT_MATERIAL);
}
@Override
public int getMaxStackSize() {
return itemMaterial.getMaxStackSize();
}
@Override
public int getMaxDamage() {
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;
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;
}
}