9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-27 02:49:15 +00:00

添加条件元素

This commit is contained in:
XiaoMoMi
2025-12-26 03:29:14 +08:00
parent 1122291e85
commit 368c3cad39
17 changed files with 199 additions and 282 deletions

View File

@@ -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;

View File

@@ -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<ArmorStandBlockEntityElement>, Glowing {
public class ArmorStandBlockEntityElementConfig implements BlockEntityElementConfig<ArmorStandBlockEntityElement> {
public static final Factory FACTORY = new Factory();
public final Function<Player, List<Object>> 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);

View File

@@ -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<PlayerContext> predicate;
protected final boolean hasCondition;
public AbstractFurnitureElement(Predicate<PlayerContext> predicate, boolean hasCondition) {
this.predicate = predicate;
this.hasCondition = hasCondition;
}
@Override
public @NotNull Predicate<PlayerContext> viewCondition() {
return this.predicate;
}
@Override
public boolean hasCondition() {
return this.hasCondition;
}
}

View File

@@ -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);

View File

@@ -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<ArmorStandFurnitureElement>, Glowing {
public class ArmorStandFurnitureElementConfig implements FurnitureElementConfig<ArmorStandFurnitureElement> {
public static final Factory FACTORY = new Factory();
public final Function<Player, List<Object>> 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<PlayerContext> predicate;
public final boolean hasCondition;
public ArmorStandFurnitureElementConfig(Key itemId,
float scale,
Vector3f position,
boolean applyDyedColor,
boolean small,
LegacyChatFormatter glowColor) {
LegacyChatFormatter glowColor,
Predicate<PlayerContext> 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<Object> 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<String, Object> arguments) {
List<Condition<PlayerContext>> 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()
);
}
}

View File

@@ -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);
}

View File

@@ -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<ItemDisplayFurnitureElement> {
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<PlayerContext> 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<PlayerContext> 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<Player, FurnitureColorSource, Item<?>> itemFunction = (player, colorSource) -> {
Item<ItemStack> 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<Player, FurnitureColorSource, List<Object>> 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<String, Object> arguments) {
Map<String, Object> brightness = ResourceConfigUtils.getAsMap(arguments.getOrDefault("brightness", Map.of()), "brightness");
List<Condition<PlayerContext>> 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()
);
}
}

View File

@@ -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);
}

View File

@@ -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<ItemFurnitureElement> {
public static final Factory FACTORY = new Factory();
@@ -29,13 +34,19 @@ public class ItemFurnitureElementConfig implements FurnitureElementConfig<ItemFu
public final Key itemId;
public final boolean applyDyedColor;
public final Vector3f position;
public final Predicate<PlayerContext> predicate;
public final boolean hasCondition;
public ItemFurnitureElementConfig(Key itemId,
Vector3f position,
boolean applyDyedColor) {
boolean applyDyedColor,
Predicate<PlayerContext> predicate,
boolean hasCondition) {
this.position = position;
this.applyDyedColor = applyDyedColor;
this.itemId = itemId;
this.hasCondition = hasCondition;
this.predicate = predicate;
BiFunction<Player, FurnitureColorSource, Item<?>> itemFunction = (player, colorSource) -> {
Item<ItemStack> wrappedItem = BukkitItemManager.instance().createWrappedItem(itemId, player);
if (applyDyedColor && colorSource != null && wrappedItem != null) {
@@ -58,22 +69,6 @@ public class ItemFurnitureElementConfig implements FurnitureElementConfig<ItemFu
};
}
public Vector3f position() {
return this.position;
}
public boolean applyDyedColor() {
return this.applyDyedColor;
}
public BiFunction<Player, FurnitureColorSource, List<Object>> 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<ItemFu
@Override
public ItemFurnitureElementConfig create(Map<String, Object> arguments) {
List<Condition<PlayerContext>> 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()
);
}
}

View File

@@ -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,

View File

@@ -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<TextDisplayFurnitureElement> {
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<PlayerContext> 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<PlayerContext> 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<Object> 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<Player, List<Object>> 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<String, Object> arguments) {
Map<String, Object> brightness = ResourceConfigUtils.getAsMap(arguments.getOrDefault("brightness", Map.of()), "brightness");
List<Condition<PlayerContext>> 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()
);
}
}

View File

@@ -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;

View File

@@ -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<PlayerContext> 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);
}

View File

@@ -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();
}

View File

@@ -49,7 +49,8 @@ public class EventConditions {
.register(ResourceKey.create(Registries.EVENT_CONDITION_FACTORY.location(), key), factory);
}
public static Condition<Context> fromMap(Map<String, Object> map) {
@SuppressWarnings("unchecked")
public static <CTX extends Context> Condition<CTX> fromMap(Map<String, Object> 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<CTX>) factory.create(map));
} else {
ConditionFactory<Context> 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<CTX>) factory.create(map);
}
}
}

View File

@@ -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();
}

View File

@@ -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