From 908a5784a18eda1f5325a5867c328b4b0abf7914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Kriv=C3=A1cs=20Schr=C3=B8der?= Date: Fri, 31 May 2019 20:16:39 +0200 Subject: [PATCH] Cache conversions to BaseBlock and fix leaky abstraction --- .../plotsquared/plot/object/LegacyPlotBlock.java | 12 ++++++++++++ .../plotsquared/plot/object/PlotBlock.java | 2 ++ .../plotsquared/plot/object/StringPlotBlock.java | 10 +++++++++- .../plot/util/block/BasicLocalBlockQueue.java | 10 +--------- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/LegacyPlotBlock.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/LegacyPlotBlock.java index be551b372..38407a52f 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/LegacyPlotBlock.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/LegacyPlotBlock.java @@ -1,6 +1,10 @@ package com.github.intellectualsites.plotsquared.plot.object; +import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockTypes; +import com.sk89q.worldedit.world.registry.LegacyMapper; import lombok.Getter; +import lombok.Setter; public class LegacyPlotBlock extends PlotBlock { @@ -15,6 +19,7 @@ public class LegacyPlotBlock extends PlotBlock { } } + @Setter private BaseBlock baseBlock = null; @Getter public final short id; @Getter public final byte data; @@ -27,6 +32,13 @@ public class LegacyPlotBlock extends PlotBlock { return this.id; } + @Override public BaseBlock getBaseBlock() { + if (baseBlock == null) { + baseBlock = LegacyMapper.getInstance().getBlockFromLegacy(id, data).toBaseBlock(); + } + return baseBlock; + } + @Override public boolean isAir() { return this.id == 0; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java index 79521908e..5b9de5852 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotBlock.java @@ -133,4 +133,6 @@ public abstract class PlotBlock implements ConfigurationSerializable { public abstract Object getRawId(); + public abstract BaseBlock getBaseBlock(); + } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/StringPlotBlock.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/StringPlotBlock.java index 706d1ae06..d4119ae92 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/StringPlotBlock.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/StringPlotBlock.java @@ -1,6 +1,7 @@ package com.github.intellectualsites.plotsquared.plot.object; import com.sk89q.worldedit.world.block.BaseBlock; +import com.sk89q.worldedit.world.block.BlockTypes; import lombok.Getter; import lombok.NonNull; import lombok.Setter; @@ -15,7 +16,7 @@ public class StringPlotBlock extends PlotBlock { private static final Map STRING_PLOT_BLOCK_CACHE = new HashMap<>(); @Getter private final String nameSpace; @Getter private final String itemId; - @Getter @Setter private BaseBlock baseBlock = null; + @Setter private BaseBlock baseBlock = null; private boolean isForeign = false; public StringPlotBlock(@NonNull final String nameSpace, @NonNull final String itemId) { @@ -79,6 +80,13 @@ public class StringPlotBlock extends PlotBlock { return this.getItemId(); } + @Override public BaseBlock getBaseBlock() { + if (baseBlock == null) { + baseBlock = BlockTypes.get(itemId).getDefaultState().toBaseBlock(); + } + return baseBlock; + } + @Override public boolean equals(Object obj) { if (this == obj) { return true; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/BasicLocalBlockQueue.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/BasicLocalBlockQueue.java index 551a9c44b..4ca62eb58 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/BasicLocalBlockQueue.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/util/block/BasicLocalBlockQueue.java @@ -121,15 +121,7 @@ public abstract class BasicLocalBlockQueue extends LocalBlockQueue { // Trying to mix PlotBlock and BaseBlock leads to all kinds of issues. // Since BaseBlock has more features than PlotBlock, simply convert // all PlotBlocks to BaseBlocks - if (id instanceof StringPlotBlock) { - StringPlotBlock stringPlotBlock = (StringPlotBlock) id; - return setBlock(x, y, z, BlockTypes.get(stringPlotBlock.getItemId()).getDefaultState().toBaseBlock()); - } else if (id instanceof LegacyPlotBlock) { - LegacyPlotBlock legacyPlotBlock = (LegacyPlotBlock) id; - return setBlock(x, y, z, LegacyMapper.getInstance().getBlockFromLegacy(legacyPlotBlock.getId(), legacyPlotBlock.getData()).toBaseBlock()); - } else { - throw new RuntimeException("Unknown PlotBock class: " + id.getClass().getName()); - } + return setBlock(x, y, z, id.getBaseBlock()); } @Override public final boolean setBiome(int x, int z, String biome) {