-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f160da9
commit d1fa267
Showing
16 changed files
with
705 additions
and
527 deletions.
There are no files selected for viewing
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
76 changes: 68 additions & 8 deletions
76
common/src/main/java/dev/latvian/mods/kubejs/core/TagLoaderKJS.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,98 @@ | ||
package dev.latvian.mods.kubejs.core; | ||
|
||
import com.google.gson.JsonArray; | ||
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.DataExport; | ||
import dev.latvian.mods.kubejs.server.ServerScriptManager; | ||
import dev.latvian.mods.kubejs.server.TagEventJS; | ||
import dev.latvian.mods.kubejs.server.tag.TagEventFilter; | ||
import dev.latvian.mods.kubejs.server.tag.TagEventJS; | ||
import dev.latvian.mods.kubejs.server.tag.TagWrapper; | ||
import dev.latvian.mods.kubejs.util.ConsoleJS; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.tags.TagLoader; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public interface TagLoaderKJS<T> { | ||
default void kjs$customTags(Map<ResourceLocation, List<TagLoader.EntryWithSource>> map) { | ||
default void kjs$customTags(ServerScriptManager ssm, Map<ResourceLocation, List<TagLoader.EntryWithSource>> map) { | ||
TagContext.INSTANCE.setValue(TagContext.EMPTY); | ||
var reg = kjs$getRegistry(); | ||
|
||
if (reg == null) { | ||
return; | ||
} | ||
var regInfo = RegistryInfo.MAP.get(reg.key()); | ||
|
||
if ((regInfo != null && regInfo.hasDefaultTags) || ServerEvents.TAGS.hasListeners(reg.key())) { | ||
var dir = kjs$getDirectory(); | ||
new TagEventJS<>(dir, map, reg).post(ServerScriptManager.instance == null ? null : ServerScriptManager.instance.tagEventHolders.get(reg.key())); | ||
var regInfo = RegistryInfo.of(reg.key()); | ||
|
||
if (regInfo.hasDefaultTags || ServerEvents.TAGS.hasListeners(reg.key())) { | ||
var preEvent = ssm.preTagEvents.get(reg.key()); | ||
|
||
var event = new TagEventJS(regInfo, reg); | ||
|
||
for (var entry : map.entrySet()) { | ||
var w = new TagWrapper(event, entry.getKey(), entry.getValue()); | ||
event.tags.put(w.id, w); | ||
|
||
if (ConsoleJS.SERVER.shouldPrintDebug()) { | ||
ConsoleJS.SERVER.debug("Tags %s/#%s; %d".formatted(regInfo, w.id, w.entries.size())); | ||
} | ||
} | ||
|
||
for (var builder : regInfo.objects.values()) { | ||
for (var s : builder.defaultTags) { | ||
event.add(s, new TagEventFilter.ID(builder.id)); | ||
} | ||
} | ||
|
||
if (preEvent == null) { | ||
ServerEvents.TAGS.post(event, regInfo.key, TagEventJS.TAG_EVENT_HANDLER); | ||
} else { | ||
for (var a : preEvent.actions) { | ||
a.accept(event); | ||
} | ||
} | ||
|
||
map.clear(); | ||
|
||
for (var entry : event.tags.entrySet()) { | ||
map.put(entry.getKey(), entry.getValue().entries); | ||
} | ||
|
||
if (DataExport.export != null) { | ||
var loc = "tags/" + regInfo + "/"; | ||
|
||
for (var entry : map.entrySet()) { | ||
var list = new ArrayList<String>(); | ||
|
||
for (var e : entry.getValue()) { | ||
list.add(e.entry().toString()); | ||
} | ||
|
||
list.sort(String.CASE_INSENSITIVE_ORDER); | ||
var arr = new JsonArray(); | ||
|
||
for (var e : list) { | ||
arr.add(e); | ||
} | ||
|
||
DataExport.export.addJson(loc + entry.getKey() + ".json", arr); | ||
} | ||
} | ||
|
||
if (event.totalAdded > 0 || event.totalRemoved > 0 || ConsoleJS.SERVER.shouldPrintDebug()) { | ||
ConsoleJS.SERVER.info("[%s] Found %d tags, added %d objects, removed %d objects".formatted(regInfo, event.tags.size(), event.totalAdded, event.totalRemoved)); | ||
} | ||
} | ||
} | ||
|
||
void kjs$setRegistry(Registry<T> registry); | ||
|
||
@Nullable | ||
Registry<T> kjs$getRegistry(); | ||
|
||
String kjs$getDirectory(); | ||
} |
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
18 changes: 18 additions & 0 deletions
18
common/src/main/java/dev/latvian/mods/kubejs/event/EventExceptionHandler.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.latvian.mods.kubejs.event; | ||
|
||
import dev.latvian.mods.rhino.WrappedException; | ||
|
||
@FunctionalInterface | ||
public interface EventExceptionHandler { | ||
/** | ||
* Handles an exception thrown by an event handler. | ||
* | ||
* @param event The event being posted | ||
* @param container The event handler container that threw the exception | ||
* @param ex The exception that was thrown | ||
* @return <code>null</code> if the exception could be recovered from, otherwise the exception that should be rethrown | ||
* @implNote The thrown exception will never be an instance of {@link EventExit} or {@link WrappedException}, | ||
* as those are already handled by the container itself. | ||
*/ | ||
Throwable handle(EventJS event, EventHandlerContainer container, Throwable ex); | ||
} |
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
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
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
84 changes: 0 additions & 84 deletions
84
common/src/main/java/dev/latvian/mods/kubejs/server/FakeTagEventJS.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.