Skip to content

Commit

Permalink
Backported tag error patch from 1.20
Browse files Browse the repository at this point in the history
  • Loading branch information
LatvianModder committed Sep 5, 2023
1 parent 414f412 commit 6394542
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.latvian.mods.kubejs.bindings.event.ServerEvents;
import dev.latvian.mods.kubejs.item.ingredient.TagContext;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.server.ServerScriptManager;
import dev.latvian.mods.kubejs.server.TagEventJS;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -24,7 +25,7 @@ public interface TagLoaderKJS<T> {

if ((regInfo != null && regInfo.hasDefaultTags) || ServerEvents.TAGS.hasListeners(reg.key())) {
var dir = kjs$getDirectory();
new TagEventJS<>(dir, map, reg).post();
new TagEventJS<>(dir, map, reg).post(ServerScriptManager.instance == null ? null : ServerScriptManager.instance.tagEventHolders.get(reg.key()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package dev.latvian.mods.kubejs.server;

import dev.latvian.mods.kubejs.event.EventJS;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import net.minecraft.resources.ResourceLocation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;

public class FakeTagEventJS extends EventJS {
public class FakeTagWrapper {
public final ResourceLocation id;

private FakeTagWrapper(ResourceLocation i) {
id = i;
invalid = false;
}

@Override
public String toString() {
return "<%s / %s>".formatted(getType(), id);
}

public FakeTagWrapper add(String... ids) {
actions.add(e -> e.get(id).add(ids));
return this;
}

public FakeTagWrapper remove(String... ids) {
actions.add(e -> e.remove(id).add(ids));
return this;
}

public FakeTagWrapper removeAll() {
actions.add(e -> e.get(id).removeAll());
return this;
}

public Collection<ResourceLocation> getObjectIds() {
invalid = true;
return Set.of();
}
}

public final RegistryInfo registry;
public final Map<ResourceLocation, FakeTagWrapper> tags;
public final List<Consumer<TagEventJS<?>>> actions;
public boolean invalid;

public FakeTagEventJS(RegistryInfo registry) {
this.registry = registry;
this.tags = new ConcurrentHashMap<>();
this.actions = new ArrayList<>();
}

public ResourceLocation getType() {
return registry.key.location();
}

public FakeTagWrapper get(ResourceLocation id) {
return tags.computeIfAbsent(id, FakeTagWrapper::new);
}

public FakeTagWrapper add(ResourceLocation tag, String... ids) {
return get(tag).add(ids);
}

public FakeTagWrapper remove(ResourceLocation tag, String... ids) {
return get(tag).remove(ids);
}

public FakeTagWrapper removeAll(ResourceLocation tag) {
return get(tag).removeAll();
}

public void removeAllTagsFrom(String... ignored) {
actions.add(e -> e.removeAllTagsFrom(ignored));
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package dev.latvian.mods.kubejs.server;

import dev.latvian.mods.kubejs.DevProperties;
import dev.latvian.mods.kubejs.KubeJS;
import dev.latvian.mods.kubejs.KubeJSPaths;
import dev.latvian.mods.kubejs.KubeJSPlugin;
import dev.latvian.mods.kubejs.bindings.event.ServerEvents;
import dev.latvian.mods.kubejs.platform.RecipePlatformHelper;
import dev.latvian.mods.kubejs.recipe.RecipesEventJS;
import dev.latvian.mods.kubejs.recipe.ingredientaction.CustomIngredientAction;
import dev.latvian.mods.kubejs.recipe.special.SpecialRecipeSerializerManager;
import dev.latvian.mods.kubejs.registry.RegistryInfo;
import dev.latvian.mods.kubejs.script.ScriptManager;
import dev.latvian.mods.kubejs.script.ScriptType;
import dev.latvian.mods.kubejs.script.data.DataPackEventJS;
import dev.latvian.mods.kubejs.script.data.KubeJSFolderPackResources;
import dev.latvian.mods.kubejs.script.data.VirtualKubeJSDataPack;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import dev.latvian.mods.kubejs.util.KubeJSPlugins;
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.ReloadableServerResources;
import net.minecraft.server.packs.FilePackResources;
import net.minecraft.server.packs.PackType;
Expand All @@ -24,7 +28,9 @@
import java.nio.file.Files;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

public class ServerScriptManager {
public static ServerScriptManager instance;
Expand All @@ -34,6 +40,7 @@ public static ScriptManager getScriptManager() {
}

private final ScriptManager scriptManager = new ScriptManager(ScriptType.SERVER, KubeJSPaths.SERVER_SCRIPTS);
public final Map<ResourceKey<?>, FakeTagEventJS> tagEventHolders = new ConcurrentHashMap<>();

public ServerScriptManager() {
try {
Expand Down Expand Up @@ -91,6 +98,28 @@ public MultiPackResourceManager wrapResourceManager(CloseableResourceManager ori
ServerEvents.SPECIAL_RECIPES.post(ScriptType.SERVER, SpecialRecipeSerializerManager.INSTANCE);
KubeJSPlugins.forEachPlugin(KubeJSPlugin::onServerReload);

tagEventHolders.clear();

if (ServerEvents.TAGS.hasListeners()) {
for (var id : ServerEvents.TAGS.findUniqueExtraIds(ScriptType.SERVER)) {
var e = new FakeTagEventJS(RegistryInfo.of((ResourceKey) id));
try {
ServerEvents.TAGS.post(ScriptType.SERVER, id, e);
} catch (Exception ex) {
e.invalid = true;

if (DevProperties.get().debugInfo) {
KubeJS.LOGGER.warn("Fake Tag event for " + e.registry + " failed:");
ex.printStackTrace();
}
}

if (!e.invalid) {
tagEventHolders.put(e.registry.key, e);
}
}
}

if (ServerEvents.RECIPES.hasListeners()) {
RecipesEventJS.instance = new RecipesEventJS();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagEntry;
import net.minecraft.tags.TagLoader;
import org.jetbrains.annotations.Nullable;

import java.nio.file.Files;
import java.text.DateFormat;
Expand Down Expand Up @@ -221,7 +222,7 @@ public ResourceLocation getType() {
return registry.key().location();
}

public void post() {
public void post(@Nullable FakeTagEventJS fakeEvent) {
var dumpFile = KubeJSPaths.EXPORT.resolve("tags/" + getType().getNamespace() + "/" + getType().getPath() + ".txt");

if (!Files.exists(dumpFile)) {
Expand Down Expand Up @@ -264,7 +265,13 @@ public void post() {
}
}

ServerEvents.TAGS.post(this, registry.key(), TAG_EVENT_HANDLER);
if (fakeEvent == null) {
ServerEvents.TAGS.post(this, registry.key(), TAG_EVENT_HANDLER);
} else {
for (var a : fakeEvent.actions) {
a.accept(this);
}
}

if (DataExport.export != null) {
var loc = "tags/" + getType().toString() + "/";
Expand Down

0 comments on commit 6394542

Please sign in to comment.