Skip to content

Commit

Permalink
Small fixes and changes (Done)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Aug 17, 2021
1 parent f9f411c commit e585f18
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
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;
import net.sourcewriters.spigot.rwg.legacy.api.version.util.MinecraftVersion;
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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public boolean unregister(long id) {

@Override
public boolean has(long id) {
return parsers.contains(id);
return parsers.containsKey(id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -57,7 +60,7 @@ public boolean unregister(long id) {

@Override
public boolean has(long id) {
return placers.contains(id);
return placers.containsKey(id);
}

@Override
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e585f18

Please sign in to comment.