From e724aa8cafe79fd898fe4d3a6657dc7a3c871584 Mon Sep 17 00:00:00 2001 From: sauilitired Date: Wed, 19 Dec 2018 18:24:35 +0100 Subject: [PATCH] Fix PlotItem. --- .../plotsquared/bukkit/util/BukkitUtil.java | 43 +++++++++++++++---- .../plotsquared/plot/object/PlotBlock.java | 17 +++++++- .../plot/object/schematic/PlotItem.java | 12 +++--- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java index 8314cbda5..d4f8b0931 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitUtil.java @@ -1,10 +1,28 @@ package com.github.intellectualsites.plotsquared.bukkit.util; import com.github.intellectualsites.plotsquared.bukkit.object.BukkitPlayer; +import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.C; -import com.github.intellectualsites.plotsquared.plot.object.*; +import com.github.intellectualsites.plotsquared.plot.object.LegacyPlotBlock; +import com.github.intellectualsites.plotsquared.plot.object.Location; +import com.github.intellectualsites.plotsquared.plot.object.Plot; +import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; +import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; +import com.github.intellectualsites.plotsquared.plot.object.RegionWrapper; +import com.github.intellectualsites.plotsquared.plot.object.StringPlotBlock; import com.github.intellectualsites.plotsquared.plot.object.schematic.PlotItem; -import com.github.intellectualsites.plotsquared.plot.util.*; +import com.github.intellectualsites.plotsquared.plot.util.MainUtil; +import com.github.intellectualsites.plotsquared.plot.util.MathMan; +import com.github.intellectualsites.plotsquared.plot.util.StringComparison; +import com.github.intellectualsites.plotsquared.plot.util.UUIDHandler; +import com.github.intellectualsites.plotsquared.plot.util.WorldUtil; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Locale; +import java.util.Set; +import javax.annotation.Nullable; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -19,10 +37,12 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; -import org.bukkit.material.*; - -import javax.annotation.Nullable; -import java.util.*; +import org.bukkit.material.MaterialData; +import org.bukkit.material.Sandstone; +import org.bukkit.material.Step; +import org.bukkit.material.Tree; +import org.bukkit.material.WoodenStep; +import org.bukkit.material.Wool; @SuppressWarnings({"unused", "WeakerAccess"}) public class BukkitUtil extends WorldUtil { @@ -359,6 +379,10 @@ import java.util.*; return list; } + public static BukkitLegacyMappings getBukkitLegacyMappings() { + return (BukkitLegacyMappings) PlotSquared.imp().getLegacyMappings(); + } + @Override public boolean addItems(@NonNull final String worldName, @NonNull final PlotItem items) { final World world = getWorld(worldName); @@ -370,8 +394,9 @@ import java.util.*; if (state instanceof InventoryHolder) { InventoryHolder holder = (InventoryHolder) state; Inventory inv = holder.getInventory(); - for (int i = 0; i < items.id.length; i++) { - ItemStack item = new ItemStack(LegacyMappings.fromLegacyId(items.id[i]).getMaterial(), items.amount[i], items.data[i]); + for (int i = 0; i < items.types.length; i++) { + // ItemStack item = new ItemStack(LegacyMappings.fromLegacyId(items.id[i]).getMaterial(), items.amount[i], items.data[i]); + ItemStack item = new ItemStack(items.types[i].to(Material.class), items.amount[i]); inv.addItem(item); } state.update(true); @@ -385,7 +410,7 @@ import java.util.*; return Material.getMaterial(((StringPlotBlock) plotBlock).getItemId().toUpperCase(Locale.ENGLISH)); } else { final LegacyPlotBlock legacyPlotBlock = (LegacyPlotBlock) plotBlock; - return LegacyMappings.fromLegacyId(legacyPlotBlock.getId()).getMaterial(); + return getBukkitLegacyMappings().fromLegacyToString(legacyPlotBlock.getId(), legacyPlotBlock.getData()).to(Material.class); } } 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 70ec0eb6d..27f6023ca 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 @@ -43,7 +43,7 @@ public abstract class PlotBlock implements ConfigurationSerializable { public static PlotBlock deserialize(@NonNull final Map map) { if (map.containsKey("material")) { final Object object = map.get("material"); - return get(object); + return get(object.toString()); } return null; } @@ -52,6 +52,21 @@ public abstract class PlotBlock implements ConfigurationSerializable { return ImmutableMap.of("material", this.getRawId()); } + public T to(@NonNull final Class clazz) { + if (blockRegistry == null) { + blockRegistry = PlotSquared.imp().getBlockRegistry(); + if (blockRegistry == null) { + throw new UnsupportedOperationException("The PlotSquared implementation has not registered a custom block registry." + + " This method can't be used."); + } + conversionType = blockRegistry.getType(); + } + if (!clazz.equals(conversionType)) { + throw new UnsupportedOperationException("The PlotSquared implementation has not registered a block registry for this object type"); + } + return clazz.cast(blockRegistry.getItem(this)); + } + public abstract boolean isAir(); public static StringPlotBlock get(@NonNull final String itemId) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/PlotItem.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/PlotItem.java index e943ab71f..2b7e31295 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/PlotItem.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/schematic/PlotItem.java @@ -1,20 +1,22 @@ package com.github.intellectualsites.plotsquared.plot.object.schematic; +import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; + public class PlotItem { public final int x; public final int y; public final int z; - public final short[] id; - public final byte[] data; + // public final short[] id; + // public final byte[] data; + public final PlotBlock[] types; public final byte[] amount; - public PlotItem(short x, short y, short z, short[] id, byte[] data, byte[] amount) { + public PlotItem(short x, short y, short z, PlotBlock[] types, byte[] amount) { this.x = x; this.y = y; this.z = z; - this.id = id; - this.data = data; + this.types = types; this.amount = amount; } }