diff --git a/.gitignore b/.gitignore index 4a412a23f..b914f987b 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,5 @@ build/ .idea/ !/libs/*.jar -!/gradle/wrapper/*.jar \ No newline at end of file +!/gradle/wrapper/*.jar +.vscode/settings.json diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurniture.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurniture.java index 5fafae5db..c31816e18 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurniture.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurniture.java @@ -362,4 +362,4 @@ public class BukkitFurniture implements Furniture { newLocation.add(offset.x, offset.y + 0.6, -offset.z); return newLocation; } -} +} \ No newline at end of file diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBoxTypes.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBoxTypes.java index 2cb59c4b8..2b9a15d99 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBoxTypes.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/HitBoxTypes.java @@ -29,4 +29,4 @@ public class HitBoxTypes { } return factory.create(arguments); } -} +} \ No newline at end of file diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java index c61dba386..b646c323e 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventFunctions.java @@ -1,7 +1,41 @@ package net.momirealms.craftengine.core.plugin.context.event; +import java.util.ArrayList; +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext; -import net.momirealms.craftengine.core.plugin.context.function.*; +import net.momirealms.craftengine.core.plugin.context.function.ActionBarFunction; +import net.momirealms.craftengine.core.plugin.context.function.BreakBlockFunction; +import net.momirealms.craftengine.core.plugin.context.function.CancelEventFunction; +import net.momirealms.craftengine.core.plugin.context.function.CommandFunction; +import net.momirealms.craftengine.core.plugin.context.function.CommonFunctions; +import net.momirealms.craftengine.core.plugin.context.function.DropLootFunction; +import net.momirealms.craftengine.core.plugin.context.function.Function; +import net.momirealms.craftengine.core.plugin.context.function.FunctionFactory; +import net.momirealms.craftengine.core.plugin.context.function.LevelerExpFunction; +import net.momirealms.craftengine.core.plugin.context.function.MessageFunction; +import net.momirealms.craftengine.core.plugin.context.function.MythicMobsSkillFunction; +import net.momirealms.craftengine.core.plugin.context.function.OpenWindowFunction; +import net.momirealms.craftengine.core.plugin.context.function.ParticleFunction; +import net.momirealms.craftengine.core.plugin.context.function.PlaceBlockFunction; +import net.momirealms.craftengine.core.plugin.context.function.PlaySoundFunction; +import net.momirealms.craftengine.core.plugin.context.function.PotionEffectFunction; +import net.momirealms.craftengine.core.plugin.context.function.RemoveCooldownFunction; +import net.momirealms.craftengine.core.plugin.context.function.RemoveFurnitureFunction; +import net.momirealms.craftengine.core.plugin.context.function.RemovePotionEffectFunction; +import net.momirealms.craftengine.core.plugin.context.function.ReplaceFurnitureFunction; +import net.momirealms.craftengine.core.plugin.context.function.RunFunction; +import net.momirealms.craftengine.core.plugin.context.function.SetCooldownFunction; +import net.momirealms.craftengine.core.plugin.context.function.SetCountFunction; +import net.momirealms.craftengine.core.plugin.context.function.SetFoodFunction; +import net.momirealms.craftengine.core.plugin.context.function.SetSaturationFunction; +import net.momirealms.craftengine.core.plugin.context.function.SpawnFurnitureFunction; +import net.momirealms.craftengine.core.plugin.context.function.SwingHandFunction; +import net.momirealms.craftengine.core.plugin.context.function.TitleFunction; +import net.momirealms.craftengine.core.plugin.context.function.UpdateInteractionFunction; import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException; import net.momirealms.craftengine.core.registry.BuiltInRegistries; import net.momirealms.craftengine.core.registry.Registries; @@ -11,8 +45,6 @@ import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.util.ResourceConfigUtils; import net.momirealms.craftengine.core.util.ResourceKey; -import java.util.*; - public class EventFunctions { static { @@ -38,6 +70,9 @@ public class EventFunctions { register(CommonFunctions.LEVELER_EXP, new LevelerExpFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.SET_COOLDOWN, new SetCooldownFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.REMOVE_COOLDOWN, new RemoveCooldownFunction.FactoryImpl<>(EventConditions::fromMap)); + register(CommonFunctions.SPAWN_FURNITURE, new SpawnFurnitureFunction.FactoryImpl<>(EventConditions::fromMap)); + register(CommonFunctions.REMOVE_FURNITURE, new RemoveFurnitureFunction.FactoryImpl<>(EventConditions::fromMap)); + register(CommonFunctions.REPLACE_FURNITURE, new ReplaceFurnitureFunction.FactoryImpl<>(EventConditions::fromMap)); register(CommonFunctions.MYTHIC_MOBS_SKILL, new MythicMobsSkillFunction.FactoryImpl<>(EventConditions::fromMap)); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java index 055e3260a..bede6e946 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/CommonFunctions.java @@ -27,5 +27,8 @@ public final class CommonFunctions { public static final Key DROP_LOOT = Key.of("craftengine:drop_loot"); public static final Key SWING_HAND = Key.of("craftengine:swing_hand"); public static final Key LEVELER_EXP = Key.of("craftengine:leveler_exp"); + public static final Key SPAWN_FURNITURE = Key.of("craftengine:spawn_furniture"); + public static final Key REMOVE_FURNITURE = Key.of("craftengine:remove_furniture"); + public static final Key REPLACE_FURNITURE = Key.of("craftengine:replace_furniture"); public static final Key MYTHIC_MOBS_SKILL = Key.of("craftengine:mythic_mobs_skill"); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/RemoveFurnitureFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/RemoveFurnitureFunction.java new file mode 100644 index 000000000..e2caefed5 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/RemoveFurnitureFunction.java @@ -0,0 +1,60 @@ +package net.momirealms.craftengine.core.plugin.context.function; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import net.momirealms.craftengine.core.entity.furniture.Furniture; +import net.momirealms.craftengine.core.plugin.context.Condition; +import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; +import net.momirealms.craftengine.core.world.WorldPosition; + +public class RemoveFurnitureFunction extends AbstractConditionalFunction { + private final boolean dropLoot; + private final boolean playSound; + + public RemoveFurnitureFunction(boolean dropLoot, boolean playSound, List> predicates) { + super(predicates); + this.dropLoot = dropLoot; + this.playSound = playSound; + } + + @Override + public void runInternal(CTX ctx) { + Optional optionalWorldPosition = ctx.getOptionalParameter(DirectContextParameters.POSITION); + if (optionalWorldPosition.isPresent()) { + // Search for furniture in the context + Optional optionalFurniture = ctx.getOptionalParameter(DirectContextParameters.FURNITURE); + if (optionalFurniture.isPresent()) { + Furniture furniture = optionalFurniture.get(); + if (furniture.isValid()) { + furniture.destroy(); + // TODO: Implement logic to drop loot and play sounds + // using this.dropLoot and this.playSound when necessary + } + } + } + } + + @Override + public Key type() { + return CommonFunctions.REMOVE_FURNITURE; + } + + public static class FactoryImpl extends AbstractFactory { + + public FactoryImpl(java.util.function.Function, Condition> factory) { + super(factory); + } + + @Override + public Function create(Map arguments) { + boolean dropLoot = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("drop-loot", true), "drop-loot"); + boolean playSound = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("play-sound", true), "play-sound"); + return new RemoveFurnitureFunction<>(dropLoot, playSound, getPredicates(arguments)); + } + } +} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ReplaceFurnitureFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ReplaceFurnitureFunction.java new file mode 100644 index 000000000..fd671a065 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/ReplaceFurnitureFunction.java @@ -0,0 +1,108 @@ +package net.momirealms.craftengine.core.plugin.context.function; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import net.momirealms.craftengine.core.entity.furniture.AnchorType; +import net.momirealms.craftengine.core.entity.furniture.CustomFurniture; +import net.momirealms.craftengine.core.entity.furniture.Furniture; +import net.momirealms.craftengine.core.entity.furniture.FurnitureExtraData; +import net.momirealms.craftengine.core.plugin.CraftEngine; +import net.momirealms.craftengine.core.plugin.context.Condition; +import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; +import net.momirealms.craftengine.core.plugin.context.number.NumberProviders; +import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; +import net.momirealms.craftengine.core.world.WorldPosition; + +public class ReplaceFurnitureFunction extends AbstractConditionalFunction { + private final Key newFurnitureId; + private final NumberProvider x; + private final NumberProvider y; + private final NumberProvider z; + private final NumberProvider pitch; + private final NumberProvider yaw; + private final AnchorType anchorType; + private final boolean dropLoot; + private final boolean playSound; + + public ReplaceFurnitureFunction(Key newFurnitureId, NumberProvider x, NumberProvider y, NumberProvider z, + NumberProvider pitch, NumberProvider yaw, AnchorType anchorType, + boolean dropLoot, boolean playSound, List> predicates) { + super(predicates); + this.newFurnitureId = newFurnitureId; + this.x = x; + this.y = y; + this.z = z; + this.pitch = pitch; + this.yaw = yaw; + this.anchorType = anchorType; + this.dropLoot = dropLoot; + this.playSound = playSound; + } + + @Override + public void runInternal(CTX ctx) { + Optional optionalWorldPosition = ctx.getOptionalParameter(DirectContextParameters.POSITION); + Optional optionalOldFurniture = ctx.getOptionalParameter(DirectContextParameters.FURNITURE); + + if (optionalWorldPosition.isPresent() && optionalOldFurniture.isPresent()) { + Furniture oldFurniture = optionalOldFurniture.get(); + + // Get the new position or use the current furniture position + double xPos = this.x.getDouble(ctx); + double yPos = this.y.getDouble(ctx); + double zPos = this.z.getDouble(ctx); + float pitchValue = this.pitch.getFloat(ctx); + float yawValue = this.yaw.getFloat(ctx); + + WorldPosition newPosition = new WorldPosition(optionalWorldPosition.get().world(), xPos, yPos, zPos, pitchValue, yawValue); + + // Get the new furniture + Optional optionalNewFurniture = CraftEngine.instance().furnitureManager().furnitureById(this.newFurnitureId); + if (optionalNewFurniture.isPresent()) { + CustomFurniture newFurniture = optionalNewFurniture.get(); + AnchorType anchor = this.anchorType != null ? this.anchorType : newFurniture.getAnyAnchorType(); + + // Remove the old furniture + if (oldFurniture.isValid()) { + oldFurniture.destroy(); + } + + // Place the new furniture + FurnitureExtraData extraData = FurnitureExtraData.builder().anchorType(anchor).build(); + CraftEngine.instance().furnitureManager().place(newPosition, newFurniture, extraData, this.playSound); + } + } + } + + @Override + public Key type() { + return CommonFunctions.REPLACE_FURNITURE; + } + + public static class FactoryImpl extends AbstractFactory { + + public FactoryImpl(java.util.function.Function, Condition> factory) { + super(factory); + } + + @Override + public Function create(Map arguments) { + String furnitureIdStr = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("furniture-id"), "warning.config.function.replace_furniture.missing_furniture_id"); + Key furnitureId = Key.of(furnitureIdStr); + NumberProvider x = NumberProviders.fromObject(arguments.getOrDefault("x", "")); + NumberProvider y = NumberProviders.fromObject(arguments.getOrDefault("y", "")); + NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "")); + NumberProvider pitch = NumberProviders.fromObject(arguments.getOrDefault("pitch", "")); + NumberProvider yaw = NumberProviders.fromObject(arguments.getOrDefault("yaw", "")); + AnchorType anchorType = Optional.ofNullable(arguments.get("anchor-type")).map(o -> AnchorType.valueOf(o.toString().toUpperCase())).orElse(null); + boolean dropLoot = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("drop-loot", true), "drop-loot"); + boolean playSound = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("play-sound", true), "play-sound"); + return new ReplaceFurnitureFunction<>(furnitureId, x, y, z, pitch, yaw, anchorType, dropLoot, playSound, getPredicates(arguments)); + } + } +} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SpawnFurnitureFunction.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SpawnFurnitureFunction.java new file mode 100644 index 000000000..e18a1e8f4 --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/function/SpawnFurnitureFunction.java @@ -0,0 +1,93 @@ +package net.momirealms.craftengine.core.plugin.context.function; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import net.momirealms.craftengine.core.entity.furniture.AnchorType; +import net.momirealms.craftengine.core.entity.furniture.CustomFurniture; +import net.momirealms.craftengine.core.entity.furniture.FurnitureExtraData; +import net.momirealms.craftengine.core.plugin.CraftEngine; +import net.momirealms.craftengine.core.plugin.context.Condition; +import net.momirealms.craftengine.core.plugin.context.Context; +import net.momirealms.craftengine.core.plugin.context.number.NumberProvider; +import net.momirealms.craftengine.core.plugin.context.number.NumberProviders; +import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters; +import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.ResourceConfigUtils; +import net.momirealms.craftengine.core.world.World; +import net.momirealms.craftengine.core.world.WorldPosition; + +public class SpawnFurnitureFunction extends AbstractConditionalFunction { + private final Key furnitureId; + private final NumberProvider x; + private final NumberProvider y; + private final NumberProvider z; + private final NumberProvider pitch; + private final NumberProvider yaw; + private final AnchorType anchorType; + private final boolean playSound; + + public SpawnFurnitureFunction(Key furnitureId, NumberProvider x, NumberProvider y, NumberProvider z, + NumberProvider pitch, NumberProvider yaw, AnchorType anchorType, + boolean playSound, List> predicates) { + super(predicates); + this.furnitureId = furnitureId; + this.x = x; + this.y = y; + this.z = z; + this.pitch = pitch; + this.yaw = yaw; + this.anchorType = anchorType; + this.playSound = playSound; + } + + @Override + public void runInternal(CTX ctx) { + Optional optionalWorldPosition = ctx.getOptionalParameter(DirectContextParameters.POSITION); + if (optionalWorldPosition.isPresent()) { + World world = optionalWorldPosition.get().world(); + double xPos = this.x.getDouble(ctx); + double yPos = this.y.getDouble(ctx); + double zPos = this.z.getDouble(ctx); + float pitchValue = this.pitch.getFloat(ctx); + float yawValue = this.yaw.getFloat(ctx); + + WorldPosition position = new WorldPosition(world, xPos, yPos, zPos, pitchValue, yawValue); + + Optional optionalFurniture = CraftEngine.instance().furnitureManager().furnitureById(this.furnitureId); + if (optionalFurniture.isPresent()) { + CustomFurniture furniture = optionalFurniture.get(); + AnchorType anchor = this.anchorType != null ? this.anchorType : furniture.getAnyAnchorType(); + FurnitureExtraData extraData = FurnitureExtraData.builder().anchorType(anchor).build(); + CraftEngine.instance().furnitureManager().place(position, furniture, extraData, this.playSound); + } + } + } + + @Override + public Key type() { + return CommonFunctions.SPAWN_FURNITURE; + } + + public static class FactoryImpl extends AbstractFactory { + + public FactoryImpl(java.util.function.Function, Condition> factory) { + super(factory); + } + + @Override + public Function create(Map arguments) { + String furnitureIdStr = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("furniture-id"), "warning.config.function.spawn_furniture.missing_furniture_id"); + Key furnitureId = Key.of(furnitureIdStr); + NumberProvider x = NumberProviders.fromObject(arguments.getOrDefault("x", "")); + NumberProvider y = NumberProviders.fromObject(arguments.getOrDefault("y", "")); + NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "")); + NumberProvider pitch = NumberProviders.fromObject(arguments.getOrDefault("pitch", "")); + NumberProvider yaw = NumberProviders.fromObject(arguments.getOrDefault("yaw", "")); + AnchorType anchorType = Optional.ofNullable(arguments.get("anchor-type")).map(o -> AnchorType.valueOf(o.toString().toUpperCase())).orElse(null); + boolean playSound = ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("play-sound", true), "play-sound"); + return new SpawnFurnitureFunction<>(furnitureId, x, y, z, pitch, yaw, anchorType, playSound, getPredicates(arguments)); + } + } +} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/FurnitureParameterProvider.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/FurnitureParameterProvider.java index 50ccfc7a9..b3e20fab4 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/FurnitureParameterProvider.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/parameter/FurnitureParameterProvider.java @@ -1,14 +1,14 @@ package net.momirealms.craftengine.core.plugin.context.parameter; -import net.momirealms.craftengine.core.entity.furniture.Furniture; -import net.momirealms.craftengine.core.plugin.context.ChainParameterProvider; -import net.momirealms.craftengine.core.plugin.context.ContextKey; - import java.util.HashMap; import java.util.Map; import java.util.Optional; import java.util.function.Function; +import net.momirealms.craftengine.core.entity.furniture.Furniture; +import net.momirealms.craftengine.core.plugin.context.ChainParameterProvider; +import net.momirealms.craftengine.core.plugin.context.ContextKey; + public class FurnitureParameterProvider implements ChainParameterProvider { private static final Map, Function> CONTEXT_FUNCTIONS = new HashMap<>(); static { @@ -18,6 +18,8 @@ public class FurnitureParameterProvider implements ChainParameterProvider furniture.position().x()); CONTEXT_FUNCTIONS.put(DirectContextParameters.Y, furniture -> furniture.position().y()); CONTEXT_FUNCTIONS.put(DirectContextParameters.Z, furniture -> furniture.position().z()); + CONTEXT_FUNCTIONS.put(DirectContextParameters.PITCH, furniture -> furniture.position().xRot()); + CONTEXT_FUNCTIONS.put(DirectContextParameters.YAW, furniture -> furniture.position().yRot()); CONTEXT_FUNCTIONS.put(DirectContextParameters.POSITION, Furniture::position); }