Skip to content

Commit

Permalink
add RemoveFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
westernat committed Jan 9, 2024
1 parent b9e49b5 commit 02c471e
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 26 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ ServerEvents.advancement((event) => {
.requireParentDone()
});

// Remove an exist advancement
event.remove("minecraft:story/lava_bucket");
// Remove an exist advancement by RemoveFilter, available filter was writen in doc.
// you can also use like this: 'event.remove("minecraft:story/lava_bucket");'
event.remove({
icon: "minecraft:lava_bucket"
});

// Modify an exist advancement
event.get("minecraft:story/smelt_iron")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.20.1+build.10
loader_version=0.15.3

# Mod Properties
mod_version=2001fabric-1.7.1
mod_version=2001fabric-1.8.0
maven_group=org.mesdag.advjs
archives_base_name=AdvJS

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/mesdag/advjs/AdvJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.nio.file.Path;

public class AdvJS implements ModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger("advjs");
public static final Logger LOGGER = LoggerFactory.getLogger("AdvJS");
public static final EventHandler ADVANCEMENT = ServerEvents.GROUP.server("advancement", () -> AdvConfigureEvent.class);
public static final Path EXAMPLE = KubeJSPaths.SERVER_SCRIPTS.resolve("advancement.js");

Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/mesdag/advjs/AdvJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void registerBindings(BindingsEvent event) {

@Override
public void onServerReload() {
REMOVES.clear();
FILTERS.clear();
GETTER_MAP.clear();
BUILDER_MAP.clear();
LOCK_MAP.clear();
Expand Down Expand Up @@ -108,8 +108,11 @@ private static void example(boolean generate) {
.requireParentDone()
});
// Remove an exist advancement
event.remove("minecraft:story/lava_bucket")
// Remove an exist advancement by RemoveFilter, available filter was writen in doc.
// you can also remove like this: 'event.remove("minecraft:story/lava_bucket");'
event.remove({
icon: "minecraft:lava_bucket"
})
// Modify an exist advancement
event.get("minecraft:story/smelt_iron")
Expand Down
23 changes: 19 additions & 4 deletions src/main/java/org/mesdag/advjs/configure/AdvConfigureEvent.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.mesdag.advjs.configure;

import com.google.gson.JsonElement;
import dev.latvian.mods.kubejs.event.EventJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.typings.Param;
Expand All @@ -9,8 +10,8 @@
import org.mesdag.advjs.trigger.Trigger;
import org.mesdag.advjs.util.Bounds;

import static org.mesdag.advjs.configure.Data.FILTERS;
import static org.mesdag.advjs.configure.Data.LOCK_MAP;
import static org.mesdag.advjs.configure.Data.REMOVES;

public class AdvConfigureEvent extends EventJS {
@Info("""
Expand All @@ -29,9 +30,23 @@ public AdvBuilder create(Identifier rootPath) {
return new AdvBuilder(null, "root", rootPath, false);
}

@Info("Remove an exist advancement.")
public void remove(Identifier remove) {
REMOVES.add(remove);
@Info("""
It will automatically remove all of its children.
If you put in a string, it will remove advancement by its path.
Else if you put in a json object, it will remove advancement by filter:
modid: the mod id of advancement.
icon: the id of icon item/block.
frame: type of frame for the icon. Available value is 'challenge', 'goal' or 'task'.
parent: the parent advancement path of this advancement.
""")
public void remove(JsonElement jsonElement) {
FILTERS.add(RemoveFilter.of(jsonElement));
}

@Info("Get an exist advancement to modify.")
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/mesdag/advjs/configure/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class Data {
public static final ArrayList<Identifier> REMOVES = new ArrayList<>();
public static final HashSet<RemoveFilter> FILTERS = new HashSet<>();
public static final HashMap<Identifier, AdvGetter> GETTER_MAP = new HashMap<>();
public static final HashMap<Identifier, AdvBuilder> BUILDER_MAP = new HashMap<>();
public static final HashMap<Item, Identifier> LOCK_MAP = new HashMap<>();
Expand Down
74 changes: 74 additions & 0 deletions src/main/java/org/mesdag/advjs/configure/RemoveFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.mesdag.advjs.configure;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.registry.Registries;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.mesdag.advjs.AdvJS;


public class RemoveFilter {
@Nullable Identifier path;
@Nullable String modid;
@Nullable Item icon;
@Nullable String frame;
@Nullable String parent;

public static RemoveFilter of(JsonElement jsonElement) {
RemoveFilter filter = new RemoveFilter();
if (jsonElement.isJsonObject()) {
JsonObject object = jsonElement.getAsJsonObject();
if (object.has("mod")) {
// TODO if check mod loaded
filter.modid = object.get("mod").getAsString();
}

if (object.has("icon")) {
String icon = object.get("icon").getAsString();
Identifier id = new Identifier(icon);
Item item = Registries.ITEM.containsId(id) ? Registries.ITEM.get(id) : Items.AIR;
if (item != Items.AIR) {
filter.icon = item;
} else {
AdvJS.LOGGER.warn("RemoveFilter: Icon '{}' not found", icon);
}
}

if (object.has("frame")) {
filter.frame = object.get("frame").getAsString();
}

if (object.has("parent")) {
filter.parent = object.get("parent").getAsString();
}
} else if (jsonElement.isJsonPrimitive()) {
filter.path = new Identifier(jsonElement.getAsString());
}

return filter;
}

public boolean matches(Identifier path, Item icon, String frame, String parent) {
if (this.path != null && !this.path.equals(path)) {
return false;
} else {
boolean flag = this.modid != null || this.icon != null || this.frame != null || this.parent != null;
if (flag && this.modid != null) {
flag = this.modid.equals(path.getNamespace());
}
if (flag && this.icon != null) {
flag = this.icon == icon;
}
if (flag && this.frame != null) {
flag = this.frame.equals(frame);
}
if (flag && this.parent != null) {
flag = this.parent.equals(parent);
}
return flag;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package org.mesdag.advjs.mixin;

import com.google.common.collect.ImmutableSet;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.AdvancementCriterion;
import net.minecraft.advancement.AdvancementDisplay;
import net.minecraft.advancement.AdvancementRewards;
import net.minecraft.item.Item;
import net.minecraft.loot.LootManager;
import net.minecraft.predicate.entity.AdvancementEntityPredicateDeserializer;
import net.minecraft.resource.ResourceManager;
import net.minecraft.server.ServerAdvancementLoader;
import net.minecraft.server.function.CommandFunction;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.profiler.Profiler;
import org.mesdag.advjs.AdvJS;
import org.mesdag.advjs.AdvJSPlugin;
import org.mesdag.advjs.configure.*;
Expand All @@ -20,7 +25,9 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Map;

Expand All @@ -32,29 +39,60 @@ public abstract class ServerAdvancementLoaderMixin {
@Final
private LootManager conditionManager;

@Inject(method = "apply(Ljava/util/Map;Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/util/profiler/Profiler;)V", at = @At("HEAD"))
private void advJS$remove(Map<Identifier, JsonElement> map, ResourceManager resourceManager, Profiler profiler, CallbackInfo ci) {
AdvJS.ADVANCEMENT.post(new AdvConfigureEvent());

int counter = 0;
ImmutableSet.Builder<Identifier> builder = new ImmutableSet.Builder<>();
for (Map.Entry<Identifier, JsonElement> entry : map.entrySet()) {
Identifier key = entry.getKey();
if (key.getPath().startsWith("recipe")) {
// Filter all recipe advancement
continue;
}

JsonObject value = entry.getValue().getAsJsonObject();
for (RemoveFilter filter : FILTERS) {
Item item;
String frame;
if (value.has("display")) {
JsonObject display = value.get("display").getAsJsonObject();
item = display.has("icon") ? JsonHelper.getItem(display.get("icon").getAsJsonObject(), "item", null) : null;
frame = display.has("frame") ? display.get("frame").getAsString() : "task";
} else {
continue;
}

String parent = null;
if (value.has("parent")) {
parent = value.get("parent").getAsString();
}

if (filter.matches(key, item, frame, parent)) {
builder.add(key);
}
}
}

for (Identifier remove : builder.build()) {
map.remove(remove);
counter++;
}

AdvJS.LOGGER.info("Removed {} advancements", counter);
}

@ModifyArg(
method = "apply(Ljava/util/Map;Lnet/minecraft/resource/ResourceManager;Lnet/minecraft/util/profiler/Profiler;)V",
at = @At(value = "INVOKE", target = "Lnet/minecraft/advancement/AdvancementManager;load(Ljava/util/Map;)V"))
private Map<Identifier, Advancement.Builder> advjs$reload(Map<Identifier, Advancement.Builder> map) {
AdvJS.ADVANCEMENT.post(new AdvConfigureEvent());
advJS$remove(map);
advJS$modify(map, conditionManager);
advJS$add(map);
AdvJS.LOGGER.info("AdvJS Loaded!");
return map;
}

@Unique
private static void advJS$remove(Map<Identifier, Advancement.Builder> map) {
int counter = 0;
for (Identifier remove : REMOVES) {
if (map.remove(remove) != null) {
counter++;
}
}
AdvJS.LOGGER.info("Removed {} advancements", counter);
}

@Unique
private static void advJS$modify(Map<Identifier, Advancement.Builder> map, LootManager predicateManager) {
if (AdvJSPlugin.DEBUG) {
Expand Down

0 comments on commit 02c471e

Please sign in to comment.