SteamWar/SpigotCore
Archiviert
13
0

Improved 1.12 .schem loader #197

Zusammengeführt
Lixfel hat 1 Commits von improvedSchemLoader nach master 2022-05-31 13:31:21 +02:00 zusammengeführt
2 geänderte Dateien mit 73 neuen und 37 gelöschten Zeilen
Nur Änderungen aus Commit 4905d88ad3 werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -22,33 +22,84 @@ package de.steamwar.core;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
class IDConverter8 {
private IDConverter8(){}
public class 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"))));
for(String blockString : legacy.getKeys(false)){
String blockNum = legacy.getString(blockString);
String[] block = blockNum.split(":");
ids.put(blockString, new BlockTypeID(Integer.parseInt(block[0]), Byte.parseByte(block[1])));
if(blockString.contains("["))
ids.putIfAbsent(blockString.split("\\[")[0], new BlockTypeID(Integer.parseInt(block[0]), (byte)0));
String[] legacyBlockId = legacy.getString(blockString).split(":");
map.computeIfAbsent(getBlockId(blockString), bId -> {
Map<Set<String>, BlockTypeID> attributeMap = new HashMap<>();
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{
private final int blockId;
private final byte dataId;
private BlockTypeID(int blockId, byte dataId) {
this.blockId = blockId;
this.dataId = dataId;
private BlockTypeID(String blockId, String dataId) {
this.blockId = Integer.parseInt(blockId);
this.dataId = Byte.parseByte(dataId);
}
int getBlockId() {

Datei anzeigen

@ -4,6 +4,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.sk89q.jnbt.*;
import com.sk89q.worldedit.*;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.blocks.BaseBlock;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.extension.input.ParserContext;
@ -20,10 +21,7 @@ import de.steamwar.sql.NoClipboardException;
import org.bukkit.entity.Player;
import java.io.*;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
@ -123,7 +121,7 @@ public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper {
}
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();
int width = (requireTag(schematic, "Width", ShortTag.class)).getValue();
@ -166,22 +164,9 @@ public class WorldEditWrapper8 implements WorldEditWrapper.IWorldEditWrapper {
parserContext.setRestricted(false);
parserContext.setPreferringWildcard(false);
int id;
BaseBlock state;
for(Iterator<String> iterator = paletteObject.keySet().iterator(); iterator.hasNext(); palette.put(id, state)) {
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());
}
for(String palettePart : paletteObject.keySet()) {
IDConverter8.BlockTypeID blockID = ids.getId(palettePart);
palette.put(requireTag(paletteObject, palettePart, IntTag.class).getValue(), new BaseBlock(blockID.getBlockId(), blockID.getDataId()));
}
byte[] blocks = requireTag(schematic, "BlockData", ByteArrayTag.class).getValue();