From 368c3cad39fdec0b496a844aa0f9e1326e63cf4e Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Fri, 26 Dec 2025 03:29:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9D=A1=E4=BB=B6=E5=85=83?= =?UTF-8?q?=E7=B4=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../slimeworld/SlimeFormatStorageAdaptor.java | 1 - .../ArmorStandBlockEntityElementConfig.java | 10 +- .../element/AbstractFurnitureElement.java | 27 +++++ .../element/ArmorStandFurnitureElement.java | 35 +++--- .../ArmorStandFurnitureElementConfig.java | 49 +++----- .../element/ItemDisplayFurnitureElement.java | 21 ++-- .../ItemDisplayFurnitureElementConfig.java | 87 +++----------- .../element/ItemFurnitureElement.java | 31 +++-- .../element/ItemFurnitureElementConfig.java | 34 +++--- .../element/TextDisplayFurnitureElement.java | 16 ++- .../TextDisplayFurnitureElementConfig.java | 111 +++--------------- .../protocol/ClientCustomBlockPacket.java | 1 - .../element/ConditionalFurnitureElement.java | 37 ++++++ .../core/plugin/context/PlayerContext.java | 2 +- .../plugin/context/event/EventConditions.java | 7 +- .../craftengine/core/world/Glowing.java | 10 -- gradle.properties | 2 +- 17 files changed, 199 insertions(+), 282 deletions(-) create mode 100644 bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/AbstractFurnitureElement.java create mode 100644 core/src/main/java/net/momirealms/craftengine/core/entity/furniture/element/ConditionalFurnitureElement.java delete mode 100644 core/src/main/java/net/momirealms/craftengine/core/world/Glowing.java diff --git a/bukkit/compatibility/src/main/java/net/momirealms/craftengine/bukkit/compatibility/slimeworld/SlimeFormatStorageAdaptor.java b/bukkit/compatibility/src/main/java/net/momirealms/craftengine/bukkit/compatibility/slimeworld/SlimeFormatStorageAdaptor.java index 9da5e9c08..d2a978466 100644 --- a/bukkit/compatibility/src/main/java/net/momirealms/craftengine/bukkit/compatibility/slimeworld/SlimeFormatStorageAdaptor.java +++ b/bukkit/compatibility/src/main/java/net/momirealms/craftengine/bukkit/compatibility/slimeworld/SlimeFormatStorageAdaptor.java @@ -12,7 +12,6 @@ import net.momirealms.craftengine.core.world.chunk.storage.CachedStorage; import net.momirealms.craftengine.core.world.chunk.storage.DefaultStorageAdaptor; import net.momirealms.craftengine.core.world.chunk.storage.WorldDataStorage; import org.bukkit.Bukkit; -import org.bukkit.Chunk; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.jetbrains.annotations.NotNull; diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/renderer/element/ArmorStandBlockEntityElementConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/renderer/element/ArmorStandBlockEntityElementConfig.java index f0e99b90b..8cfec60cb 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/renderer/element/ArmorStandBlockEntityElementConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/block/entity/renderer/element/ArmorStandBlockEntityElementConfig.java @@ -13,10 +13,8 @@ import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.LegacyChatFormatter; import net.momirealms.craftengine.core.util.ResourceConfigUtils; import net.momirealms.craftengine.core.world.BlockPos; -import net.momirealms.craftengine.core.world.Glowing; import net.momirealms.craftengine.core.world.World; import org.bukkit.inventory.ItemStack; -import org.jetbrains.annotations.Nullable; import org.joml.Vector3f; import java.util.ArrayList; @@ -24,7 +22,7 @@ import java.util.List; import java.util.Map; import java.util.function.Function; -public class ArmorStandBlockEntityElementConfig implements BlockEntityElementConfig, Glowing { +public class ArmorStandBlockEntityElementConfig implements BlockEntityElementConfig { public static final Factory FACTORY = new Factory(); public final Function> lazyMetadataPacket; public final Key itemId; @@ -63,12 +61,6 @@ public class ArmorStandBlockEntityElementConfig implements BlockEntityElementCon }; } - @Nullable - @Override - public LegacyChatFormatter glowColor() { - return this.glowColor; - } - @Override public ArmorStandBlockEntityElement create(World world, BlockPos pos) { return new ArmorStandBlockEntityElement(this, pos); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/AbstractFurnitureElement.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/AbstractFurnitureElement.java new file mode 100644 index 000000000..150dcf1a8 --- /dev/null +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/AbstractFurnitureElement.java @@ -0,0 +1,27 @@ +package net.momirealms.craftengine.bukkit.entity.furniture.element; + +import net.momirealms.craftengine.core.entity.furniture.element.ConditionalFurnitureElement; +import net.momirealms.craftengine.core.plugin.context.PlayerContext; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Predicate; + +public abstract class AbstractFurnitureElement implements ConditionalFurnitureElement { + protected final Predicate predicate; + protected final boolean hasCondition; + + public AbstractFurnitureElement(Predicate predicate, boolean hasCondition) { + this.predicate = predicate; + this.hasCondition = hasCondition; + } + + @Override + public @NotNull Predicate viewCondition() { + return this.predicate; + } + + @Override + public boolean hasCondition() { + return this.hasCondition; + } +} diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElement.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElement.java index e25917dd5..842644c53 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElement.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElement.java @@ -8,42 +8,47 @@ import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MAttributeH import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MEntityTypes; import net.momirealms.craftengine.bukkit.world.score.BukkitTeamManager; import net.momirealms.craftengine.core.entity.furniture.Furniture; -import net.momirealms.craftengine.core.entity.furniture.FurnitureColorSource; -import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElement; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.util.VersionHelper; import net.momirealms.craftengine.core.world.Vec3d; import net.momirealms.craftengine.core.world.WorldPosition; +import org.jetbrains.annotations.NotNull; import java.util.Collections; import java.util.List; import java.util.UUID; import java.util.function.Consumer; -public class ArmorStandFurnitureElement implements FurnitureElement { +public class ArmorStandFurnitureElement extends AbstractFurnitureElement { private final ArmorStandFurnitureElementConfig config; - private final FurnitureColorSource colorSource; - public final Object cachedSpawnPacket; - public final Object cachedDespawnPacket; - public final Object cachedScalePacket; - public final Object cachedTeamPacket; - public final int entityId; - public final UUID uuid = UUID.randomUUID(); + private final Furniture furniture; + private final Object cachedSpawnPacket; + private final Object cachedDespawnPacket; + private final Object cachedScalePacket; + private final Object cachedTeamPacket; + private final int entityId; + private final UUID uuid = UUID.randomUUID(); + + @Override + public @NotNull Furniture furniture() { + return this.furniture; + } public ArmorStandFurnitureElement(Furniture furniture, ArmorStandFurnitureElementConfig config) { + super(config.predicate, config.hasCondition); this.config = config; + this.furniture = furniture; this.entityId = CoreReflections.instance$Entity$ENTITY_COUNTER.incrementAndGet(); WorldPosition furniturePos = furniture.position(); - Vec3d position = Furniture.getRelativePosition(furniturePos, config.position()); + Vec3d position = Furniture.getRelativePosition(furniturePos, config.position); this.cachedSpawnPacket = FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket( this.entityId, this.uuid, position.x, position.y, position.z, furniturePos.xRot, furniturePos.yRot, MEntityTypes.ARMOR_STAND, 0, CoreReflections.instance$Vec3$Zero, furniturePos.yRot ); - this.colorSource = furniture.dataAccessor.getColorSource(); this.cachedDespawnPacket = FastNMS.INSTANCE.constructor$ClientboundRemoveEntitiesPacket(IntList.of(this.entityId)); if (VersionHelper.isOrAbove1_20_5() && config.scale != 1) { Object attributeIns = FastNMS.INSTANCE.constructor$AttributeInstance(MAttributeHolders.SCALE, (Consumer) (o) -> {}); - FastNMS.INSTANCE.method$AttributeInstance$setBaseValue(attributeIns, config.scale()); + FastNMS.INSTANCE.method$AttributeInstance$setBaseValue(attributeIns, config.scale); this.cachedScalePacket = FastNMS.INSTANCE.constructor$ClientboundUpdateAttributesPacket(this.entityId, Collections.singletonList(attributeIns)); } else { this.cachedScalePacket = null; @@ -59,10 +64,10 @@ public class ArmorStandFurnitureElement implements FurnitureElement { } @Override - public void show(Player player) { + public void showInternal(Player player) { player.sendPackets(List.of(this.cachedSpawnPacket, FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(this.entityId, this.config.metadata.apply(player))), false); player.sendPacket(FastNMS.INSTANCE.constructor$ClientboundSetEquipmentPacket(this.entityId, List.of( - Pair.of(CoreReflections.instance$EquipmentSlot$HEAD, this.config.item(player, this.colorSource).getLiteralObject()) + Pair.of(CoreReflections.instance$EquipmentSlot$HEAD, this.config.item(player, this.furniture.dataAccessor.getColorSource()).getLiteralObject()) )), false); if (this.cachedScalePacket != null) { player.sendPacket(this.cachedScalePacket, false); diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElementConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElementConfig.java index ab2f09849..2a92ea2fa 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElementConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ArmorStandFurnitureElementConfig.java @@ -12,13 +12,15 @@ import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.item.ItemKeys; import net.momirealms.craftengine.core.item.data.FireworkExplosion; +import net.momirealms.craftengine.core.plugin.context.Condition; +import net.momirealms.craftengine.core.plugin.context.PlayerContext; +import net.momirealms.craftengine.core.plugin.context.event.EventConditions; import net.momirealms.craftengine.core.util.Key; import net.momirealms.craftengine.core.util.LegacyChatFormatter; +import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.util.ResourceConfigUtils; -import net.momirealms.craftengine.core.world.Glowing; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.joml.Vector3f; import java.util.ArrayList; @@ -26,8 +28,9 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Function; +import java.util.function.Predicate; -public class ArmorStandFurnitureElementConfig implements FurnitureElementConfig, Glowing { +public class ArmorStandFurnitureElementConfig implements FurnitureElementConfig { public static final Factory FACTORY = new Factory(); public final Function> metadata; public final Key itemId; @@ -36,19 +39,25 @@ public class ArmorStandFurnitureElementConfig implements FurnitureElementConfig< public final Vector3f position; public final boolean small; public final LegacyChatFormatter glowColor; + public final Predicate predicate; + public final boolean hasCondition; public ArmorStandFurnitureElementConfig(Key itemId, float scale, Vector3f position, boolean applyDyedColor, boolean small, - LegacyChatFormatter glowColor) { + LegacyChatFormatter glowColor, + Predicate predicate, + boolean hasCondition) { this.position = position; this.applyDyedColor = applyDyedColor; this.small = small; this.scale = scale; this.itemId = itemId; this.glowColor = glowColor; + this.predicate = predicate; + this.hasCondition = hasCondition; this.metadata = (player) -> { List dataValues = new ArrayList<>(2); if (glowColor != null) { @@ -59,7 +68,6 @@ public class ArmorStandFurnitureElementConfig implements FurnitureElementConfig< if (small) { ArmorStandData.ArmorStandFlags.addEntityData((byte) 0x01, dataValues); } - return dataValues; }; } @@ -79,32 +87,6 @@ public class ArmorStandFurnitureElementConfig implements FurnitureElementConfig< return Optional.ofNullable(wrappedItem).orElseGet(() -> BukkitItemManager.instance().createWrappedItem(ItemKeys.BARRIER, null)); } - @Nullable - @Override - public LegacyChatFormatter glowColor() { - return this.glowColor; - } - - public float scale() { - return scale; - } - - public boolean small() { - return small; - } - - public Vector3f position() { - return this.position; - } - - public boolean applyDyedColor() { - return this.applyDyedColor; - } - - public Key itemId() { - return this.itemId; - } - @Override public ArmorStandFurnitureElement create(@NotNull Furniture furniture) { return new ArmorStandFurnitureElement(furniture, this); @@ -114,13 +96,16 @@ public class ArmorStandFurnitureElementConfig implements FurnitureElementConfig< @Override public ArmorStandFurnitureElementConfig create(Map arguments) { + List> conditions = ResourceConfigUtils.parseConfigAsList(arguments.get("conditions"), EventConditions::fromMap); return new ArmorStandFurnitureElementConfig( Key.of(ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("item"), "warning.config.furniture.element.armor_stand.missing_item")), ResourceConfigUtils.getAsFloat(arguments.getOrDefault("scale", 1f), "scale"), ResourceConfigUtils.getAsVector3f(arguments.getOrDefault("position", 0f), "position"), ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("apply-dyed-color", true), "apply-dyed-color"), ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("small", false), "small"), - ResourceConfigUtils.getAsEnum(arguments.get("glow-color"), LegacyChatFormatter.class, null) + ResourceConfigUtils.getAsEnum(arguments.get("glow-color"), LegacyChatFormatter.class, null), + MiscUtils.allOf(conditions), + !conditions.isEmpty() ); } } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElement.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElement.java index b21d957e0..6aca1f728 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElement.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElement.java @@ -5,44 +5,49 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MEntityTypes; import net.momirealms.craftengine.core.entity.furniture.Furniture; -import net.momirealms.craftengine.core.entity.furniture.FurnitureColorSource; -import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElement; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.world.Vec3d; import net.momirealms.craftengine.core.world.WorldPosition; +import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.UUID; import java.util.function.Consumer; -public class ItemDisplayFurnitureElement implements FurnitureElement { +public class ItemDisplayFurnitureElement extends AbstractFurnitureElement { private final ItemDisplayFurnitureElementConfig config; + private final Furniture furniture; private final WorldPosition position; private final int entityId; private final Object despawnPacket; - private final FurnitureColorSource colorSource; private final UUID uuid = UUID.randomUUID(); public ItemDisplayFurnitureElement(Furniture furniture, ItemDisplayFurnitureElementConfig config) { + super(config.predicate, config.hasCondition); this.config = config; + this.furniture = furniture; this.entityId = CoreReflections.instance$Entity$ENTITY_COUNTER.incrementAndGet(); WorldPosition furniturePos = furniture.position(); - Vec3d position = Furniture.getRelativePosition(furniturePos, config.position()); + Vec3d position = Furniture.getRelativePosition(furniturePos, config.position); this.position = new WorldPosition(furniturePos.world, position.x, position.y, position.z, furniturePos.xRot, furniturePos.yRot); this.despawnPacket = FastNMS.INSTANCE.constructor$ClientboundRemoveEntitiesPacket(MiscUtils.init(new IntArrayList(), a -> a.add(entityId))); - this.colorSource = furniture.dataAccessor.getColorSource(); } @Override - public void show(Player player) { + public @NotNull Furniture furniture() { + return this.furniture; + } + + @Override + public void showInternal(Player player) { player.sendPacket(FastNMS.INSTANCE.constructor$ClientboundBundlePacket(List.of( FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket( this.entityId, this.uuid, this.position.x, this.position.y, this.position.z, 0, this.position.yRot, MEntityTypes.ITEM_DISPLAY, 0, CoreReflections.instance$Vec3$Zero, 0 ), - FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(this.entityId, this.config.metadata.apply(player, this.colorSource)) + FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(this.entityId, this.config.metadata.apply(player, this.furniture.dataAccessor.getColorSource())) )), false); } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElementConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElementConfig.java index 3a4c9d38e..88a3ef863 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElementConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemDisplayFurnitureElementConfig.java @@ -13,8 +13,12 @@ import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.item.ItemKeys; import net.momirealms.craftengine.core.item.data.FireworkExplosion; +import net.momirealms.craftengine.core.plugin.context.Condition; +import net.momirealms.craftengine.core.plugin.context.PlayerContext; +import net.momirealms.craftengine.core.plugin.context.event.EventConditions; import net.momirealms.craftengine.core.util.Color; import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.util.ResourceConfigUtils; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -27,6 +31,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.BiFunction; +import java.util.function.Predicate; public class ItemDisplayFurnitureElementConfig implements FurnitureElementConfig { public static final Factory FACTORY = new Factory(); @@ -47,6 +52,8 @@ public class ItemDisplayFurnitureElementConfig implements FurnitureElementConfig public final int blockLight; public final int skyLight; public final float viewRange; + public final Predicate predicate; + public final boolean hasCondition; public ItemDisplayFurnitureElementConfig(Key itemId, Vector3f scale, @@ -63,7 +70,9 @@ public class ItemDisplayFurnitureElementConfig implements FurnitureElementConfig @Nullable Color glowColor, int blockLight, int skyLight, - float viewRange) { + float viewRange, + Predicate predicate, + boolean hasCondition) { this.scale = scale; this.position = position; this.translation = translation; @@ -80,6 +89,8 @@ public class ItemDisplayFurnitureElementConfig implements FurnitureElementConfig this.blockLight = blockLight; this.skyLight = skyLight; this.viewRange = viewRange; + this.predicate = predicate; + this.hasCondition = hasCondition; BiFunction> itemFunction = (player, colorSource) -> { Item wrappedItem = BukkitItemManager.instance().createWrappedItem(itemId, player); if (applyDyedColor && colorSource != null && wrappedItem != null) { @@ -116,75 +127,6 @@ public class ItemDisplayFurnitureElementConfig implements FurnitureElementConfig }; } - public Vector3f scale() { - return this.scale; - } - - public Vector3f position() { - return this.position; - } - - public Vector3f translation() { - return this.translation; - } - - public float xRot() { - return this.xRot; - } - - public float yRot() { - return this.yRot; - } - - public Quaternionf rotation() { - return this.rotation; - } - - public ItemDisplayContext displayContext() { - return this.displayContext; - } - - public Billboard billboard() { - return this.billboard; - } - - public float shadowRadius() { - return this.shadowRadius; - } - - public float shadowStrength() { - return this.shadowStrength; - } - - public boolean applyDyedColor() { - return this.applyDyedColor; - } - - public BiFunction> metadata() { - return this.metadata; - } - - public Key itemId() { - return this.itemId; - } - - @Nullable - public Color glowColor() { - return this.glowColor; - } - - public int blockLight() { - return this.blockLight; - } - - public int skyLight() { - return this.skyLight; - } - - public float viewRange() { - return this.viewRange; - } - @Override public ItemDisplayFurnitureElement create(@NotNull Furniture furniture) { return new ItemDisplayFurnitureElement(furniture, this); @@ -195,6 +137,7 @@ public class ItemDisplayFurnitureElementConfig implements FurnitureElementConfig @Override public ItemDisplayFurnitureElementConfig create(Map arguments) { Map brightness = ResourceConfigUtils.getAsMap(arguments.getOrDefault("brightness", Map.of()), "brightness"); + List> conditions = ResourceConfigUtils.parseConfigAsList(arguments.get("conditions"), EventConditions::fromMap); return new ItemDisplayFurnitureElementConfig( Key.of(ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("item"), "warning.config.furniture.element.item_display.missing_item")), ResourceConfigUtils.getAsVector3f(arguments.getOrDefault("scale", 1f), "scale"), @@ -211,7 +154,9 @@ public class ItemDisplayFurnitureElementConfig implements FurnitureElementConfig Optional.ofNullable(arguments.get("glow-color")).map(it -> Color.fromStrings(it.toString().split(","))).orElse(null), ResourceConfigUtils.getAsInt(brightness.getOrDefault("block-light", -1), "block-light"), ResourceConfigUtils.getAsInt(brightness.getOrDefault("sky-light", -1), "sky-light"), - ResourceConfigUtils.getAsFloat(arguments.getOrDefault("view-range", 1f), "view-range") + ResourceConfigUtils.getAsFloat(arguments.getOrDefault("view-range", 1f), "view-range"), + MiscUtils.allOf(conditions), + !conditions.isEmpty() ); } } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElement.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElement.java index 26621dd33..2937905ec 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElement.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElement.java @@ -5,33 +5,34 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MEntityTypes; import net.momirealms.craftengine.core.entity.furniture.Furniture; -import net.momirealms.craftengine.core.entity.furniture.FurnitureColorSource; -import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElement; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.world.Vec3d; import net.momirealms.craftengine.core.world.WorldPosition; +import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.UUID; import java.util.function.Consumer; -public class ItemFurnitureElement implements FurnitureElement { +public class ItemFurnitureElement extends AbstractFurnitureElement { private final ItemFurnitureElementConfig config; - public final int entityId1; - public final int entityId2; + private final Furniture furniture; + private final int entityId1; + private final int entityId2; private final Object despawnPacket; - private final FurnitureColorSource colorSource; - public final Object cachedSpawnPacket1; - public final Object cachedSpawnPacket2; - public final Object cachedRidePacket; + private final Object cachedSpawnPacket1; + private final Object cachedSpawnPacket2; + private final Object cachedRidePacket; public ItemFurnitureElement(Furniture furniture, ItemFurnitureElementConfig config) { + super(config.predicate, config.hasCondition); + this.furniture = furniture; this.config = config; this.entityId1 = CoreReflections.instance$Entity$ENTITY_COUNTER.incrementAndGet(); this.entityId2 = CoreReflections.instance$Entity$ENTITY_COUNTER.incrementAndGet(); WorldPosition furniturePos = furniture.position(); - Vec3d position = Furniture.getRelativePosition(furniturePos, config.position()); + Vec3d position = Furniture.getRelativePosition(furniturePos, config.position); this.cachedSpawnPacket1 = FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket( entityId1, UUID.randomUUID(), position.x, position.y, position.z, 0, 0, MEntityTypes.ITEM_DISPLAY, 0, CoreReflections.instance$Vec3$Zero, 0 @@ -47,16 +48,20 @@ public class ItemFurnitureElement implements FurnitureElement { a.add(entityId2); } )); - this.colorSource = furniture.dataAccessor.getColorSource(); } @Override - public void show(Player player) { + public @NotNull Furniture furniture() { + return this.furniture; + } + + @Override + public void showInternal(Player player) { player.sendPackets(List.of( this.cachedSpawnPacket1, this.cachedSpawnPacket2, this.cachedRidePacket, - FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(this.entityId2, this.config.metadata().apply(player, this.colorSource) + FastNMS.INSTANCE.constructor$ClientboundSetEntityDataPacket(this.entityId2, this.config.metadata.apply(player, this.furniture.dataAccessor.getColorSource()) )), false); } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElementConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElementConfig.java index af3808b69..28e1a0634 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElementConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/ItemFurnitureElementConfig.java @@ -11,7 +11,11 @@ import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.item.Item; import net.momirealms.craftengine.core.item.ItemKeys; import net.momirealms.craftengine.core.item.data.FireworkExplosion; +import net.momirealms.craftengine.core.plugin.context.Condition; +import net.momirealms.craftengine.core.plugin.context.PlayerContext; +import net.momirealms.craftengine.core.plugin.context.event.EventConditions; import net.momirealms.craftengine.core.util.Key; +import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.util.ResourceConfigUtils; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -22,6 +26,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.BiFunction; +import java.util.function.Predicate; public class ItemFurnitureElementConfig implements FurnitureElementConfig { public static final Factory FACTORY = new Factory(); @@ -29,13 +34,19 @@ public class ItemFurnitureElementConfig implements FurnitureElementConfig predicate; + public final boolean hasCondition; public ItemFurnitureElementConfig(Key itemId, Vector3f position, - boolean applyDyedColor) { + boolean applyDyedColor, + Predicate predicate, + boolean hasCondition) { this.position = position; this.applyDyedColor = applyDyedColor; this.itemId = itemId; + this.hasCondition = hasCondition; + this.predicate = predicate; BiFunction> itemFunction = (player, colorSource) -> { Item wrappedItem = BukkitItemManager.instance().createWrappedItem(itemId, player); if (applyDyedColor && colorSource != null && wrappedItem != null) { @@ -58,22 +69,6 @@ public class ItemFurnitureElementConfig implements FurnitureElementConfig> metadata() { - return this.metadata; - } - - public Key itemId() { - return this.itemId; - } - @Override public ItemFurnitureElement create(@NotNull Furniture furniture) { return new ItemFurnitureElement(furniture, this); @@ -83,10 +78,13 @@ public class ItemFurnitureElementConfig implements FurnitureElementConfig arguments) { + List> conditions = ResourceConfigUtils.parseConfigAsList(arguments.get("conditions"), EventConditions::fromMap); return new ItemFurnitureElementConfig( Key.of(ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("item"), "warning.config.furniture.element.item.missing_item")), ResourceConfigUtils.getAsVector3f(arguments.getOrDefault("position", 0f), "position"), - ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("apply-dyed-color", true), "apply-dyed-color") + ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("apply-dyed-color", true), "apply-dyed-color"), + MiscUtils.allOf(conditions), + !conditions.isEmpty() ); } } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElement.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElement.java index a68e1c483..d142e9b32 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElement.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElement.java @@ -5,34 +5,42 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections; import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MEntityTypes; import net.momirealms.craftengine.core.entity.furniture.Furniture; -import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElement; import net.momirealms.craftengine.core.entity.player.Player; import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.world.Vec3d; import net.momirealms.craftengine.core.world.WorldPosition; +import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.UUID; import java.util.function.Consumer; -public class TextDisplayFurnitureElement implements FurnitureElement { +public class TextDisplayFurnitureElement extends AbstractFurnitureElement { private final TextDisplayFurnitureElementConfig config; + private final Furniture furniture; private final WorldPosition position; private final int entityId; private final Object despawnPacket; private final UUID uuid = UUID.randomUUID(); public TextDisplayFurnitureElement(Furniture furniture, TextDisplayFurnitureElementConfig config) { + super(config.predicate, config.hasCondition); + this.furniture = furniture; this.config = config; this.entityId = CoreReflections.instance$Entity$ENTITY_COUNTER.incrementAndGet(); WorldPosition furniturePos = furniture.position(); - Vec3d position = Furniture.getRelativePosition(furniturePos, config.position()); + Vec3d position = Furniture.getRelativePosition(furniturePos, config.position); this.position = new WorldPosition(furniturePos.world, position.x, position.y, position.z, furniturePos.xRot, furniturePos.yRot); this.despawnPacket = FastNMS.INSTANCE.constructor$ClientboundRemoveEntitiesPacket(MiscUtils.init(new IntArrayList(), a -> a.add(entityId))); } @Override - public void show(Player player) { + public @NotNull Furniture furniture() { + return this.furniture; + } + + @Override + public void showInternal(Player player) { player.sendPacket(FastNMS.INSTANCE.constructor$ClientboundBundlePacket(List.of( FastNMS.INSTANCE.constructor$ClientboundAddEntityPacket( this.entityId, this.uuid, diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElementConfig.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElementConfig.java index 614efbad3..d359b3548 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElementConfig.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/element/TextDisplayFurnitureElementConfig.java @@ -9,9 +9,13 @@ import net.momirealms.craftengine.core.entity.furniture.Furniture; import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElementConfig; import net.momirealms.craftengine.core.entity.furniture.element.FurnitureElementConfigFactory; import net.momirealms.craftengine.core.entity.player.Player; +import net.momirealms.craftengine.core.plugin.context.Condition; import net.momirealms.craftengine.core.plugin.context.NetworkTextReplaceContext; +import net.momirealms.craftengine.core.plugin.context.PlayerContext; +import net.momirealms.craftengine.core.plugin.context.event.EventConditions; import net.momirealms.craftengine.core.util.AdventureHelper; import net.momirealms.craftengine.core.util.Color; +import net.momirealms.craftengine.core.util.MiscUtils; import net.momirealms.craftengine.core.util.ResourceConfigUtils; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -23,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.function.Function; +import java.util.function.Predicate; public class TextDisplayFurnitureElementConfig implements FurnitureElementConfig { public static final Factory FACTORY = new Factory(); @@ -49,6 +54,8 @@ public class TextDisplayFurnitureElementConfig implements FurnitureElementConfig public final boolean isSeeThrough; public final boolean useDefaultBackgroundColor; public final TextDisplayAlignment alignment; + public final Predicate predicate; + public final boolean hasCondition; public TextDisplayFurnitureElementConfig(String text, Vector3f scale, @@ -71,7 +78,9 @@ public class TextDisplayFurnitureElementConfig implements FurnitureElementConfig boolean hasShadow, boolean isSeeThrough, boolean useDefaultBackgroundColor, - TextDisplayAlignment alignment) { + TextDisplayAlignment alignment, + Predicate predicate, + boolean hasCondition) { this.text = text; this.scale = scale; this.position = position; @@ -94,6 +103,8 @@ public class TextDisplayFurnitureElementConfig implements FurnitureElementConfig this.useDefaultBackgroundColor = useDefaultBackgroundColor; this.alignment = alignment; this.isSeeThrough = isSeeThrough; + this.hasCondition = hasCondition; + this.predicate = predicate; this.metadata = (player) -> { List dataValues = new ArrayList<>(); if (glowColor != null) { @@ -119,99 +130,6 @@ public class TextDisplayFurnitureElementConfig implements FurnitureElementConfig }; } - public Vector3f scale() { - return this.scale; - } - - public Vector3f position() { - return this.position; - } - - public Vector3f translation() { - return this.translation; - } - - public float xRot() { - return this.xRot; - } - - public float yRot() { - return this.yRot; - } - - public Quaternionf rotation() { - return this.rotation; - } - - public ItemDisplayContext displayContext() { - return this.displayContext; - } - - public Billboard billboard() { - return this.billboard; - } - - public float shadowRadius() { - return this.shadowRadius; - } - - public float shadowStrength() { - return this.shadowStrength; - } - - public Function> metadata() { - return this.metadata; - } - - @Nullable - public Color glowColor() { - return this.glowColor; - } - - public int blockLight() { - return this.blockLight; - } - - public int skyLight() { - return this.skyLight; - } - - public float viewRange() { - return this.viewRange; - } - - public String text() { - return this.text; - } - - public int lineWidth() { - return this.lineWidth; - } - - public int backgroundColor() { - return this.backgroundColor; - } - - public byte opacity() { - return this.opacity; - } - - public boolean hasShadow() { - return this.hasShadow; - } - - public boolean isSeeThrough() { - return this.isSeeThrough; - } - - public boolean useDefaultBackgroundColor() { - return this.useDefaultBackgroundColor; - } - - public TextDisplayAlignment alignment() { - return this.alignment; - } - @Override public TextDisplayFurnitureElement create(@NotNull Furniture furniture) { return new TextDisplayFurnitureElement(furniture, this); @@ -222,6 +140,7 @@ public class TextDisplayFurnitureElementConfig implements FurnitureElementConfig @Override public TextDisplayFurnitureElementConfig create(Map arguments) { Map brightness = ResourceConfigUtils.getAsMap(arguments.getOrDefault("brightness", Map.of()), "brightness"); + List> conditions = ResourceConfigUtils.parseConfigAsList(arguments.get("conditions"), EventConditions::fromMap); return new TextDisplayFurnitureElementConfig( ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("text"), "warning.config.furniture.element.text_display.missing_text"), ResourceConfigUtils.getAsVector3f(arguments.getOrDefault("scale", 1f), "scale"), @@ -244,7 +163,9 @@ public class TextDisplayFurnitureElementConfig implements FurnitureElementConfig ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("has-shadow", false), "has-shadow"), ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("is-see-through", false), "is-see-through"), ResourceConfigUtils.getAsBoolean(arguments.getOrDefault("use-default-background-color", false), "use-default-background-color"), - ResourceConfigUtils.getAsEnum(arguments.get("alignment"), TextDisplayAlignment.class, TextDisplayAlignment.CENTER) + ResourceConfigUtils.getAsEnum(arguments.get("alignment"), TextDisplayAlignment.class, TextDisplayAlignment.CENTER), + MiscUtils.allOf(conditions), + !conditions.isEmpty() ); } } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/payload/protocol/ClientCustomBlockPacket.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/payload/protocol/ClientCustomBlockPacket.java index f1934d6de..a26e61379 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/payload/protocol/ClientCustomBlockPacket.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/payload/protocol/ClientCustomBlockPacket.java @@ -13,7 +13,6 @@ import net.momirealms.craftengine.bukkit.plugin.reflection.paper.PaperReflection import net.momirealms.craftengine.bukkit.util.BlockStateUtils; import net.momirealms.craftengine.bukkit.util.RegistryUtils; import net.momirealms.craftengine.core.plugin.CraftEngine; -import net.momirealms.craftengine.core.plugin.logger.Debugger; import net.momirealms.craftengine.core.plugin.network.ModPacket; import net.momirealms.craftengine.core.plugin.network.NetWorkUser; import net.momirealms.craftengine.core.plugin.network.codec.NetworkCodec; diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/element/ConditionalFurnitureElement.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/element/ConditionalFurnitureElement.java new file mode 100644 index 000000000..89310f21e --- /dev/null +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/element/ConditionalFurnitureElement.java @@ -0,0 +1,37 @@ +package net.momirealms.craftengine.core.entity.furniture.element; + +import net.momirealms.craftengine.core.entity.furniture.Furniture; +import net.momirealms.craftengine.core.entity.player.Player; +import net.momirealms.craftengine.core.plugin.context.ContextHolder; +import net.momirealms.craftengine.core.plugin.context.PlayerContext; +import net.momirealms.craftengine.core.plugin.context.PlayerOptionalContext; +import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters; +import org.jetbrains.annotations.NotNull; + +import java.util.function.Predicate; + +public interface ConditionalFurnitureElement extends FurnitureElement { + + @NotNull + Predicate viewCondition(); + + @NotNull + Furniture furniture(); + + boolean hasCondition(); + + @Override + default void show(Player player) { + if (hasCondition()) { + PlayerOptionalContext context = PlayerOptionalContext.of(player, ContextHolder.builder() + .withParameter(DirectContextParameters.FURNITURE, furniture())); + if (viewCondition().test(context)) { + showInternal(player); + } + } else { + showInternal(player); + } + } + + void showInternal(Player player); +} diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerContext.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerContext.java index 89bc0f1fc..ed3d51162 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerContext.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/PlayerContext.java @@ -2,7 +2,7 @@ package net.momirealms.craftengine.core.plugin.context; import net.momirealms.craftengine.core.entity.player.Player; -public interface PlayerContext { +public interface PlayerContext extends Context { Player player(); } diff --git a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java index 6c9466d0a..998ced962 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java +++ b/core/src/main/java/net/momirealms/craftengine/core/plugin/context/event/EventConditions.java @@ -49,7 +49,8 @@ public class EventConditions { .register(ResourceKey.create(Registries.EVENT_CONDITION_FACTORY.location(), key), factory); } - public static Condition fromMap(Map map) { + @SuppressWarnings("unchecked") + public static Condition fromMap(Map map) { String type = ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("type"), "warning.config.event.condition.missing_type"); Key key = Key.withDefaultNamespace(type, Key.DEFAULT_NAMESPACE); if (key.value().charAt(0) == '!') { @@ -57,13 +58,13 @@ public class EventConditions { if (factory == null) { throw new LocalizedResourceConfigException("warning.config.event.condition.invalid_type", type); } - return new InvertedCondition<>(factory.create(map)); + return new InvertedCondition<>((Condition) factory.create(map)); } else { ConditionFactory factory = BuiltInRegistries.EVENT_CONDITION_FACTORY.getValue(key); if (factory == null) { throw new LocalizedResourceConfigException("warning.config.event.condition.invalid_type", type); } - return factory.create(map); + return (Condition) factory.create(map); } } } diff --git a/core/src/main/java/net/momirealms/craftengine/core/world/Glowing.java b/core/src/main/java/net/momirealms/craftengine/core/world/Glowing.java deleted file mode 100644 index b237e34b5..000000000 --- a/core/src/main/java/net/momirealms/craftengine/core/world/Glowing.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.momirealms.craftengine.core.world; - -import net.momirealms.craftengine.core.util.LegacyChatFormatter; -import org.jetbrains.annotations.Nullable; - -public interface Glowing { - - @Nullable - LegacyChatFormatter glowColor(); -} diff --git a/gradle.properties b/gradle.properties index 1bf118c1d..e1c0bf3ed 100644 --- a/gradle.properties +++ b/gradle.properties @@ -37,7 +37,7 @@ geantyref_version=1.3.16 zstd_version=1.5.7-6 commons_io_version=2.21.0 commons_lang3_version=3.20.0 -sparrow_nbt_version=0.10.9 +sparrow_nbt_version=0.11 sparrow_util_version=0.79 fastutil_version=8.5.18 netty_version=4.1.128.Final