9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-31 21:06:31 +00:00

重构context again

This commit is contained in:
XiaoMoMi
2025-05-06 02:34:03 +08:00
parent dd3e1276ec
commit 79c441c0e0
98 changed files with 1022 additions and 216 deletions

View File

@@ -1,13 +1,20 @@
package net.momirealms.craftengine.bukkit.util;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Player;
import java.util.Objects;
import java.util.Optional;
public class LegacyAttributeUtils {
public static void setMaxHealth(ArmorStand entity) {
Objects.requireNonNull(entity.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(0.01);
}
public static double getLuck(Player player) {
return Optional.ofNullable(player.getAttribute(Attribute.GENERIC_LUCK)).map(AttributeInstance::getValue).orElse(1d);
}
}

View File

@@ -291,4 +291,5 @@ warning.config.conflict_matcher.inverted.missing_term: "<yellow>Issue found in c
warning.config.conflict_matcher.all_of.missing_terms: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'terms' argument for 'all_of' matcher.</yellow>"
warning.config.conflict_matcher.any_of.missing_terms: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'terms' argument for 'any_of' matcher.</yellow>"
warning.config.conflict_resolution.missing_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'type' argument for one of the resolutions.</yellow>"
warning.config.conflict_resolution.invalid_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - One of the resolutions is using the invalid type '<arg:0>'.</yellow>"
warning.config.conflict_resolution.invalid_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - One of the resolutions is using the invalid type '<arg:0>'.</yellow>"
warning.config.function.command.missing_command: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'command' argument for 'command' function.</yellow>"

View File

@@ -10,7 +10,6 @@ import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.UpdateOption;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
import net.momirealms.craftengine.core.plugin.context.parameter.CommonParameters;
@@ -176,9 +175,9 @@ public final class CraftEngineBlocks {
BukkitServerPlayer serverPlayer = BukkitCraftEngine.instance().adapt(player);
if (player != null) {
builder.withParameter(CommonParameters.PLAYER, serverPlayer);
builder.withOptionalParameter(CommonParameters.TOOL, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND));
//mark item builder.withOptionalParameter(CommonParameters.MAIN_HAND_ITEM, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND));
}
for (Item<?> item : state.getDrops(builder, world)) {
for (Item<?> item : state.getDrops(builder, world, serverPlayer)) {
world.dropItemNaturally(vec3d, item);
}
}

View File

@@ -9,7 +9,6 @@ import net.momirealms.craftengine.bukkit.util.LocationUtils;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.entity.furniture.AnchorType;
import net.momirealms.craftengine.core.entity.furniture.CustomFurniture;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.loot.LootTable;
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
@@ -275,9 +274,9 @@ public final class CraftEngineFurniture {
builder.withParameter(CommonParameters.WORLD, world);
if (player != null) {
builder.withParameter(CommonParameters.PLAYER, player);
builder.withOptionalParameter(CommonParameters.TOOL, player.getItemInHand(InteractionHand.MAIN_HAND));
//mark item builder.withOptionalParameter(CommonParameters.MAIN_HAND_ITEM, player.getItemInHand(InteractionHand.MAIN_HAND));
}
List<Item<ItemStack>> items = lootTable.getRandomItems(builder.build(), world);
List<Item<ItemStack>> items = lootTable.getRandomItems(builder.build(), world, player);
for (Item<ItemStack> item : items) {
world.dropItemNaturally(vec3d, item);
}

View File

@@ -153,9 +153,9 @@ public class BlockEventListener implements Listener {
ContextHolder.Builder builder = ContextHolder.builder()
.withParameter(CommonParameters.WORLD, world)
.withParameter(CommonParameters.LOCATION, vec3d)
.withParameter(CommonParameters.PLAYER, serverPlayer)
.withOptionalParameter(CommonParameters.TOOL, itemInHand);
for (Item<Object> item : state.getDrops(builder, world)) {
.withParameter(CommonParameters.PLAYER, serverPlayer);
//mark item .withOptionalParameter(CommonParameters.MAIN_HAND_ITEM, itemInHand);
for (Item<Object> item : state.getDrops(builder, world, serverPlayer)) {
world.dropItemNaturally(vec3d, item);
}
}
@@ -174,11 +174,11 @@ public class BlockEventListener implements Listener {
ContextHolder.Builder builder = ContextHolder.builder()
.withParameter(CommonParameters.WORLD, world)
.withParameter(CommonParameters.LOCATION, vec3d)
.withParameter(CommonParameters.PLAYER, serverPlayer)
.withOptionalParameter(CommonParameters.TOOL, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND));
.withParameter(CommonParameters.PLAYER, serverPlayer);
//mark item .withOptionalParameter(CommonParameters.MAIN_HAND_ITEM, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND));
ContextHolder contextHolder = builder.build();
for (LootTable<?> lootTable : it.lootTables()) {
for (Item<?> item : lootTable.getRandomItems(contextHolder, world)) {
for (Item<?> item : lootTable.getRandomItems(contextHolder, world, serverPlayer)) {
world.dropItemNaturally(vec3d, item);
}
}
@@ -216,7 +216,7 @@ public class BlockEventListener implements Listener {
ContextHolder.Builder builder = ContextHolder.builder()
.withParameter(CommonParameters.WORLD, world)
.withParameter(CommonParameters.LOCATION, vec3d);
for (Item<?> item : immutableBlockState.getDrops(builder, world)) {
for (Item<?> item : immutableBlockState.getDrops(builder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
}
@@ -236,7 +236,7 @@ public class BlockEventListener implements Listener {
builder.withParameter(CommonParameters.LOCATION, vec3d);
ContextHolder contextHolder = builder.build();
for (LootTable<?> lootTable : it.lootTables()) {
for (Item<?> item : lootTable.getRandomItems(contextHolder, world)) {
for (Item<?> item : lootTable.getRandomItems(contextHolder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
}
@@ -334,7 +334,7 @@ public class BlockEventListener implements Listener {
if (yield < 1f) {
builder.withParameter(CommonParameters.EXPLOSION_RADIUS, 1.0f / yield);
}
for (Item<Object> item : state.getDrops(builder, world)) {
for (Item<Object> item : state.getDrops(builder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
world.playBlockSound(vec3d, state.sounds().breakSound());

View File

@@ -40,7 +40,7 @@ public class FallingBlockRemoveListener implements Listener {
net.momirealms.craftengine.core.world.World world = new BukkitWorld(fallingBlock.getWorld());
builder.withParameter(CommonParameters.LOCATION, vec3d);
builder.withParameter(CommonParameters.WORLD, world);
for (Item<Object> item : immutableBlockState.getDrops(builder, world)) {
for (Item<Object> item : immutableBlockState.getDrops(builder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
Object entityData = Reflections.field$Entity$entityData.get(fallingBlockEntity);

View File

@@ -73,7 +73,7 @@ public class BushBlockBehavior extends BukkitBlockBehavior {
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
builder.withParameter(CommonParameters.LOCATION, vec3d);
builder.withParameter(CommonParameters.WORLD, world);
for (Item<Object> item : previousState.getDrops(builder, world)) {
for (Item<Object> item : previousState.getDrops(builder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
world.playBlockSound(vec3d, previousState.sounds().breakSound());

View File

@@ -12,8 +12,8 @@ import net.momirealms.craftengine.core.block.UpdateOption;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
import net.momirealms.craftengine.core.block.properties.IntegerProperty;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.loot.LootContext;
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
import net.momirealms.craftengine.core.plugin.context.SimpleContext;
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.CommonParameters;
@@ -145,12 +145,14 @@ public class CropBlockBehavior extends BushBlockBehavior {
int x = FastNMS.INSTANCE.field$Vec3i$x(pos);
int y = FastNMS.INSTANCE.field$Vec3i$y(pos);
int z = FastNMS.INSTANCE.field$Vec3i$z(pos);
net.momirealms.craftengine.core.world.World wrappedWorld = new BukkitWorld(world);
int i = this.getAge(immutableBlockState) + this.boneMealBonus.getInt(new LootContext(wrappedWorld, 1, ContextHolder.builder()
.withParameter(CommonParameters.WORLD, wrappedWorld)
.withParameter(CommonParameters.LOCATION, Vec3d.atCenterOf(new Vec3i(x, y, z)))
.build()));
int i = this.getAge(immutableBlockState) + this.boneMealBonus.getInt(
SimpleContext.of(
ContextHolder.builder()
.withParameter(CommonParameters.WORLD, new BukkitWorld(world))
.withParameter(CommonParameters.LOCATION, Vec3d.atCenterOf(new Vec3i(x, y, z)))
.build()
)
);
int maxAge = this.ageProperty.max;
if (i > maxAge) {
i = maxAge;

View File

@@ -99,7 +99,7 @@ public class FallingBlockBehavior extends BukkitBlockBehavior {
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
builder.withParameter(CommonParameters.LOCATION, vec3d);
builder.withParameter(CommonParameters.WORLD, world);
for (Item<Object> item : immutableBlockState.getDrops(builder, world)) {
for (Item<Object> item : immutableBlockState.getDrops(builder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
Object entityData = Reflections.field$Entity$entityData.get(fallingBlockEntity);

View File

@@ -127,7 +127,7 @@ public class LeavesBlockBehavior extends WaterLoggedBlockBehavior {
ContextHolder.Builder builder = ContextHolder.builder()
.withParameter(CommonParameters.LOCATION, vec3d)
.withParameter(CommonParameters.WORLD, world);
for (Item<Object> item : immutableBlockState.getDrops(builder, world)) {
for (Item<Object> item : immutableBlockState.getDrops(builder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
}

View File

@@ -63,7 +63,7 @@ public class SugarCaneBlockBehavior extends BushBlockBehavior {
ContextHolder.Builder builder = ContextHolder.builder()
.withParameter(CommonParameters.LOCATION, vec3d)
.withParameter(CommonParameters.WORLD, world);
for (Item<Object> item : currentState.getDrops(builder, world)) {
for (Item<Object> item : currentState.getDrops(builder, world, null)) {
world.dropItemNaturally(vec3d, item);
}
world.playBlockSound(vec3d, currentState.sounds().breakSound());

View File

@@ -1,8 +1,10 @@
package net.momirealms.craftengine.bukkit.entity;
import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.entity.Entity;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.world.World;
import java.lang.ref.WeakReference;
@@ -62,4 +64,9 @@ public class BukkitEntity extends Entity {
public org.bukkit.entity.Entity literalObject() {
return this.entity.get();
}
@Override
public Key type() {
return KeyUtils.namespacedKey2Key(literalObject().getType().getKey());
}
}

View File

@@ -6,7 +6,6 @@ import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.bukkit.util.Reflections;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.entity.player.InteractionHand;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.loot.AbstractVanillaLootManager;
import net.momirealms.craftengine.core.loot.LootTable;
@@ -70,16 +69,17 @@ public class BukkitVanillaLootManager extends AbstractVanillaLootManager impleme
ContextHolder.Builder builder = ContextHolder.builder();
builder.withParameter(CommonParameters.WORLD, world);
builder.withParameter(CommonParameters.LOCATION, vec3d);
BukkitServerPlayer optionalPlayer = null;
if (VersionHelper.isOrAbove1_20_5()) {
if (event.getDamageSource().getCausingEntity() instanceof Player player) {
BukkitServerPlayer serverPlayer = this.plugin.adapt(player);
builder.withParameter(CommonParameters.PLAYER, serverPlayer);
builder.withOptionalParameter(CommonParameters.TOOL, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND));
optionalPlayer = this.plugin.adapt(player);
builder.withParameter(CommonParameters.PLAYER, optionalPlayer);
//mark item builder.withOptionalParameter(CommonParameters.MAIN_HAND_ITEM, serverPlayer.getItemInHand(InteractionHand.MAIN_HAND));
}
}
ContextHolder contextHolder = builder.build();
for (LootTable<?> lootTable : loot.lootTables()) {
for (Item<?> item : lootTable.getRandomItems(contextHolder, world)) {
for (Item<?> item : lootTable.getRandomItems(contextHolder, world, optionalPlayer)) {
world.dropItemNaturally(vec3d, item);
}
}

View File

@@ -75,6 +75,7 @@ public class BukkitCraftEngine extends CraftEngine {
super.classPathAppender = new ReflectionClassPathAppender(this);
super.scheduler = new BukkitSchedulerAdapter(this);
super.logger = new JavaPluginLogger(bootstrap.getLogger());
super.platform = new BukkitPlatform();
// find mod class if present
Class<?> modClass = ReflectionUtils.getClazz(MOD_CLASS);
if (modClass != null) {

View File

@@ -0,0 +1,12 @@
package net.momirealms.craftengine.bukkit.plugin;
import net.momirealms.craftengine.core.plugin.Platform;
import org.bukkit.Bukkit;
public class BukkitPlatform implements Platform {
@Override
public void dispatchCommand(String command) {
Bukkit.getServer().dispatchCommand(Bukkit.getConsoleSender(), command);
}
}

View File

@@ -26,6 +26,8 @@ import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.World;
import net.momirealms.craftengine.core.world.WorldEvents;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.block.Block;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
@@ -800,4 +802,18 @@ public class BukkitServerPlayer extends Player {
this.resourcePackUUID.clear();
}
}
@Override
public void performCommand(String command) {
platformPlayer().performCommand(command);
}
@Override
public double luck() {
if (VersionHelper.isOrAbove1_21_3()) {
return Optional.ofNullable(platformPlayer().getAttribute(Attribute.LUCK)).map(AttributeInstance::getValue).orElse(1d);
} else {
return LegacyAttributeUtils.getLuck(platformPlayer());
}
}
}