Skip to content

Commit

Permalink
More entity fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
melontini committed Mar 31, 2024
1 parent 92a61d8 commit 097b9ba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/me/melontini/commander/Commander.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

//TODO:
// Better validation during `apply`
// Wrap most common/server fabric events.
// Macros with dynamic fields
public class Commander implements ModInitializer {

public static Identifier id(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.google.common.collect.ImmutableMap;
import me.melontini.commander.util.functions.ToDoubleFunction;
import me.melontini.commander.util.macro.MacroContainer;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -17,16 +20,36 @@ public class MacroBuilder {

public static Consumer<MacroBuilder> forEntity() {
return builder -> builder
.string("uuid", source -> Objects.requireNonNull(source.getEntity()).getUuidAsString())
.arithmetic("vel/x", source -> Objects.requireNonNull(source.getEntity()).getVelocity().x)
.arithmetic("vel/y", source -> Objects.requireNonNull(source.getEntity()).getVelocity().y)
.arithmetic("vel/z", source -> Objects.requireNonNull(source.getEntity()).getVelocity().z);
.string("uuid", source -> entity(source).getUuidAsString())
.arithmetic("vel/x", source -> entity(source).getVelocity().x)
.arithmetic("vel/y", source -> entity(source).getVelocity().y)
.arithmetic("vel/z", source -> entity(source).getVelocity().z)
.arithmetic("age", source -> entity(source).age)
.arithmetic("living/health", source -> living(source).getHealth())
.arithmetic("living/max_health", source -> living(source).getMaxHealth())
.arithmetic("living/stuck_arrows", source -> living(source).getStuckArrowCount())
.arithmetic("living/stingers", source -> living(source).getStingerCount())
.arithmetic("living/armor", source -> living(source).getArmor())
.arithmetic("player/xp/level", source -> player(source).experienceLevel)
.arithmetic("player/xp/total", source -> player(source).totalExperience);
}

public static Consumer<MacroBuilder> forEntity(Consumer<MacroBuilder> consumer) {
return forEntity().andThen(consumer);
}

private static LivingEntity living(ServerCommandSource source) {
return ((LivingEntity) Objects.requireNonNull(source.getEntity()));
}

private static ServerPlayerEntity player(ServerCommandSource source) {
return Objects.requireNonNull(source.getPlayer());
}

private static Entity entity(ServerCommandSource source) {
return Objects.requireNonNull(source.getEntity());
}

public MacroBuilder arithmetic(String field, ToDoubleFunction<ServerCommandSource> function) {
var old = arithmeticFunctions.put(field, function);
if (old != null || stringFunctions.containsKey(field)) throw new IllegalStateException("Tried to register field '%s' twice!".formatted(field));
Expand Down

0 comments on commit 097b9ba

Please sign in to comment.