Merge pull request 'Improved 1.12 .schem loader' (#197) from improvedSchemLoader into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
Reviewed-on: #197 Reviewed-by: YoyoNow <jwsteam@nidido.de>
Dieser Commit ist enthalten in:
Commit
c39bb2005d
@ -22,33 +22,84 @@ package de.steamwar.core;
|
|||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Map;
|
import java.util.stream.Collectors;
|
||||||
import java.util.Objects;
|
|
||||||
|
|
||||||
class IDConverter8 {
|
public class IDConverter8 {
|
||||||
private IDConverter8(){}
|
|
||||||
|
private final Map<String, Set<String>> availibleAttributes;
|
||||||
|
private final Map<String, Map<Set<String>, BlockTypeID>> map;
|
||||||
|
|
||||||
|
public IDConverter8() {
|
||||||
|
Map<String, Map<Set<String>, BlockTypeID>> map = new HashMap<>();
|
||||||
|
|
||||||
static Map<String, BlockTypeID> getMap(){
|
|
||||||
Map<String, BlockTypeID> ids = new HashMap<>();
|
|
||||||
YamlConfiguration legacy = YamlConfiguration.loadConfiguration(new InputStreamReader(Objects.requireNonNull(IDConverter8.class.getClassLoader().getResourceAsStream("legacy.yml"))));
|
YamlConfiguration legacy = YamlConfiguration.loadConfiguration(new InputStreamReader(Objects.requireNonNull(IDConverter8.class.getClassLoader().getResourceAsStream("legacy.yml"))));
|
||||||
for(String blockString : legacy.getKeys(false)){
|
for(String blockString : legacy.getKeys(false)){
|
||||||
String blockNum = legacy.getString(blockString);
|
String[] legacyBlockId = legacy.getString(blockString).split(":");
|
||||||
String[] block = blockNum.split(":");
|
|
||||||
ids.put(blockString, new BlockTypeID(Integer.parseInt(block[0]), Byte.parseByte(block[1])));
|
map.computeIfAbsent(getBlockId(blockString), bId -> {
|
||||||
if(blockString.contains("["))
|
Map<Set<String>, BlockTypeID> attributeMap = new HashMap<>();
|
||||||
ids.putIfAbsent(blockString.split("\\[")[0], new BlockTypeID(Integer.parseInt(block[0]), (byte)0));
|
attributeMap.put(new HashSet<>(), new BlockTypeID(legacyBlockId[0], "0"));
|
||||||
|
return attributeMap;
|
||||||
|
}).put(getAttributes(blockString), new BlockTypeID(legacyBlockId[0], legacyBlockId[1]));
|
||||||
}
|
}
|
||||||
return ids;
|
this.map = map;
|
||||||
|
|
||||||
|
Map<String, Set<String>> availableAttributes = new HashMap<>();
|
||||||
|
for (Map.Entry<String, Map<Set<String>, BlockTypeID>> entry : map.entrySet()) {
|
||||||
|
availableAttributes.put(entry.getKey(), entry.getValue().keySet().stream().flatMap(Collection::stream).collect(Collectors.toSet()));
|
||||||
|
}
|
||||||
|
this.availibleAttributes = availableAttributes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BlockTypeID getId(String blockString) {
|
||||||
|
String blockId = getBlockId(blockString);
|
||||||
|
Map<Set<String>, IDConverter8.BlockTypeID> attributeMap = map.get(blockId);
|
||||||
|
if(attributeMap == null) { // Block nonexistent pre-flattening
|
||||||
|
return new BlockTypeID("0", "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<String> attributes = getAttributes(blockString);
|
||||||
|
Set<String> knownAttributes = this.availibleAttributes.get(blockId);
|
||||||
|
attributes.removeIf(attribute -> !knownAttributes.contains(attribute));
|
||||||
|
|
||||||
|
long bestMatch = -1;
|
||||||
|
BlockTypeID blockID = null;
|
||||||
|
for(Map.Entry<Set<String>, BlockTypeID> entry : attributeMap.entrySet()) {
|
||||||
|
Set<String> attrs = entry.getKey();
|
||||||
|
if(attrs.size() <= bestMatch)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
long matching = attributes.stream().filter(attrs::contains).count();
|
||||||
|
if(matching > bestMatch) {
|
||||||
|
blockID = entry.getValue();
|
||||||
|
bestMatch = matching;
|
||||||
|
|
||||||
|
if(bestMatch == attributes.size())
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return blockID;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getBlockId(String blockString) {
|
||||||
|
return blockString.split("\\[", 2)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
private Set<String> getAttributes(String blockString) {
|
||||||
|
Set<String> attributes = new HashSet<>();
|
||||||
|
if(blockString.contains("["))
|
||||||
|
Collections.addAll(attributes, blockString.split("\\[")[1].replace("]", "").split(","));
|
||||||
|
return attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
static class BlockTypeID{
|
static class BlockTypeID{
|
||||||
private final int blockId;
|
private final int blockId;
|
||||||
private final byte dataId;
|
private final byte dataId;
|
||||||
|
|
||||||
private BlockTypeID(int blockId, byte dataId) {
|
private BlockTypeID(String blockId, String dataId) {
|
||||||
this.blockId = blockId;
|
this.blockId = Integer.parseInt(blockId);
|
||||||
this.dataId = dataId;
|
this.dataId = Byte.parseByte(dataId);
|
||||||
}
|
}
|
||||||
|
|
||||||
int getBlockId() {
|
int getBlockId() {
|
||||||
|
@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
|
|||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
import com.sk89q.jnbt.*;
|
import com.sk89q.jnbt.*;
|
||||||
import com.sk89q.worldedit.*;
|
import com.sk89q.worldedit.*;
|
||||||
|
import com.sk89q.worldedit.Vector;
|
||||||
import com.sk89q.worldedit.blocks.BaseBlock;
|
import com.sk89q.worldedit.blocks.BaseBlock;
|
||||||
import com.sk89q.worldedit.bukkit.BukkitWorld;
|
import com.sk89q.worldedit.bukkit.BukkitWorld;
|
||||||
import com.sk89q.worldedit.extension.input.ParserContext;
|
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||||
@ -20,10 +21,7 @@ import de.steamwar.sql.NoClipboardException;
|
|||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -123,7 +121,7 @@ public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private BlockArrayClipboard readSchematic(CompoundTag schematicTag) throws IOException {
|
private BlockArrayClipboard readSchematic(CompoundTag schematicTag) throws IOException {
|
||||||
final Map<String, IDConverter8.BlockTypeID> ids = IDConverter8.getMap();
|
IDConverter8 ids = new IDConverter8();
|
||||||
|
|
||||||
Map<String, Tag> schematic = schematicTag.getValue();
|
Map<String, Tag> schematic = schematicTag.getValue();
|
||||||
int width = (requireTag(schematic, "Width", ShortTag.class)).getValue();
|
int width = (requireTag(schematic, "Width", ShortTag.class)).getValue();
|
||||||
@ -166,22 +164,9 @@ public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper {
|
|||||||
parserContext.setRestricted(false);
|
parserContext.setRestricted(false);
|
||||||
parserContext.setPreferringWildcard(false);
|
parserContext.setPreferringWildcard(false);
|
||||||
|
|
||||||
int id;
|
for(String palettePart : paletteObject.keySet()) {
|
||||||
BaseBlock state;
|
IDConverter8.BlockTypeID blockID = ids.getId(palettePart);
|
||||||
for(Iterator<String> iterator = paletteObject.keySet().iterator(); iterator.hasNext(); palette.put(id, state)) {
|
palette.put(requireTag(paletteObject, palettePart, IntTag.class).getValue(), new BaseBlock(blockID.getBlockId(), blockID.getDataId()));
|
||||||
String palettePart = iterator.next();
|
|
||||||
id = requireTag(paletteObject, palettePart, IntTag.class).getValue();
|
|
||||||
|
|
||||||
IDConverter8.BlockTypeID blockID = ids.get(palettePart);
|
|
||||||
if(blockID == null){
|
|
||||||
blockID = ids.get(palettePart.split("\\[")[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(blockID == null){
|
|
||||||
state = new BaseBlock(0); //AIR
|
|
||||||
}else{
|
|
||||||
state = new BaseBlock(blockID.getBlockId(), blockID.getDataId());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] blocks = requireTag(schematic, "BlockData", ByteArrayTag.class).getValue();
|
byte[] blocks = requireTag(schematic, "BlockData", ByteArrayTag.class).getValue();
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren