Skip to content

Commit

Permalink
Finish actions!
Browse files Browse the repository at this point in the history
  • Loading branch information
Dueris committed Aug 2, 2024
1 parent c97ec65 commit 9af1f8d
Show file tree
Hide file tree
Showing 54 changed files with 2,238 additions and 15 deletions.
3 changes: 2 additions & 1 deletion origins/src/main/java/me/dueris/originspaper/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ public void bootstrap(@Nullable BootstrapContext bootContext) {
new Tuple<>("apoli:custom_data", "apoli:nbt"),
new Tuple<>("apoli:is_equippable", "apoli:equippable"),
new Tuple<>("apoli:fireproof", "apoli:fire_resistant"),
new Tuple<>("apoli:merge_nbt", "apoli:merge_custom_data")
new Tuple<>("apoli:merge_nbt", "apoli:merge_custom_data"),
new Tuple<>("apoli:revoke_power", "apoli:remove_power")
)
);
CalioParser.REMAPPER.set(remapper);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa
Commands.argument("origin", ResourceLocationArgument.id())
.suggests(
(context, builder) -> {
OriginLayer layer = CraftApoli.getLayerFromTag(
OriginLayer layer = CraftApoli.getLayer(
CraftNamespacedKey.fromMinecraft(ResourceLocationArgument.getId(context, "layer")).asString()
);
ORIGINS.stream()
Expand All @@ -106,7 +106,7 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa
.executes(
context -> {
Collection<ServerPlayer> targets = EntityArgument.getPlayers(context, "targets");
OriginLayer layer = CraftApoli.getLayerFromTag(
OriginLayer layer = CraftApoli.getLayer(
CraftNamespacedKey.fromMinecraft(ResourceLocationArgument.getId(context, "layer")).asString()
);
Origin origin = CraftApoli.getOrigin(
Expand Down Expand Up @@ -259,7 +259,7 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa
.executes(
context -> {
Collection<ServerPlayer> targets = EntityArgument.getPlayers(context, "targets");
OriginLayer layer = CraftApoli.getLayerFromTag(
OriginLayer layer = CraftApoli.getLayer(
CraftNamespacedKey.fromMinecraft(ResourceLocationArgument.getId(context, "layer")).asString()
);
targets.forEach(
Expand Down Expand Up @@ -318,7 +318,7 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa
)
.executes(context -> {
Collection<ServerPlayer> targets = EntityArgument.getPlayers(context, "targets");
OriginLayer layer = CraftApoli.getLayerFromTag(
OriginLayer layer = CraftApoli.getLayer(
CraftNamespacedKey.fromMinecraft(ResourceLocationArgument.getId(context, "layer")).asString()
);
RandomOriginPage randomOriginPage = new RandomOriginPage();
Expand Down Expand Up @@ -383,7 +383,7 @@ public static void register(@NotNull CommandDispatcher<CommandSourceStack> dispa
)
.executes(context -> {
Collection<ServerPlayer> targets = EntityArgument.getPlayers(context, "targets");
OriginLayer layer = CraftApoli.getLayerFromTag(
OriginLayer layer = CraftApoli.getLayer(
CraftNamespacedKey.fromMinecraft(ResourceLocationArgument.getId(context, "layer")).asString()
);
targets.forEach(player -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
context.getSource().getBukkitSender(),
power,
player.getBukkitEntity(),
CraftApoli.getLayerFromTag("apoli:command"),
CraftApoli.getLayer("apoli:command"),
context.getSource().isSilent()
);
} catch (IllegalAccessException |
Expand Down Expand Up @@ -158,7 +158,7 @@ public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
context.getSource().getBukkitSender(),
power,
player.getBukkitEntity(),
CraftApoli.getLayerFromTag(
CraftApoli.getLayer(
CraftNamespacedKey.fromMinecraft(ResourceLocationArgument.getId(context, "layer"))
.asString()
),
Expand Down Expand Up @@ -379,7 +379,7 @@ private static void addRemoveArg(LiteralArgumentBuilder<CommandSourceStack> main
context.getSource().getBukkitSender(),
OriginsPaper.getPlugin().registry.retrieve(Registries.CRAFT_POWER).get(arg),
p.getBukkitEntity(),
CraftApoli.getLayerFromTag(layer),
CraftApoli.getLayer(layer),
context.getSource().isSilent()
);
} catch (IllegalAccessException |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public static Origin getOrigin(String originTag) {
return emptyOrigin();
}

public static OriginLayer getLayerFromTag(String layerTag) {
public static OriginLayer getLayer(ResourceLocation layerTag) {
return getLayer(layerTag.toString());
}

public static OriginLayer getLayer(String layerTag) {
for (OriginLayer l : layerRegistrar.values()) {
if (l.getTag().equals(layerTag)) {
return l;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package me.dueris.originspaper.factory.action.types;

import io.github.dueris.calio.SerializableDataTypes;
import io.github.dueris.calio.parser.InstanceDefiner;
import me.dueris.originspaper.OriginsPaper;
import me.dueris.originspaper.factory.CraftApoli;
import me.dueris.originspaper.factory.action.ActionFactory;
import me.dueris.originspaper.factory.action.Actions;
import me.dueris.originspaper.factory.action.meta.*;
import me.dueris.originspaper.factory.data.ApoliDataTypes;
import me.dueris.originspaper.registry.Registries;
import me.dueris.originspaper.registry.registries.OriginLayer;
import me.dueris.originspaper.registry.registries.PowerType;
import me.dueris.originspaper.util.entity.PowerHolderComponent;
import me.dueris.originspaper.util.entity.PowerUtils;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.Entity;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class EntityActions {

Expand All @@ -25,6 +34,64 @@ public static void registerAll() {
register(NothingAction.getFactory());
register(SideAction.getFactory(ApoliDataTypes.ENTITY_ACTION, entity -> !entity.level().isClientSide));

register(new ActionFactory<>(OriginsPaper.apoliIdentifier("grant_power"),
InstanceDefiner.instanceDefiner()
.add("power", SerializableDataTypes.IDENTIFIER)
.add("source", SerializableDataTypes.IDENTIFIER),
(data, entity) -> {
if (entity.getBukkitEntity() instanceof Player p) {
PowerType powerContainer = OriginsPaper.getPlugin().registry.retrieve(Registries.CRAFT_POWER).get(data.getId("power"));
if (powerContainer == null) {
OriginsPaper.getPlugin().getLogger().severe("Searched PowerType was null when attempting to revoke a power: {}".replace("{}", data.getId("power").toString()));
return;
}
OriginLayer layer = CraftApoli.getLayer(data.getId("source"));
try {
PowerUtils.removePower(Bukkit.getConsoleSender(), powerContainer, p, layer, false);
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}));
register(new ActionFactory<>(OriginsPaper.apoliIdentifier("remove_power"),
InstanceDefiner.instanceDefiner()
.add("power", SerializableDataTypes.IDENTIFIER)
.add("source", SerializableDataTypes.IDENTIFIER),
(data, entity) -> {
if (entity.getBukkitEntity() instanceof Player p) {
PowerType powerContainer = OriginsPaper.getPlugin().registry.retrieve(Registries.CRAFT_POWER).get(data.getId("power"));
if (powerContainer == null) {
OriginsPaper.getPlugin().getLogger().severe("Searched PowerType was null when attempting to apply a new power: {}".replace("{}", data.getId("power").toString()));
return;
}
OriginLayer layer = CraftApoli.getLayer(data.getId("source"));
try {
PowerUtils.grantPower(Bukkit.getConsoleSender(), powerContainer, p, layer, false);
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}));
register(new ActionFactory<>(OriginsPaper.apoliIdentifier("revoke_all_powers"),
InstanceDefiner.instanceDefiner()
.add("source", SerializableDataTypes.IDENTIFIER),
(data, entity) -> {
if (entity instanceof Player p) {
for (ResourceLocation powerKey : PowerHolderComponent.getPowers(p).stream().map(PowerType::key).toList()) {
PowerType powerContainer = OriginsPaper.getPlugin().registry.retrieve(Registries.CRAFT_POWER).get(powerKey);
if (powerContainer == null) {
OriginsPaper.getPlugin().getLogger().severe("Searched PowerType was null when attempting to revoke a power: {}".replace("{}", powerKey.toString()));
return;
}
OriginLayer layer = CraftApoli.getLayer(data.getId("source"));
try {
PowerUtils.removePower(Bukkit.getConsoleSender(), powerContainer, p, layer, false);
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
}));
Actions.registerPackage(EntityActions::register, "me.dueris.originspaper.factory.action.types.entity");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package me.dueris.originspaper.factory.action.types.entity;

import io.github.dueris.calio.SerializableDataTypes;
import io.github.dueris.calio.parser.InstanceDefiner;
import me.dueris.originspaper.OriginsPaper;
import me.dueris.originspaper.factory.action.ActionFactory;
import me.dueris.originspaper.factory.data.ApoliDataTypes;
import me.dueris.originspaper.factory.data.types.Space;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import org.apache.logging.log4j.util.TriConsumer;
import org.jetbrains.annotations.NotNull;
import org.joml.Vector3f;

public class AddVelocityAction {

public static @NotNull ActionFactory<Entity> getFactory() {
return new ActionFactory<>(OriginsPaper.apoliIdentifier("add_velocity"),
InstanceDefiner.instanceDefiner()
.add("x", SerializableDataTypes.FLOAT, 0F)
.add("y", SerializableDataTypes.FLOAT, 0F)
.add("z", SerializableDataTypes.FLOAT, 0F)
.add("space", ApoliDataTypes.SPACE, Space.WORLD)
.add("client", SerializableDataTypes.BOOLEAN, true)
.add("server", SerializableDataTypes.BOOLEAN, true)
.add("set", SerializableDataTypes.BOOLEAN, false),
(data, entity) -> {
if (entity instanceof Player
&& (entity.level().isClientSide ?
!data.getBoolean("client") : !data.getBoolean("server")))
return;
Space space = data.get("space");
Vector3f vec = new Vector3f(data.getFloat("x"), data.getFloat("y"), data.getFloat("z"));
TriConsumer<Float, Float, Float> method = entity::push;
if (data.getBoolean("set")) {
method = entity::setDeltaMovement;
}
space.toGlobal(vec, entity);
method.accept(vec.x, vec.y, vec.z);
entity.hurtMarked = true;
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package me.dueris.originspaper.factory.action.types.entity;

import io.github.dueris.calio.SerializableDataTypes;
import io.github.dueris.calio.parser.InstanceDefiner;
import me.dueris.originspaper.OriginsPaper;
import me.dueris.originspaper.factory.action.ActionFactory;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.NotNull;

public class AddXpAction {

public static @NotNull ActionFactory<Entity> getFactory() {
return new ActionFactory<>(OriginsPaper.apoliIdentifier("add_xp"),
InstanceDefiner.instanceDefiner()
.add("points", SerializableDataTypes.INT, 0)
.add("levels", SerializableDataTypes.INT, 0),
(data, entity) -> {
if (entity instanceof Player) {
int points = data.getInt("points");
int levels = data.getInt("levels");
if (points > 0) {
((Player) entity).giveExperiencePoints(points);
}
((Player) entity).giveExperienceLevels(levels);
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.dueris.originspaper.factory.action.types.entity;

import io.github.dueris.calio.SerializableDataTypes;
import io.github.dueris.calio.parser.InstanceDefiner;
import me.dueris.originspaper.OriginsPaper;
import me.dueris.originspaper.factory.action.ActionFactory;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class ApplyEffectAction {

public static @NotNull ActionFactory<Entity> getFactory() {
return new ActionFactory<>(OriginsPaper.apoliIdentifier("apply_effect"),
InstanceDefiner.instanceDefiner()
.add("effect", SerializableDataTypes.STATUS_EFFECT_INSTANCE, null)
.add("effects", SerializableDataTypes.list(SerializableDataTypes.STATUS_EFFECT_INSTANCE), null),
(data, entity) -> {
if (entity instanceof LivingEntity le && !entity.level().isClientSide) {
if (data.isPresent("effect")) {
MobEffectInstance effect = data.get("effect");
le.addEffect(new MobEffectInstance(effect));
}
if (data.isPresent("effects")) {
((List<MobEffectInstance>) data.get("effects")).forEach(e -> le.addEffect(new MobEffectInstance(e)));
}
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package me.dueris.originspaper.factory.action.types.entity;

import io.github.dueris.calio.SerializableDataTypes;
import io.github.dueris.calio.parser.InstanceDefiner;
import io.github.dueris.calio.parser.reader.DeserializedFactoryJson;
import me.dueris.originspaper.OriginsPaper;
import me.dueris.originspaper.factory.action.ActionFactory;
import me.dueris.originspaper.factory.data.ApoliDataTypes;
import me.dueris.originspaper.factory.data.types.Shape;
import net.minecraft.util.Tuple;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.NotNull;

import java.util.function.Consumer;
import java.util.function.Predicate;

public class AreaOfEffectAction {

public static void action(@NotNull DeserializedFactoryJson data, @NotNull Entity entity) {

Consumer<Tuple<Entity, Entity>> biEntityAction = data.get("bientity_action");
Predicate<Tuple<Entity, Entity>> biEntityCondition = data.get("bientity_condition");
Shape shape = data.get("shape");

boolean includeActor = data.get("include_actor");
double radius = data.get("radius");

for (Entity target : Shape.getEntities(shape, entity.level(), entity.getPosition(1.0f), radius)) {

if (target == entity && !includeActor) {
continue;
}

Tuple<Entity, Entity> actorAndTarget = new Tuple<>(entity, target);
if (biEntityCondition == null || biEntityCondition.test(actorAndTarget)) {
biEntityAction.accept(actorAndTarget);
}

}

}

public static @NotNull ActionFactory<Entity> getFactory() {
return new ActionFactory<>(
OriginsPaper.apoliIdentifier("area_of_effect"),
InstanceDefiner.instanceDefiner()
.add("radius", SerializableDataTypes.DOUBLE, 16D)
.add("shape", SerializableDataTypes.enumValue(Shape.class), Shape.CUBE)
.add("bientity_action", ApoliDataTypes.BIENTITY_ACTION)
.add("bientity_condition", ApoliDataTypes.BIENTITY_CONDITION, null)
.add("include_target", SerializableDataTypes.BOOLEAN, false)
.add("include_actor", SerializableDataTypes.BOOLEAN, false),
AreaOfEffectAction::action
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package me.dueris.originspaper.factory.action.types.entity;

import io.github.dueris.calio.parser.InstanceDefiner;
import me.dueris.originspaper.OriginsPaper;
import me.dueris.originspaper.factory.action.ActionFactory;
import me.dueris.originspaper.factory.data.ApoliDataTypes;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import org.apache.commons.lang3.tuple.Triple;
import org.jetbrains.annotations.NotNull;

public class BlockActionAtAction {

public static @NotNull ActionFactory<Entity> getFactory() {
return new ActionFactory<>(OriginsPaper.apoliIdentifier("block_action_at"),
InstanceDefiner.instanceDefiner()
.add("block_action", ApoliDataTypes.BLOCK_ACTION),
(data, entity) -> ((ActionFactory<Triple<Level, BlockPos, Direction>>) data.get("block_action")).accept(
Triple.of(entity.level(), entity.blockPosition(), Direction.UP)));
}
}
Loading

0 comments on commit 9af1f8d

Please sign in to comment.