Progress.

This commit is contained in:
sauilitired
2018-12-18 20:54:20 +01:00
parent fb7bcef05f
commit ccad0bced9
8 changed files with 272 additions and 5 deletions

View File

@ -4,6 +4,7 @@ import com.github.intellectualsites.plotsquared.plot.generator.GeneratorWrapper;
import com.github.intellectualsites.plotsquared.plot.generator.HybridUtils;
import com.github.intellectualsites.plotsquared.plot.generator.IndependentPlotGenerator;
import com.github.intellectualsites.plotsquared.plot.logger.ILogger;
import com.github.intellectualsites.plotsquared.plot.object.BlockRegistry;
import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer;
import com.github.intellectualsites.plotsquared.plot.util.*;
import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider;
@ -11,7 +12,7 @@ import com.github.intellectualsites.plotsquared.plot.util.block.QueueProvider;
import java.io.File;
import java.util.List;
public interface IPlotMain extends ILogger {
public interface IPlotMain<BlockType> extends ILogger {
/**
* Log a message to console.
@ -272,4 +273,6 @@ public interface IPlotMain extends ILogger {
AbstractTitle initTitleManager();
List<String> getPluginIds();
BlockRegistry<BlockType> getBlockRegistry();
}

View File

@ -0,0 +1,31 @@
package com.github.intellectualsites.plotsquared.plot.object;
import java.util.HashMap;
import java.util.Map;
import lombok.Getter;
import lombok.NonNull;
public abstract class BlockRegistry<T> {
@Getter
private final Class<T> type;
private final Map<PlotBlock, T> map = new HashMap<>();
public BlockRegistry(@NonNull final Class<T> type, final T... preInitializedItems) {
this.type = type;
}
public final void addMapping(@NonNull final PlotBlock plotBlock, @NonNull final T t) {
if (map.containsKey(plotBlock)) {
return;
}
this.map.put(plotBlock, t);
}
public abstract PlotBlock getPlotBlock(final T item);
public final T getItem(final PlotBlock plotBlock) {
return this.map.get(plotBlock);
}
}

View File

@ -0,0 +1,40 @@
package com.github.intellectualsites.plotsquared.plot.object;
import java.util.Collections;
import java.util.Map;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
public final class BlockWrapper {
@Getter
private final PlotBlock type;
private final Map<Class, Object> blockStates;
public <T> T getState(@NonNull final Class stateType, @NonNull final Class<T> valueType, @NonNull final T defaultValue) {
if (!blockStates.containsKey(stateType)) {
return defaultValue;
}
final Object rawValue = blockStates.get(stateType);
if (!rawValue.getClass().equals(valueType)) {
throw new ClassCastException(String.format("State type %s has a value of type %s but %s was requested",
stateType.getSimpleName(), rawValue.getClass().getSimpleName(), valueType.getSimpleName()));
}
return valueType.cast(rawValue);
}
public <T> void setState(@NonNull final Class stateType, @NonNull final T value) {
this.blockStates.put(stateType, value);
}
public Map<Class, Object> getAllStates() {
return Collections.unmodifiableMap(this.blockStates);
}
}

View File

@ -0,0 +1,97 @@
package com.github.intellectualsites.plotsquared.plot.object;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
public final class BlockWrapperFactory<BlockType> {
@RequiredArgsConstructor
public static final class StateEntry<StateType extends Class, ObjectType> {
@Getter
private final StateType stateType;
@Getter
private final ObjectType value;
}
public interface BlockStateDeserializer<StateType extends Class, ObjectType> {
StateEntry<StateType, ObjectType> deserialize(PlotBlock type, String serialized);
boolean isOfType(String serializedString);
}
@FunctionalInterface
public interface BlockStateSerializer<StateType extends Class, ObjectType> {
String serialize(PlotBlock type, StateEntry<StateType, ObjectType> entry);
}
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
public static final class StateSerializationMapping<StateType extends Class, ObjectType extends Class> {
@Getter
private final StateType type;
@Getter
private final ObjectType objectType;
@Getter
private final BlockStateSerializer<StateType, ObjectType> serializer;
@Getter
private final BlockStateDeserializer<StateType, ObjectType> deserializer;
}
private final Map<Class, StateSerializationMapping> stateSerializationMappings = new HashMap<>();
public <StateType extends Class, ObjectType extends Class> StateSerializationMapping<StateType, ObjectType>
addStateMapping(@NonNull final StateType type, @NonNull final ObjectType objectType, @NonNull final BlockStateSerializer<StateType, ObjectType> serializer, @NonNull final BlockStateDeserializer<StateType, ObjectType> deserializer) {
final StateSerializationMapping<StateType, ObjectType> stateSerializationMapping = new StateSerializationMapping<>(type, objectType, serializer, deserializer);
this.stateSerializationMappings.put(type, stateSerializationMapping);
return stateSerializationMapping;
}
public <StateType extends Class, ObjectType extends Class> StateSerializationMapping<StateType, ObjectType>
getStateMapping(@NonNull final StateType type) {
return this.stateSerializationMappings.get(type);
}
public Collection<String> serializeStates(@NonNull final BlockWrapper blockWrapper) {
final List<String> serializedStates = new ArrayList<>();
blockWrapper.getAllStates().entrySet().stream().map(entry -> new StateEntry(entry.getKey(), entry.getValue()))
.forEach(entry -> {
final StateSerializationMapping stateSerializationMapping = getStateMapping(entry.getStateType());
final BlockStateSerializer blockStateSerializer = stateSerializationMapping.getSerializer();
final String serialized = blockStateSerializer.serialize(blockWrapper.getType(), entry);
serializedStates.add(serialized);
});
return serializedStates;
}
public BlockStateDeserializer getDeserializerRaw(@NonNull final String serializedString) {
for (final StateSerializationMapping stateSerializationMapping : this.stateSerializationMappings.values()) {
if (stateSerializationMapping.getDeserializer().isOfType(serializedString)) {
return stateSerializationMapping.getDeserializer();
}
}
return null;
}
public Collection<StateEntry> deserializeStates(@NonNull final PlotBlock plotBlock, @NonNull final Collection<String> serializedStates) {
final Collection<StateEntry> stateEntries = new ArrayList<>(serializedStates.size());
for (final String serializedState : serializedStates) {
if (serializedState == null || serializedState.isEmpty()) {
continue;
}
final BlockStateDeserializer blockStateDeserializer = getDeserializerRaw(serializedState);
if (blockStateDeserializer == null) {
throw new IllegalStateException(String.format("No deserializer available for %s", serializedState));
}
stateEntries.add(blockStateDeserializer.deserialize(plotBlock, serializedState));
}
return stateEntries;
}
}

View File

@ -1,5 +1,6 @@
package com.github.intellectualsites.plotsquared.plot.object;
import com.github.intellectualsites.plotsquared.plot.PlotSquared;
import com.github.intellectualsites.plotsquared.plot.config.Settings;
import lombok.NonNull;
@ -7,6 +8,9 @@ import java.util.Collection;
public abstract class PlotBlock {
private static Class<?> conversionType;
private static BlockRegistry blockRegistry;
public static boolean isEverything(@NonNull final PlotBlock block) {
return block.equals(LegacyPlotBlock.EVERYTHING) || block.equals(StringPlotBlock.EVERYTHING);
}
@ -56,6 +60,21 @@ public abstract class PlotBlock {
return get(((LegacyPlotBlock) plotBlock).getId(), (byte) 0);
}
public static PlotBlock get(@NonNull final Object type) {
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 (!type.getClass().equals(blockRegistry.getClass())) {
throw new UnsupportedOperationException("The PlotSquared implementation has not registered a block registry for this object type");
}
return blockRegistry.getPlotBlock(type);
}
public final boolean equalsAny(final int id, @NonNull final String stringId) {
if (this instanceof StringPlotBlock) {
final StringPlotBlock stringPlotBlock = (StringPlotBlock) this;