Skip to content

Commit

Permalink
Implement Block Mapping Overrides
Browse files Browse the repository at this point in the history
* All attempts to load a resource will first look for the path under a "resources" folder relative to its folder. This allows overriding any resource at runtime.
* When loading mappings/blocks.json we also look for a overrides/blocks.json. Any entry in the override file will override the entry in the mappings file allowing one to selectively override blocks.
  • Loading branch information
bundabrg committed May 26, 2020
1 parent 14fcd77 commit 0734f8c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.geysermc.connector.utils.FileUtils;
import org.reflections.Reflections;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

Expand Down Expand Up @@ -97,6 +98,13 @@ public class BlockTranslator {
} catch (Exception e) {
throw new AssertionError("Unable to load Java block mappings", e);
}

// Load Block Overrides
JsonNode blocksOverride = null;
try (InputStream is = FileUtils.getResource("overrides/blocks.json")) {
blocksOverride = GeyserConnector.JSON_MAPPER.readTree(is);
} catch (IOException | AssertionError ignored) { }

Object2IntMap<CompoundTag> addedStatesMap = new Object2IntOpenHashMap<>();
addedStatesMap.defaultReturnValue(-1);
List<CompoundTag> paletteList = new ArrayList<>();
Expand All @@ -113,6 +121,12 @@ public class BlockTranslator {
javaRuntimeId++;
Map.Entry<String, JsonNode> entry = blocksIterator.next();
String javaId = entry.getKey();

// Check for an override
if (blocksOverride != null && blocksOverride.has(javaId)) {
entry = new AbstractMap.SimpleEntry<>(javaId, blocksOverride.get(javaId));
}

BlockState javaBlockState = new BlockState(javaRuntimeId);
CompoundTag blockTag = buildBedrockState(entry.getValue());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
import org.geysermc.connector.GeyserConnector;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.function.Function;

public class FileUtils {
Expand Down Expand Up @@ -133,6 +135,11 @@ public static void writeFile(String name, char[] data) throws IOException {
* @return InputStream of the given resource
*/
public static InputStream getResource(String resource) {
// First try open file under a resources folder. We use this try format so we don't close it
try {
return new FileInputStream(Paths.get("resources", resource).toFile());
} catch (IOException ignored) { }

InputStream stream = FileUtils.class.getClassLoader().getResourceAsStream(resource);
if (stream == null) {
throw new AssertionError("Unable to find resource: " + resource);
Expand Down

0 comments on commit 0734f8c

Please sign in to comment.