From c7f90ebcf2fce9ff15d6592c968ade342500e0f5 Mon Sep 17 00:00:00 2001 From: DalekCraft Date: Thu, 2 Dec 2021 19:07:39 -0500 Subject: [PATCH] Include mapColor in BlockMaterial and add more delegations to Forge/Fabric BlockMaterials --- .../worldedit/bukkit/BukkitBlockRegistry.java | 9 ++- .../worldedit/util/gson/ColorAdapter.java | 56 +++++++++++++++++++ .../world/registry/BlockMaterial.java | 7 +++ .../world/registry/BundledBlockData.java | 2 + .../registry/PassthroughBlockMaterial.java | 5 ++ .../world/registry/SimpleBlockMaterial.java | 18 ++++-- .../worldedit/fabric/FabricBlockMaterial.java | 53 ++++++++++++++++++ .../worldedit/forge/ForgeBlockMaterial.java | 54 ++++++++++++++++++ 8 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 worldedit-core/src/main/java/com/sk89q/worldedit/util/gson/ColorAdapter.java diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java index 4407ca3d69..34abe9c6d1 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/BukkitBlockRegistry.java @@ -28,10 +28,10 @@ import com.sk89q.worldedit.world.registry.PassthroughBlockMaterial; import org.bukkit.Material; +import javax.annotation.Nullable; import java.util.EnumMap; import java.util.Map; import java.util.OptionalInt; -import javax.annotation.Nullable; public class BukkitBlockRegistry extends BundledBlockRegistry { private final Map materialMap = new EnumMap<>(Material.class); @@ -92,6 +92,13 @@ public boolean isAir() { } } + // TODO Determine whether this would be the correct usage of isTransparent + /*@SuppressWarnings("deprecation") + @Override + public boolean isOpaque() { + return !material.isTransparent(); + }*/ + @Override public boolean isSolid() { return material.isSolid(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/util/gson/ColorAdapter.java b/worldedit-core/src/main/java/com/sk89q/worldedit/util/gson/ColorAdapter.java new file mode 100644 index 0000000000..c4ef47b8ee --- /dev/null +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/util/gson/ColorAdapter.java @@ -0,0 +1,56 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * 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 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.util.gson; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; + +import java.lang.reflect.Type; + +/** + * Deserializes hexadecimal {@link Integer}s from {@link String}s. + */ +public class ColorAdapter implements JsonDeserializer { + + @Override + public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { + String hexString = json.getAsJsonPrimitive().getAsString(); + + if (!hexString.startsWith("#")) { + hexString = "#" + hexString; + } + if (hexString.length() != 7) { + throw new JsonParseException("String does not have length 6"); + } + + System.out.println("String: " + hexString); + + int hex; + try { + hex = Integer.decode(hexString); + } catch (NumberFormatException e) { + throw new JsonParseException("String does not contain parseable integer", e); + } + + return hex; + } +} diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java index d280f2cc0e..228914bac1 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BlockMaterial.java @@ -94,6 +94,13 @@ public interface BlockMaterial { */ int getLightValue(); + /** + * Get the map color for this block. + * + * @return the map color + */ + int getMapColor(); + /** * Get whether this block breaks when it is pushed by a piston. * diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockData.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockData.java index e3a7e1bce7..2cc365ff8b 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockData.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/BundledBlockData.java @@ -28,6 +28,7 @@ import com.sk89q.worldedit.internal.Constants; import com.sk89q.worldedit.internal.util.LogManagerCompat; import com.sk89q.worldedit.math.Vector3; +import com.sk89q.worldedit.util.gson.ColorAdapter; import com.sk89q.worldedit.util.gson.VectorAdapter; import com.sk89q.worldedit.util.io.ResourceLoader; import org.apache.logging.log4j.Logger; @@ -80,6 +81,7 @@ private BundledBlockData() { private void loadFromResource() throws IOException { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter()); + gsonBuilder.registerTypeAdapter(Integer.class, new ColorAdapter()); Gson gson = gsonBuilder.create(); URL url = null; final int dataVersion = WorldEdit.getInstance().getPlatformManager().queryCapability(Capability.WORLD_EDITING).getDataVersion(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java index e868276a63..24d61255be 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/PassthroughBlockMaterial.java @@ -93,6 +93,11 @@ public int getLightValue() { return blockMaterial.getLightValue(); } + @Override + public int getMapColor() { + return blockMaterial.getMapColor(); + } + @Override public boolean isFragileWhenPushed() { return blockMaterial.isFragileWhenPushed(); diff --git a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java index de70a38619..6ce6cf9552 100644 --- a/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java +++ b/worldedit-core/src/main/java/com/sk89q/worldedit/world/registry/SimpleBlockMaterial.java @@ -31,6 +31,7 @@ class SimpleBlockMaterial implements BlockMaterial { private float resistance; private float slipperiness; private int lightValue; + private int mapColor; private boolean fragileWhenPushed; private boolean unpushable; private boolean ticksRandomly; @@ -131,6 +132,15 @@ public void setLightValue(int lightValue) { this.lightValue = lightValue; } + @Override + public int getMapColor() { + return mapColor; + } + + public void setMapColor(int mapColor) { + this.mapColor = mapColor; + } + @Override public boolean isFragileWhenPushed() { return fragileWhenPushed; @@ -190,8 +200,8 @@ public boolean isReplacedDuringPlacement() { return replacedDuringPlacement; } - public void setTranslucent(boolean isTranslucent) { - this.isTranslucent = isTranslucent; + public void setReplacedDuringPlacement(boolean replacedDuringPlacement) { + this.replacedDuringPlacement = replacedDuringPlacement; } @Override @@ -199,8 +209,8 @@ public boolean isTranslucent() { return this.isTranslucent; } - public void setReplacedDuringPlacement(boolean replacedDuringPlacement) { - this.replacedDuringPlacement = replacedDuringPlacement; + public void setTranslucent(boolean isTranslucent) { + this.isTranslucent = isTranslucent; } @Override diff --git a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockMaterial.java b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockMaterial.java index ae420c10f1..55ab63af6e 100644 --- a/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockMaterial.java +++ b/worldedit-fabric/src/main/java/com/sk89q/worldedit/fabric/FabricBlockMaterial.java @@ -27,6 +27,7 @@ import javax.annotation.Nullable; +// TODO Finish delegating all methods /** * Fabric block material that pulls as much info as possible from the Minecraft * Material, and passes the rest to another implementation, typically the @@ -48,11 +49,22 @@ public boolean isAir() { return delegate == Material.AIR || super.isAir(); } + @Override + public boolean isFullCube() { + // return block.isFullCube(); + return super.isFullCube(); + } + @Override public boolean isOpaque() { return delegate.blocksLight(); } + @Override + public boolean isPowerSource() { + return block.emitsRedstonePower(); + } + @Override public boolean isLiquid() { return delegate.isLiquid(); @@ -63,6 +75,31 @@ public boolean isSolid() { return delegate.isSolid(); } + @Override + public float getHardness() { + return block.getBlock().getHardness(); + } + + @Override + public float getResistance() { + return block.getBlock().getBlastResistance(); + } + + @Override + public float getSlipperiness() { + return block.getBlock().getSlipperiness(); + } + + @Override + public int getLightValue() { + return block.getLuminance(); + } + + @Override + public int getMapColor() { + return delegate.getColor().color; + } + @Override public boolean isFragileWhenPushed() { return delegate.getPistonBehavior() == PistonBehavior.DESTROY; @@ -73,6 +110,11 @@ public boolean isUnpushable() { return delegate.getPistonBehavior() == PistonBehavior.BLOCK; } + @Override + public boolean isTicksRandomly() { + return block.hasRandomTicks(); + } + @Override public boolean isMovementBlocker() { return delegate.blocksMovement(); @@ -93,4 +135,15 @@ public boolean isReplacedDuringPlacement() { return delegate.isReplaceable(); } + @Override + public boolean isTranslucent() { + // return block.isTranslucent(); + return super.isTranslucent(); + } + + @Override + public boolean hasContainer() { + return block.hasBlockEntity(); + } + } diff --git a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockMaterial.java b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockMaterial.java index e749965756..048bd63b56 100644 --- a/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockMaterial.java +++ b/worldedit-forge/src/main/java/com/sk89q/worldedit/forge/ForgeBlockMaterial.java @@ -27,6 +27,7 @@ import javax.annotation.Nullable; +// TODO Finish delegating all methods /** * Forge block material that pulls as much info as possible from the Minecraft * Material, and passes the rest to another implementation, typically the @@ -48,11 +49,22 @@ public boolean isAir() { return delegate == Material.AIR || super.isAir(); } + @Override + public boolean isFullCube() { + // return block.isCollisionShapeFullBlock(); + return super.isFullCube(); + } + @Override public boolean isOpaque() { return delegate.isSolidBlocking(); } + @Override + public boolean isPowerSource() { + return block.isSignalSource(); + } + @Override public boolean isLiquid() { return delegate.isLiquid(); @@ -63,6 +75,33 @@ public boolean isSolid() { return delegate.isSolid(); } + @Override + public float getHardness() { + return block.getBlock().defaultDestroyTime(); + } + + @Override + public float getResistance() { + // return block.getBlock().getExplosionResistance(); + return super.getResistance(); + } + + @Override + public float getSlipperiness() { + return block.getBlock().getFriction(); + } + + @Override + public int getLightValue() { + // return block.getLightEmission(); + return super.getLightValue(); + } + + @Override + public int getMapColor() { + return delegate.getColor().col; + } + @Override public boolean isFragileWhenPushed() { return delegate.getPushReaction() == PushReaction.DESTROY; @@ -73,6 +112,11 @@ public boolean isUnpushable() { return delegate.getPushReaction() == PushReaction.BLOCK; } + @Override + public boolean isTicksRandomly() { + return block.isRandomlyTicking(); + } + @Override public boolean isMovementBlocker() { return delegate.blocksMotion(); @@ -93,4 +137,14 @@ public boolean isReplacedDuringPlacement() { return delegate.isReplaceable(); } + @Override + public boolean isTranslucent() { + return super.isTranslucent(); + } + + @Override + public boolean hasContainer() { + return block.hasBlockEntity(); + } + }