Merge branch 'breaking' into breaking

This commit is contained in:
dordsor21
2018-12-19 14:54:32 +00:00
committed by GitHub
8 changed files with 298 additions and 6 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.
@ -273,5 +274,8 @@ public interface IPlotMain extends ILogger {
List<String> getPluginIds();
BlockRegistry<BlockType> getBlockRegistry();
LegacyMappings getLegacyMappings();
}

View File

@ -831,7 +831,11 @@ public enum C {
HELP_FOOTER("$3&m---------&r $1Plot\u00B2 Help $3&m---------", "Help"),
HELP_INFO_ITEM("$1/plot help %category% $3- $2%category_desc%", "Help"), HELP_ITEM(
"$1%usage% [%alias%]&- $3- $2%desc%&-", "Help"), /*
"$1%usage% [%alias%]&- $3- $2%desc%&-", "Help"),
BUCKET_ENTRIES_IGNORED("$2Total bucket values add up to 1 or more. Blocks without a spcified chance will be ignored", "Generator_Bucket"),
/*
* Direction
*/

View File

@ -0,0 +1,154 @@
package com.github.intellectualsites.plotsquared.plot.object;
import com.github.intellectualsites.plotsquared.plot.config.C;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
/**
* A block bucket is a container of block types, where each block
* has a specified chance of being randomly picked
*/
@SuppressWarnings({"unused", "WeakerAccess"})
public final class BlockBucket implements Iterable<PlotBlock> {
private final Random random = new Random();
private final Map<Range, PlotBlock> ranges = new HashMap<>();
private final Map<PlotBlock, Integer> blocks;
private final BucketIterator bucketIterator = new BucketIterator();
private boolean compiled;
public BlockBucket() {
this.blocks = new HashMap<>();
}
public void addBlock(@NonNull final PlotBlock block) {
this.addBlock(block, -1);
}
public void addBlock(@NonNull final PlotBlock block, final int chance) {
this.blocks.put(block, chance);
this.compiled = false;
}
public void compile() {
if (isCompiled()) {
return;
}
final Map<PlotBlock, Integer> temp = new HashMap<>(blocks.size());
final List<PlotBlock> unassigned = new ArrayList<>(blocks.size());
int sum = 0;
for (final Map.Entry<PlotBlock, Integer> entry : blocks.entrySet()) {
if (entry.getValue() == -1) {
unassigned.add(entry.getKey());
} else {
sum += entry.getValue();
}
}
//
// If this doesn't amount to 100 add it up to exactly 100.
//
if (sum < 100) {
final int remaining = 100 - sum;
final int perUnassigned = remaining / unassigned.size();
for (final PlotBlock block : unassigned) {
temp.put(block, perUnassigned);
sum += perUnassigned;
}
// Make sure there isn't a tiny difference remaining
if (sum < 100) {
final int difference = 100 - sum;
temp.put(unassigned.get(0), perUnassigned + difference);
sum = 100;
}
} else if (!unassigned.isEmpty()) {
C.BUCKET_ENTRIES_IGNORED.send(ConsolePlayer.getConsole());
}
//
// If the sum adds up to more than 100, divide all values
//
if (sum > 100) {
final double ratio = 100D / sum;
for (final Map.Entry<PlotBlock, Integer> entry : blocks.entrySet()) {
if (entry.getValue() == -1) {
continue;
}
temp.put(entry.getKey(), (int)(entry.getValue() * ratio));
}
} else {
temp.forEach(temp::put);
}
int start = 0;
for (final Map.Entry<PlotBlock, Integer> entry : temp.entrySet()) {
final int rangeStart = start;
final int rangeEnd = rangeStart + entry.getValue();
start = rangeEnd + 1;
final Range range = new Range(rangeStart, rangeEnd);
this.ranges.put(range, entry.getKey());
}
this.blocks.clear();
this.compiled = true;
}
@Override
public Iterator<PlotBlock> iterator() {
return this.bucketIterator;
}
public boolean isCompiled() {
return this.compiled;
}
/**
* Get a random block out of the bucket
*
* @return Randomly picked block (cased on specified rates)
*/
public PlotBlock getBlock() {
if (!isCompiled()) {
this.compile();
}
final int number = random.nextInt(101);
for (final Map.Entry<Range, PlotBlock> entry : ranges.entrySet()) {
if (entry.getKey().isInRange(number)) {
return entry.getValue();
}
}
// Didn't find a block? Try again
return getBlock();
}
private final class BucketIterator implements Iterator<PlotBlock> {
@Override
public boolean hasNext() {
return true;
}
@Override
public PlotBlock next() {
return getBlock();
}
}
@Getter
@EqualsAndHashCode
@RequiredArgsConstructor
private final static class Range {
private final int min;
private final int max;
public boolean isInRange(final int num) {
return num <= max && num >= min;
}
}
}

View File

@ -0,0 +1,34 @@
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;
for (final T preInitializedItem : preInitializedItems) {
this.addMapping(getPlotBlock(preInitializedItem), preInitializedItem);
}
}
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

@ -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(conversionType)) {
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;