Created
October 30, 2024 16:16
-
-
Save kennytv/578659f3f04d89ffeff7c5d4d65de25d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
public final class ProjectsPath { | |
public static final Path HOME = Paths.get(System.getProperty("user.home")); | |
public static final Path PROJECTS = HOME.resolve("IdeaProjects"); | |
public static Path resolve(final String path) { | |
final int firstSeparator = path.indexOf('/'); | |
if (firstSeparator == -1) { | |
return PROJECTS.resolve(path); | |
} | |
final String[] split = path.substring(firstSeparator + 1).split("/"); | |
return PROJECTS.resolve(Paths.get(path.substring(0, firstSeparator), split)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.JsonElement; | |
import com.google.gson.JsonObject; | |
import eu.kennytv.test.util.G; | |
import eu.kennytv.test.util.ProjectsPath; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Set; | |
import java.util.function.Function; | |
/** | |
* Similar to {@link ViaStateMapper} in use, except for the annoying blockstates from VB. | |
*/ | |
public final class VBBlockStateMapper { | |
private static final Path PATH_TO_PACKAGE = ProjectsPath.resolve("Mappings/mappings/diff"); | |
private static final List<String> WONDERFUL_STATES = List.of( | |
"_wood", "_log", "_sapling", "_wall", "_slab", "_stairs", | |
"_trapdoor", "_door", "_button", "_hanging_sign", "_wall_sign", "_sign", | |
"_leaves", "_fence_gate", "_fence", "_pressure_plate" | |
); | |
private static final List<Function<String, String>> MAPPERS = new ArrayList<>(); | |
static { | |
replace("resin_brick", "brick"); | |
} | |
public static void main(final String[] args) throws IOException { | |
final String from = "1.21.4"; | |
final String to = "1.21.2"; | |
final Path path = PATH_TO_PACKAGE.resolve("mapping-" + from + "to" + to + ".json"); | |
final JsonObject object = G.SON.fromJson(Files.readString(path), JsonObject.class); | |
final JsonObject blockStates = object.getAsJsonObject("blockstates"); | |
final JsonObject outputStates = new JsonObject(); | |
final Set<String> handled = new HashSet<>(); | |
for (final Map.Entry<String, JsonElement> entry : blockStates.entrySet()) { | |
final String value = entry.getValue().getAsString(); | |
final String key = entry.getKey(); | |
if (!value.isEmpty()) { | |
outputStates.add(key, entry.getValue()); | |
continue; | |
} | |
final String keyPart = key.split("\\[")[0]; | |
if (handled.contains(keyPart)) { | |
outputStates.add(key, entry.getValue()); | |
continue; | |
} | |
final String wonderfulState = WONDERFUL_STATES.stream().filter(keyPart::endsWith).findAny().orElse(null); | |
if (wonderfulState == null) { | |
outputStates.add(key, entry.getValue()); | |
continue; | |
} | |
handled.add(key); | |
String outputKey = keyPart.replace(wonderfulState, ""); | |
for (final Function<String, String> mapper : MAPPERS) { | |
outputKey = mapper.apply(outputKey); | |
} | |
outputStates.addProperty(keyPart, outputKey + wonderfulState + "["); | |
} | |
object.add("blockstates", outputStates); | |
Files.writeString(path, G.SON.toJson(object)); | |
} | |
private static void equals(final String from, final String to) { | |
MAPPERS.add(s -> s.equals(from) ? to : s); | |
} | |
private static void contains(final String from, final String to) { | |
MAPPERS.add(s -> s.contains(from) ? to : s); | |
} | |
private static void replace(final String from, final String to) { | |
MAPPERS.add(s -> s.replace(from, to)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.base.Preconditions; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.Function; | |
import org.checkerframework.checker.nullness.qual.Nullable; | |
/** | |
* Utility to more easily map blockstates. | |
*/ | |
public final class ViaStateMapper { | |
private static final Function<BlockState, BlockState> CONSUMER = state -> { | |
state.addProperty(5, "waterlogged", "false"); | |
state.setState("glow_lichen"); | |
return state; | |
}; | |
public static void main(final String[] args) throws IOException { | |
applyFunction(); | |
} | |
public static void applyFunction() throws IOException { | |
final String content = Files.readString(new File("states.txt").toPath()); | |
for (final String line : content.split("\n")) { | |
String trimmedLine = line.replace("\"", "").trim(); | |
if (trimmedLine.endsWith(": ,")) { | |
trimmedLine = trimmedLine.substring(0, trimmedLine.length() - 3); | |
} | |
final BlockState state = new BlockState(trimmedLine); | |
System.out.println("\"" + trimmedLine + "\": \"" + CONSUMER.apply(state) + "\","); | |
} | |
} | |
public static void replace(final String... replacements) throws IOException { | |
final String content = Files.readString(new File("states.txt").toPath()); | |
for (final String line : content.split("\n")) { | |
boolean found = false; | |
for (int i = 0; i < replacements.length; i += 2) { | |
final String from = replacements[i]; | |
if (!line.contains(from)) { | |
continue; | |
} | |
final String to = replacements[i + 1]; | |
final String[] split = line.split("\": \"", 2); | |
System.out.println(split[0] + "\": \"" + split[1].replace(from, to)); | |
found = true; | |
break; | |
} | |
if (!found) { | |
System.out.println(line); | |
} | |
} | |
} | |
public static void editKey() throws IOException { | |
final String newName = "dark_oak_slab"; | |
final String content = Files.readString(new File("states.txt").toPath()); | |
for (String line : content.split("\"\",")) { | |
if (line.trim().equals("}")) { | |
continue; | |
} | |
final String firstPart = line; | |
line = line.replace("\"", "").replace(": ", "").trim(); | |
final String[] split = line.split("\\[", 2); | |
System.out.println(firstPart + "\"minecraft:" + newName + "[" + split[1] + "\","); | |
} | |
} | |
public static void editProperties() throws IOException { | |
final String content = Files.readString(new File("test/fok.txt").toPath()); | |
for (String line : content.split("\n")) { | |
if (!line.contains("_wall")) { | |
continue; | |
} | |
final String firstPart = line; | |
line = line.replace("\"", "").replace(": ,", "").trim(); | |
System.out.println(firstPart.replace("\"\"", "\"" + line.replace("none", "false").replace("tall", "true").replace("low", "true") + "\"")); | |
} | |
} | |
public static final class BlockState { | |
private final List<Property> properties = new ArrayList<>(); | |
private String state; | |
public BlockState(final String state) { | |
final int start = state.indexOf('['); | |
if (start == -1) { | |
this.state = state; | |
return; | |
} | |
Preconditions.checkArgument(state.endsWith("]")); | |
this.state = state.substring(0, start); | |
int lastCommaIndex = start; | |
int commaIndex; | |
while ((commaIndex = state.indexOf(',', lastCommaIndex + 1)) != -1) { | |
final String part = state.substring(lastCommaIndex + 1, commaIndex); | |
final String[] split = part.split("=", 2); | |
properties.add(new Property(split[0], split[1])); | |
lastCommaIndex = commaIndex; | |
} | |
final String part = state.substring(lastCommaIndex + 1, state.length() - 1); | |
final String[] split = part.split("=", 2); | |
properties.add(new Property(split[0], split[1])); | |
} | |
public @Nullable Property getProperty(final String key) { | |
for (final Property property : properties) { | |
if (property.key.equals(key)) { | |
return property; | |
} | |
} | |
return null; | |
} | |
public void addProperty(final String key, final String value) { | |
properties.add(new Property(key, value)); | |
} | |
public void addProperty(final int index, final String key, final String value) { | |
properties.add(index, new Property(key, value)); | |
} | |
public @Nullable Property removeProperty(final String key) { | |
for (int i = 0; i < properties.size(); i++) { | |
final Property property = properties.get(i); | |
if (property.key.equals(key)) { | |
properties.remove(i); | |
return property; | |
} | |
} | |
return null; | |
} | |
public List<Property> getProperties() { | |
return properties; | |
} | |
public String getState() { | |
return state; | |
} | |
public void setState(final String state) { | |
this.state = state; | |
} | |
@Override | |
public String toString() { | |
final StringBuilder builder = new StringBuilder(state).append('['); | |
for (final Property property : properties) { | |
builder.append(property.getKey()).append('=').append(property.getValue()).append(','); | |
} | |
return builder.substring(0, builder.length() - 1) + "]"; | |
} | |
@Override | |
public boolean equals(final Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
final BlockState that = (BlockState) o; | |
if (!properties.equals(that.properties)) return false; | |
return state.equals(that.state); | |
} | |
@Override | |
public int hashCode() { | |
int result = properties.hashCode(); | |
result = 31 * result + state.hashCode(); | |
return result; | |
} | |
public BlockState copy() { | |
return new BlockState(toString()); | |
} | |
} | |
public static final class Property { | |
private String key; | |
private String value; | |
public Property(final String key, final String value) { | |
this.key = key; | |
this.value = value; | |
} | |
public String getKey() { | |
return key; | |
} | |
public void setKey(final String key) { | |
this.key = key; | |
} | |
public String getValue() { | |
return value; | |
} | |
public void setValue(final String value) { | |
this.value = value; | |
} | |
@Override | |
public boolean equals(final Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
final Property property = (Property) o; | |
if (!key.equals(property.key)) return false; | |
return value.equals(property.value); | |
} | |
@Override | |
public int hashCode() { | |
int result = key.hashCode(); | |
result = 31 * result + value.hashCode(); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment