Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include mapColor in BlockMaterial and add more delegations to Forge/Fabric BlockMaterials #1956

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Material, BukkitBlockMaterial> 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();
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 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 <https://www.gnu.org/licenses/>.
*/

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<Integer> {

@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;
}
}
Original file line number Diff line number Diff line change
@@ -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.
*
Original file line number Diff line number Diff line change
@@ -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();
Original file line number Diff line number Diff line change
@@ -93,6 +93,11 @@ public int getLightValue() {
return blockMaterial.getLightValue();
}

@Override
public int getMapColor() {
return blockMaterial.getMapColor();
}

@Override
public boolean isFragileWhenPushed() {
return blockMaterial.isFragileWhenPushed();
Original file line number Diff line number Diff line change
@@ -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,17 +200,17 @@ public boolean isReplacedDuringPlacement() {
return replacedDuringPlacement;
}

public void setTranslucent(boolean isTranslucent) {
this.isTranslucent = isTranslucent;
public void setReplacedDuringPlacement(boolean replacedDuringPlacement) {
this.replacedDuringPlacement = replacedDuringPlacement;
}

@Override
public boolean isTranslucent() {
return this.isTranslucent;
}

public void setReplacedDuringPlacement(boolean replacedDuringPlacement) {
this.replacedDuringPlacement = replacedDuringPlacement;
public void setTranslucent(boolean isTranslucent) {
this.isTranslucent = isTranslucent;
}

@Override
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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();
}

}