From e585f182328c7c2c84c39b40faddd3711083d521 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Endre=C3=9F?= Date: Tue, 17 Aug 2021 19:51:43 +0200 Subject: [PATCH] Small fixes and changes (Done) --- .../spigot/rwg/legacy/api/block/BlockDataParser.java | 5 ++++- .../spigot/rwg/legacy/api/block/BlockDataPlacer.java | 10 ++++++++-- .../spigot/rwg/legacy/api/block/IBlockData.java | 9 +++++++-- .../rwg/legacy/api/block/impl/BaseBlockData.java | 8 ++++++++ .../api/block/impl/BlockDataParserManagerImpl.java | 2 +- .../api/block/impl/BlockDataPlacerManagerImpl.java | 12 ++++++------ .../rwg/legacy/api/block/impl/CustomBlockData.java | 9 ++++----- .../api/block/impl/DefaultMinecraftParser.java | 4 ++-- .../legacy/api/block/impl/MinecraftBlockData.java | 8 +++----- 9 files changed, 43 insertions(+), 24 deletions(-) diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataParser.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataParser.java index 2063c6c..fcc34db 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataParser.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataParser.java @@ -1,6 +1,7 @@ package net.sourcewriters.spigot.rwg.legacy.api.block; import java.util.Objects; +import java.util.concurrent.atomic.AtomicLong; import org.bukkit.plugin.Plugin; @@ -9,6 +10,8 @@ import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull; public abstract class BlockDataParser { + + private static final AtomicLong GLOBAL_ID = new AtomicLong(0); protected final long id; protected final Plugin plugin; @@ -17,7 +20,7 @@ public abstract class BlockDataParser { public BlockDataParser(@NonNull Plugin plugin, @NonNull String namespace) { Objects.requireNonNull(plugin, "Plugin can't be null!"); Objects.requireNonNull(namespace, "String namespace can't be null!"); - this.id = plugin.getName().hashCode() + (namespace.hashCode() * 32); + this.id = GLOBAL_ID.getAndIncrement(); this.plugin = plugin; this.namespace = namespace; } diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataPlacer.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataPlacer.java index b3ac69c..a5140bb 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataPlacer.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/BlockDataPlacer.java @@ -1,11 +1,13 @@ package net.sourcewriters.spigot.rwg.legacy.api.block; import java.util.Objects; +import java.util.concurrent.atomic.AtomicLong; import org.bukkit.Location; import org.bukkit.block.Block; import org.bukkit.plugin.Plugin; +import com.google.common.base.Preconditions; import com.syntaxphoenix.syntaxapi.random.RandomNumberGenerator; import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull; @@ -13,6 +15,8 @@ import net.sourcewriters.spigot.rwg.legacy.api.version.util.ServerVersion; public abstract class BlockDataPlacer { + + private static final AtomicLong GLOBAL_ID = new AtomicLong(0); protected final long id; @@ -21,8 +25,10 @@ public abstract class BlockDataPlacer { public BlockDataPlacer(@NonNull Plugin plugin, @NonNull String namespace) { this.plugin = Objects.requireNonNull(plugin, "Plugin can't be null!"); - this.namespace = Objects.requireNonNull(namespace, "String namespace can't be null!"); - this.id = plugin.getName().hashCode() + (namespace.hashCode() * 32); + Objects.requireNonNull(namespace, "String namespace can't be null!"); + Preconditions.checkArgument(!namespace.isBlank(), "String namespace can't be empty!"); + this.namespace = namespace; + this.id = GLOBAL_ID.getAndIncrement(); } @NonNull diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/IBlockData.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/IBlockData.java index 321ced3..bebabca 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/IBlockData.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/IBlockData.java @@ -38,13 +38,18 @@ default boolean isMinecraft() { } default IBlockData setConversionPossible(boolean state) { - getProperties().set(IProperty.of("data_conversion", state)); + IProperties properties = getProperties(); + if (state) { + properties.remove("data_conversion"); + return this; + } + properties.set(IProperty.of("data_conversion", false)); return this; } default boolean isConversionPossible() { IProperty property = getProperties().find("data_conversion").cast(boolean.class); - return property.isPresent() ? property.getValue() : false; + return property.isPresent() ? property.getValue() : true; } default boolean isSame(Object obj) { diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BaseBlockData.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BaseBlockData.java index a9ccb95..8065247 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BaseBlockData.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BaseBlockData.java @@ -3,6 +3,7 @@ import net.sourcewriters.spigot.rwg.legacy.api.block.IBlockData; import net.sourcewriters.spigot.rwg.legacy.api.data.property.IProperties; import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull; +import net.sourcewriters.spigot.rwg.legacy.api.util.data.JsonIO; public abstract class BaseBlockData implements IBlockData { @@ -14,6 +15,13 @@ public final IProperties getProperties() { return properties; } + protected abstract String dataString(); + + @Override + public final String asString() { + return properties.isEmpty() ? dataString() : dataString() + JsonIO.toString(properties); + } + @Override public final boolean equals(Object obj) { return isSame(obj); diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataParserManagerImpl.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataParserManagerImpl.java index 40ef270..717b526 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataParserManagerImpl.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataParserManagerImpl.java @@ -62,7 +62,7 @@ public boolean unregister(long id) { @Override public boolean has(long id) { - return parsers.contains(id); + return parsers.containsKey(id); } @Override diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataPlacerManagerImpl.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataPlacerManagerImpl.java index e4da78d..a95bfae 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataPlacerManagerImpl.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/BlockDataPlacerManagerImpl.java @@ -26,6 +26,9 @@ public class BlockDataPlacerManagerImpl implements IBlockDataPlacerManager { private final ILogger logger; + private final MinecraftVersion minecraft = Versions.getMinecraft(); + private final ServerVersion server = Versions.getServer(); + public BlockDataPlacerManagerImpl(ILogger logger) { this.logger = logger; } @@ -57,7 +60,7 @@ public boolean unregister(long id) { @Override public boolean has(long id) { - return placers.contains(id); + return placers.containsKey(id); } @Override @@ -105,17 +108,14 @@ public boolean setBlock(@NonNull Block block, @NonNull IBlockData data, @NonNull logger.log(LogTypeId.WARNING, "Can't setBlock for namespace '" + namespace + "', no BlockPlacer available!"); return false; } - MinecraftVersion minecraft = Versions.getMinecraft(); - ServerVersion server = Versions.getServer(); int size = list.size(); for (int index = size - 1; index >= 0; index--) { BlockDataPlacer placer = placers.get(list.get(index)); if (!placer.owns(data)) { continue; } - boolean output = placer.placeBlock(block.getLocation(), block, data, random, minecraft, server); - if (output) { - return output; + if (placer.placeBlock(block.getLocation(), block, data, random, minecraft, server)) { + return true; } } return false; diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/CustomBlockData.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/CustomBlockData.java index d87f114..0a92e4d 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/CustomBlockData.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/CustomBlockData.java @@ -10,7 +10,6 @@ import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull; import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.unsafe.Unsafe; import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.unsafe.UnsafeStatus; -import net.sourcewriters.spigot.rwg.legacy.api.util.data.JsonIO; public class CustomBlockData extends BaseBlockData { @@ -24,6 +23,7 @@ public CustomBlockData(@NonNull String namespace, @NonNull String id) { this.namespace = Objects.requireNonNull(namespace, "String namespace can't be null!"); this.id = Objects.requireNonNull(id, "String id can't be null!"); this.key = namespace + ":" + id; + setConversionPossible(false); } @NonNull @@ -49,11 +49,10 @@ public final String getKey() { public Material getMaterial() { return Material.AIR; } - - @NonNull + @Override - public final String asString() { - return key + JsonIO.toString(properties); + protected String dataString() { + return key; } @NonNull diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/DefaultMinecraftParser.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/DefaultMinecraftParser.java index 0e7b476..5b8a04d 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/DefaultMinecraftParser.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/DefaultMinecraftParser.java @@ -27,8 +27,8 @@ public IBlockData parse(IBlockAccess access, NbtCompound compound) { for (String key : states.getKeys()) { builder.append(key).append('=').append(states.getString(key)).append(','); } - String state = builder.substring(0, builder.length() - 1) + ']'; - IBlockData data = access.dataOf("minecraft:" + id + state).setConversionPossible(true); + String state = builder.substring(0, builder.length() == 1 ? 1 : builder.length() - 1) + ']'; + IBlockData data = access.dataOf("minecraft:" + id + state); if (data == null || !compound.hasKey("properties", NbtType.COMPOUND)) { return data; } diff --git a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/MinecraftBlockData.java b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/MinecraftBlockData.java index 1d9e85b..dbbb6cf 100644 --- a/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/MinecraftBlockData.java +++ b/legacy-api/src/main/java/net/sourcewriters/spigot/rwg/legacy/api/block/impl/MinecraftBlockData.java @@ -7,7 +7,6 @@ import net.sourcewriters.spigot.rwg.legacy.api.block.BlockStateEditor; import net.sourcewriters.spigot.rwg.legacy.api.util.annotation.source.NonNull; -import net.sourcewriters.spigot.rwg.legacy.api.util.data.JsonIO; public class MinecraftBlockData extends BaseBlockData { @@ -54,11 +53,10 @@ public Material getMaterial() { public BlockData asBukkit() { return data; } - - @NonNull + @Override - public final String asString() { - return data.getAsString() + JsonIO.toString(properties); + protected String dataString() { + return data.getAsString(); } @NonNull