Improve PlotBlock name matching

This commit is contained in:
sauilitired 2018-12-19 18:36:50 +01:00
parent e724aa8caf
commit 444222c7f9
2 changed files with 29 additions and 36 deletions

View File

@ -4,12 +4,20 @@ import com.github.intellectualsites.plotsquared.plot.object.LegacyPlotBlock;
import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; import com.github.intellectualsites.plotsquared.plot.object.PlotBlock;
import com.github.intellectualsites.plotsquared.plot.object.StringPlotBlock; import com.github.intellectualsites.plotsquared.plot.object.StringPlotBlock;
import com.github.intellectualsites.plotsquared.plot.util.LegacyMappings; import com.github.intellectualsites.plotsquared.plot.util.LegacyMappings;
import lombok.*; import com.github.intellectualsites.plotsquared.plot.util.StringComparison;
import org.bukkit.Material; import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.bukkit.Material;
/** /**
* Borrowed from https://github.com/Phoenix616/IDConverter/blob/master/mappings/src/main/java/de/themoep/idconverter/IdMappings.java * Borrowed from https://github.com/Phoenix616/IDConverter/blob/master/mappings/src/main/java/de/themoep/idconverter/IdMappings.java
@ -692,6 +700,16 @@ public class BukkitLegacyMappings extends LegacyMappings {
} }
} }
public Collection<PlotBlock> getPlotBlocks() {
return Arrays.stream(BLOCKS).map(block -> PlotBlock.get(block.getNewName()))
.collect(Collectors.toList());
}
public StringComparison<PlotBlock>.ComparisonResult getClosestsMatch(@NonNull final String string) {
final StringComparison<PlotBlock> comparison = new StringComparison<>(string, getPlotBlocks());
return comparison.getBestMatchAdvanced();
}
/** /**
* Try to find a legacy plot block by any means possible. * Try to find a legacy plot block by any means possible.
* Strategy: * Strategy:
@ -794,7 +812,10 @@ public class BukkitLegacyMappings extends LegacyMappings {
return LegacyPlotBlock.get(numericalId, dataValue); return LegacyPlotBlock.get(numericalId, dataValue);
} }
@Override
public String toString() {
return this.newName;
}
} }
} }

View File

@ -448,39 +448,11 @@ import org.bukkit.material.Wool;
@Override @Nullable @Override @Nullable
public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) { public StringComparison<PlotBlock>.ComparisonResult getClosestBlock(String name) {
try { final PlotBlock plotBlock = BukkitUtil.getBukkitLegacyMappings().fromAny(name);
final Material material = Material.valueOf(name.toUpperCase()); if (plotBlock != null) {
return new StringComparison<PlotBlock>().new ComparisonResult(1, return new StringComparison<PlotBlock>().new ComparisonResult(1, plotBlock);
PlotBlock.get(name));
} catch (IllegalArgumentException ignored) {
} }
try { return BukkitUtil.getBukkitLegacyMappings().getClosestsMatch(name);
byte data;
String[] split = name.split(":");
if (split.length == 2) {
data = Byte.parseByte(split[1]);
name = split[0];
} else {
data = 0;
}
double match;
short id;
if (MathMan.isInteger(split[0])) {
id = Short.parseShort(split[0]);
match = 0;
} else {
StringComparison<Material>.ComparisonResult comparison =
new StringComparison<>(name, Material.values()).getBestMatchAdvanced();
match = comparison.match;
id = (short) comparison.best.getId();
}
PlotBlock block = PlotBlock.get(id, data);
StringComparison<PlotBlock> outer = new StringComparison<>();
return outer.new ComparisonResult(match, block);
} catch (NumberFormatException ignored) {
}
return null;
} }
@Override @Override