Skip to content

Commit

Permalink
2.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
westernat committed Feb 14, 2024
1 parent ea3b416 commit c183c7a
Show file tree
Hide file tree
Showing 20 changed files with 274 additions and 97 deletions.
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-2.7.0
mod_version=2001fabric-2.8.0
maven_group=org.mesdag.advjs
archives_base_name=AdvJS

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/mesdag/advjs/AdvJSPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import java.nio.file.Files;

public class AdvJSPlugin extends KubeJSPlugin {
static boolean DEBUG;
static boolean DEBUG = false;

@Override
public void registerBindings(BindingsEvent event) {
Expand All @@ -24,6 +24,7 @@ public void registerBindings(BindingsEvent event) {
event.add("GameMode", GameTypeWrapper.class);
event.add("Bounds", Bounds.class);
event.add("CustomTriggers", CustomTriggers.class);
event.add("AdvHelper", AdvHelper.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public AdvBuilder create(Identifier rootPath) {
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.
@param mod: the mod id of advancement.
@param icon: the icon of advancement widget.
@param frame: type of frame for the icon. Available value is 'challenge', 'goal' or 'task'.
@param parent: the parent advancement id of this advancement.
""")
public void remove(AdvancementFilter filter) {
if (filter.fail()) {
Expand Down
43 changes: 28 additions & 15 deletions src/main/java/org/mesdag/advjs/advancement/AdvLockEventJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,43 @@

import dev.latvian.mods.kubejs.event.EventJS;
import dev.latvian.mods.kubejs.typings.Info;
import dev.latvian.mods.kubejs.typings.Param;
import net.minecraft.predicate.item.ItemPredicate;
import net.minecraft.block.Block;
import net.minecraft.entity.EntityType;
import net.minecraft.item.Item;
import net.minecraft.recipe.Ingredient;
import net.minecraft.util.Identifier;
import org.mesdag.advjs.predicate.ItemPredicateBuilder;
import org.mesdag.advjs.util.ItemSetter;

import static org.mesdag.advjs.util.Data.LOCK_RESULT;
import java.util.function.Consumer;

import static org.mesdag.advjs.util.Data.*;

public class AdvLockEventJS extends EventJS implements ItemSetter {
@Info(value = "Lock recipe by advancement. It will only deny player take the result from GUI.",
params = {
@Param(name = "toLock"),
@Param(name = "lockBy")
})
@Info("Deny player take specific result. It only available in GUI.")
public void result(Ingredient toLock, Identifier lockBy) {
LOCK_RESULT.put(wrapItem(toLock), lockBy);
}

@Info(value = "Lock recipe by advancement. It will only deny player take the result from GUI.",
params = {
@Param(name = "toLock"),
@Param(name = "lockBy")
})
public void result(ItemPredicate toLock, Identifier lockBy) {
LOCK_RESULT.put(toLock, lockBy);
@Info("Deny player take specific result. It only available in GUI.")
public void result(Consumer<ItemPredicateBuilder> consumer, Identifier lockBy) {
ItemPredicateBuilder builder = new ItemPredicateBuilder();
consumer.accept(builder);
LOCK_RESULT.put(builder.build(), lockBy);
}

@Info("Pass item use or use on block.")
public void itemUse(Item toLock, Identifier lockBy) {
LOCK_ITEM_USE.put(toLock, lockBy);
}

@Info("Deny player interact with specific block.")
public void blockInteract(Block toLock, Identifier lockBy) {
LOCK_BLOCK_INTERACT.put(toLock, lockBy);
}

@Info("pass player interact with specific type of entities.")
public void entityInteract(EntityType<?> toLock, Identifier lockBy) {
LOCK_ENTITY_INTERACT.put(toLock, lockBy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public CriteriaBuilder(Map<String, AdvancementCriterion> criteria) {
@Param(name = "name", value = "The name of this trigger."),
@Param(name = "trigger", value = "The trigger itself.")
})
public <Trigger extends CriterionConditions> void add(String name, Trigger instance) {
public void add(String name, CriterionConditions instance) {
criteria.put(name, new AdvancementCriterion(instance));
}

@Info("Add a nameless trigger for this advancement.")
public <Trigger extends CriterionConditions> void add(Trigger instance) {
public void add(CriterionConditions instance) {
criteria.put(UUID.randomUUID().toString(), new AdvancementCriterion(instance));
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/mesdag/advjs/command/AdvCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2186,8 +2186,10 @@ function lookAtThroughSpyglass(entityType) {
})
AdvJSEvents.lock(event => {
// Lock recipe by advancement
event.result("stone_slab", "minecraft:story/smelt_iron");
event.itemUse("spyglass", "minecraft:story/smelt_iron");
event.blockInteract("chest", "minecraft:story/smelt_iron");
event.entityInteract("villager", "minecraft:story/smelt_iron");
})
PlayerEvents.advancement(event => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.mesdag.advjs.mixin;

import net.minecraft.block.AbstractBlock;
import net.minecraft.block.Block;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.world.World;
import org.mesdag.advjs.util.AdvHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static org.mesdag.advjs.util.Data.LOCK_BLOCK_INTERACT;

@Mixin(AbstractBlock.AbstractBlockState.class)
public abstract class AbstractBlock$AbstractBlockStateMixin {
@Shadow public abstract Block getBlock();

@Inject(method = "onUse", at = @At("HEAD"), cancellable = true)
private void advJS$pass(World world, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir) {
if (world.isClient) return;
if (!AdvHelper.advDone((ServerPlayerEntity) player, LOCK_BLOCK_INTERACT.get(getBlock()))) cir.setReturnValue(ActionResult.PASS);
}
}
47 changes: 47 additions & 0 deletions src/main/java/org/mesdag/advjs/mixin/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.mesdag.advjs.mixin;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUsageContext;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import org.mesdag.advjs.util.AdvHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

import static org.mesdag.advjs.util.Data.LOCK_ITEM_USE;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin {
@Shadow
public abstract Item getItem();

@Inject(
method = "useOnBlock",
at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemUsageContext;getBlockPos()Lnet/minecraft/util/math/BlockPos;"),
locals = LocalCapture.CAPTURE_FAILSOFT,
cancellable = true)
private void advJS$useOn(ItemUsageContext context, CallbackInfoReturnable<ActionResult> cir, PlayerEntity player) {
if (player instanceof ServerPlayerEntity serverPlayer && !AdvHelper.advDone(serverPlayer, LOCK_ITEM_USE.get(getItem()))) {
cir.setReturnValue(ActionResult.PASS);
}
}

@Inject(method = "use", at = @At("HEAD"), cancellable = true)
private void advJS$use(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
if (world.isClient) {
if (AdvHelper.clientAdvDone(LOCK_ITEM_USE.get(getItem()))) return;
} else if (AdvHelper.advDone((ServerPlayerEntity) user, LOCK_ITEM_USE.get(getItem()))) {
return;
}
cir.setReturnValue(TypedActionResult.pass((ItemStack) (Object) this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.mesdag.advjs.AdvJS;
import org.mesdag.advjs.util.AdvHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -28,10 +29,10 @@ public abstract class PlayerAdvancementTrackerMixin {
@Shadow public abstract boolean revokeCriterion(Advancement advancement, String criterionName);

@Inject(method = "grantCriterion", at = @At("HEAD"), cancellable = true)
private void advJS$checkParentDone(Advancement advancement, String criterionName, CallbackInfoReturnable<Boolean> cir) {
private void advJS$checkDone(Advancement advancement, String criterionName, CallbackInfoReturnable<Boolean> cir) {
Identifier id = advancement.getId();
if (!REQUIRE_DONE.containsKey(id)) return;
Identifier[] requires = REQUIRE_DONE.get(id);
if (requires==null) return;
if (requires.length == 0) {
ConsoleJS.SERVER.error("AdvJS/requireDone: Invalid requires[] of '%s', which length is 0".formatted(id));
return;
Expand All @@ -40,12 +41,8 @@ public abstract class PlayerAdvancementTrackerMixin {
ServerAdvancementLoader loader = owner.server.getAdvancementLoader();
for (Identifier requireId : requires) {
Advancement required = requireId == AdvJS.PARENT ? advancement.getParent() : loader.get(requireId);
if (required == null) {
ConsoleJS.SERVER.warn("AdvJS/requireDone: Advancement '%s' is not exist, so '%s' will not check it".formatted(requireId, id));
continue;
}

if (!getProgress(required).isDone()) {
String nullMsg = "AdvJS/requireDone: Advancement '%s' is not exist, so '%s' will not check it".formatted(requireId, id);
if (!AdvHelper.advDone(owner, required, nullMsg)) {
cir.setReturnValue(false);
return;
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/mesdag/advjs/mixin/PlayerEntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import org.mesdag.advjs.trigger.custom.CriteriaTriggers;
import org.mesdag.advjs.util.AdvHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static org.mesdag.advjs.util.Data.LOCK_ENTITY_INTERACT;

@Mixin(PlayerEntity.class)
public abstract class PlayerEntityMixin {
Expand All @@ -18,4 +24,12 @@ public abstract class PlayerEntityMixin {
CriteriaTriggers.PLAYER_TOUCH.trigger(serverPlayer, entity);
}
}

@Inject(method = "interact", at = @At("HEAD"), cancellable = true)
private void advJS$interact(Entity entity, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
PlayerEntity self = (PlayerEntity) (Object) this;
if (self instanceof ServerPlayerEntity serverPlayer && !AdvHelper.advDone(serverPlayer, LOCK_ENTITY_INTERACT.get(entity.getType()))) {
cir.setReturnValue(ActionResult.PASS);
}
}
}
25 changes: 17 additions & 8 deletions src/main/java/org/mesdag/advjs/mixin/ScreenHandlerMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
import org.mesdag.advjs.AdvJS;
import org.mesdag.advjs.util.AdvHelper;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -30,17 +31,25 @@ public abstract class ScreenHandlerMixin {

@Inject(method = "internalOnSlotClick", at = @At("HEAD"), cancellable = true)
private void advJS$checkAdvancement(int slotIndex, int button, SlotActionType actionType, PlayerEntity player, CallbackInfo ci) {
if (slotIndex < 0) return;
if (slotIndex < 0 || !(player instanceof ServerPlayerEntity serverPlayer)) return;

// ResourceLocation slot_lock_by = LOCK_SLOT.get(index);
// if (LOCK_SLOT.containsKey(index) && AdvHelper.advNotDone(serverPlayer, slot_lock_by)) {
// ci.cancel();
// AdvJS.debugInfo("AdvJS/lockSlot: slot index '%s' has locked by '%s'".formatted(index, slot_lock_by));
// return;
// }

Slot slot = slots.get(slotIndex);
if (!(slot.inventory instanceof CraftingResultInventory)) return;

if (actionType == SlotActionType.PICKUP && player instanceof ServerPlayerEntity serverPlayer) {
ItemStack result = slot.getStack();
for (Map.Entry<ItemPredicate, Identifier> entry : LOCK_RESULT.entrySet()){
if(entry.getKey().test(result) && AdvHelper.advNotDone(serverPlayer, entry.getValue())){
ci.cancel();
return;
}
ItemStack result = slot.getStack();
for (Map.Entry<ItemPredicate, Identifier> entry : LOCK_RESULT.entrySet()) {
Identifier result_lock_by = entry.getValue();
if (entry.getKey().test(result) && !AdvHelper.advDone(serverPlayer, result_lock_by)) {
ci.cancel();
AdvJS.debugInfo("AdvJS/lockResult: result '%s' has locked by '%s'".formatted(result.getItem().kjs$getId(), result_lock_by));
return;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.google.gson.JsonObject;
import dev.latvian.mods.kubejs.util.ConsoleJS;
import net.minecraft.advancement.*;
import net.minecraft.item.Item;
import net.minecraft.loot.LootManager;
import net.minecraft.predicate.entity.AdvancementEntityPredicateDeserializer;
import net.minecraft.resource.ResourceManager;
Expand Down Expand Up @@ -57,12 +56,10 @@ public abstract class ServerAdvancementLoaderMixin {
for (AdvancementFilter filter : FILTERS) {
if (filter.isResolved()) continue;

String parent = advJson.has("parent") ? advJson.get("parent").getAsString() : null;
Identifier parent = advJson.has("parent") ? new Identifier(advJson.get("parent").getAsString()) : null;
if (advJson.has("display")) {
JsonObject display = advJson.get("display").getAsJsonObject();
Item item = display.has("icon") ? JsonHelper.getItem(display.get("icon").getAsJsonObject(), "item", null) : null;
String frame = display.has("frame") ? display.get("frame").getAsString() : null;
if (filter.matches(key, item, frame, parent)) {
AdvancementDisplay displayInfo = AdvancementDisplay.fromJson(JsonHelper.getObject(advJson, "display"));
if (filter.matches(key, displayInfo.getIcon(), displayInfo.getFrame(), parent)) {
builder.add(key);
}
} else if (filter.matches(key, null, null, parent)) {
Expand All @@ -76,13 +73,13 @@ public abstract class ServerAdvancementLoaderMixin {
counter++;
}

ConsoleJS.SERVER.info("AdvJS: Removed '%s' advancements".formatted(counter));
ConsoleJS.SERVER.info("AdvJS: Removed %s advancements".formatted(counter));
}

@Inject(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", shift = At.Shift.BEFORE),
locals = LocalCapture.CAPTURE_FAILSOFT)
private void advjs$modify_add(Map<Identifier, JsonElement> map, ResourceManager resourceManager, Profiler profiler, CallbackInfo ci, Map<Identifier, Advancement.Builder> map2, AdvancementManager advancementManager) {
private void advJS$modify_add(Map<Identifier, JsonElement> map, ResourceManager resourceManager, Profiler profiler, CallbackInfo ci, Map<Identifier, Advancement.Builder> map2, AdvancementManager advancementManager) {
advJS$modify(map2, conditionManager);
advJS$add(map2);
ConsoleJS.SERVER.info("AdvJS: Completely loaded!");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.mesdag.advjs.mixin.client;

import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.AdvancementProgress;
import net.minecraft.client.network.ClientAdvancementManager;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.Map;

@Mixin(ClientAdvancementManager.class)
public interface ClientAdvancementsAccessor {
@Accessor
Map<Advancement, AdvancementProgress> getAdvancementProgresses();
}
Loading

0 comments on commit c183c7a

Please sign in to comment.