Skip to content

Commit

Permalink
feat: Add function for detecting Minecraft status effects [skip ci] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
WindPerhaps authored Jan 11, 2025
1 parent bd56e98 commit 679cce6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2024.
* Copyright © Wynntils 2022-2025.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.core.consumers.functions;
Expand Down Expand Up @@ -553,6 +553,7 @@ private void registerAllFunctions() {
registerFunction(new MinecraftFunctions.DirFunction());
registerFunction(new MinecraftFunctions.FpsFunction());
registerFunction(new MinecraftFunctions.KeyPressedFunction());
registerFunction(new MinecraftFunctions.MinecraftEffectDurationFunction());
registerFunction(new MinecraftFunctions.MyLocationFunction());
registerFunction(new MinecraftFunctions.TicksFunction());

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © Wynntils 2022-2024.
* Copyright © Wynntils 2022-2025.
* This file is released under LGPLv3. See LICENSE for full license details.
*/
package com.wynntils.functions;
Expand All @@ -11,6 +11,11 @@
import com.wynntils.utils.mc.McUtils;
import com.wynntils.utils.mc.type.Location;
import java.util.List;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance;

public class MinecraftFunctions {
public static class MyLocationFunction extends Function<Location> {
Expand Down Expand Up @@ -59,4 +64,34 @@ public FunctionArguments.Builder getArgumentsBuilder() {
List.of(new FunctionArguments.Argument<>("keyCode", Integer.class, null)));
}
}

public static class MinecraftEffectDurationFunction extends Function<Integer> {
@Override
public Integer getValue(FunctionArguments arguments) {
String effectName = arguments.getArgument("effectName").getStringValue();
ResourceLocation effectLocation = ResourceLocation.withDefaultNamespace(effectName);

Holder<MobEffect> effectHolder =
BuiltInRegistries.MOB_EFFECT.getHolder(effectLocation).orElse(null);

if (effectHolder == null) return -1; // Effect holder not found

// Check if the player has the effect
if (McUtils.player().hasEffect(effectHolder)) {
MobEffectInstance effectInstance = McUtils.player().getEffect(effectHolder);

if (effectInstance != null && effectInstance.getDuration() >= 0) {
return effectInstance.getDuration();
}
}

return -1; // Effect not active
}

@Override
public FunctionArguments.Builder getArgumentsBuilder() {
return new FunctionArguments.RequiredArgumentBuilder(
List.of(new FunctionArguments.Argument<>("effectName", String.class, null)));
}
}
}
3 changes: 3 additions & 0 deletions common/src/main/resources/assets/wynntils/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -2066,6 +2066,9 @@
"function.wynntils.memPct.name": "Mem[%%]",
"function.wynntils.memUsed.description": "Current amount of memory used by the JVM",
"function.wynntils.memUsed.name": "Mem[used]",
"function.wynntils.minecraftEffectDuration.argument.effectName": "The Minecraft effect identifier",
"function.wynntils.minecraftEffectDuration.description": "Returns the duration left of the specified Minecraft effect if it is currently active, -1 otherwise",
"function.wynntils.minecraftEffectDuration.name": "Minecraft Effect Duration",
"function.wynntils.mobTotem.argument.totemNumber": "The number of the mob totem to get the location of",
"function.wynntils.mobTotem.description": "The location of the mob totem",
"function.wynntils.mobTotem.name": "Mob Totem Location",
Expand Down

0 comments on commit 679cce6

Please sign in to comment.