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

优化新增的两个行为

This commit is contained in:
XiaoMoMi
2025-06-18 17:24:47 +08:00
parent cef339c8c6
commit 89c35d2d41
5 changed files with 48 additions and 34 deletions

View File

@@ -13,6 +13,7 @@ import net.momirealms.craftengine.core.block.UpdateOption;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
import net.momirealms.craftengine.core.plugin.context.parameter.DirectContextParameters;
import net.momirealms.craftengine.core.sound.SoundData;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.world.World;
import net.momirealms.craftengine.core.world.WorldEvents;
@@ -116,7 +117,8 @@ public final class CraftEngineBlocks {
if (success) {
FastNMS.INSTANCE.method$BlockStateBase$onPlace(blockState, worldServer, blockPos, oldBlockState, false);
if (playSound) {
location.getWorld().playSound(location, block.sounds().placeSound().id().toString(), SoundCategory.BLOCKS, block.sounds().placeSound().volume(), block.sounds().placeSound().pitch());
SoundData data = block.sounds().placeSound();
location.getWorld().playSound(location, data.id().toString(), SoundCategory.BLOCKS, data.volume(), data.pitch());
}
}
return success;

View File

@@ -1,19 +1,20 @@
package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.util.LocationUtils;
import net.momirealms.craftengine.core.block.BlockBehavior;
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.block.behavior.BlockBehaviorFactory;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.block.properties.IntegerProperty;
import net.momirealms.craftengine.core.entity.player.InteractionResult;
import net.momirealms.craftengine.core.entity.player.Player;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.item.context.UseOnContext;
import net.momirealms.craftengine.core.sound.SoundData;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
import net.momirealms.craftengine.core.world.BlockPos;
import net.momirealms.craftengine.core.world.World;
@@ -21,22 +22,19 @@ import org.bukkit.Location;
import org.bukkit.SoundCategory;
import org.bukkit.inventory.ItemStack;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class StackableBlockBehavior extends BukkitBlockBehavior {
public static final Factory FACTORY = new Factory();
private final Property<Integer> amountProperty;
private final Set<Key> items;
private final SoundData soundData;
private final IntegerProperty amountProperty;
private final List<Key> items;
private final SoundData stackSound;
public StackableBlockBehavior(CustomBlock block,Property<Integer> amountProperty, Set<Key> items, SoundData soundData) {
public StackableBlockBehavior(CustomBlock block, IntegerProperty amountProperty, List<Key> items, SoundData stackSound) {
super(block);
this.amountProperty = amountProperty;
this.items = items;
this.soundData = soundData;
this.stackSound = stackSound;
}
@Override
@@ -50,18 +48,17 @@ public class StackableBlockBehavior extends BukkitBlockBehavior {
if (item == null) {
return InteractionResult.PASS;
}
if (this.items.contains(item.id()) && state.get(this.amountProperty) < this.amountProperty.possibleValues().getLast()) {
if (this.items.contains(item.id()) && state.get(this.amountProperty) < this.amountProperty.max) {
ImmutableBlockState nextStage = state.cycle(this.amountProperty);
World world = context.getLevel();
BlockPos pos = context.getClickedPos();
Location location = new Location((org.bukkit.World) world.platformWorld(), pos.x(), pos.y(), pos.z());
if (CraftEngineBlocks.place(location, nextStage, UpdateOption.UPDATE_NONE, false)) {
if (soundData != null) {
location.getWorld().playSound(location, soundData.id().toString(), SoundCategory.BLOCKS, soundData.volume(), soundData.pitch());
}
FastNMS.INSTANCE.method$ItemStack$consume(item.getLiteralObject(), 1, player.serverPlayer());
player.swingHand(context.getHand());
FastNMS.INSTANCE.method$LevelWriter$setBlock(world.serverWorld(), LocationUtils.toBlockPos(pos), nextStage.customBlockState().handle(), UpdateOption.UPDATE_NONE.flags());
if (stackSound != null) {
location.getWorld().playSound(location, stackSound.id().toString(), SoundCategory.BLOCKS, stackSound.volume(), stackSound.pitch());
}
FastNMS.INSTANCE.method$ItemStack$consume(item.getLiteralObject(), 1, player.serverPlayer());
player.swingHand(context.getHand());
return InteractionResult.SUCCESS_AND_CANCEL;
}
return InteractionResult.PASS;
@@ -72,16 +69,15 @@ public class StackableBlockBehavior extends BukkitBlockBehavior {
@Override
@SuppressWarnings("unchecked")
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
Property<Integer> amount = (Property<Integer>) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("amount"), "warning.config.block.behavior.stackable.missing_amount");
SoundData soundData = arguments.containsKey("sound") ? SoundData.create(arguments.get("sound"), 1f, 0.8f) : null;
Set<Key> items = new HashSet<>();
if (arguments.get("items") instanceof List<?> list) {
for (Object obj : list) {
if (obj == null) continue;
items.add(Key.of(obj.toString()));
}
IntegerProperty amount = (IntegerProperty) ResourceConfigUtils.requireNonNullOrThrow(block.getProperty("amount"), "warning.config.block.behavior.stackable.missing_amount");
Map<String, Object> sounds = (Map<String, Object>) arguments.get("sounds");
SoundData stackSound = null;
if (sounds != null) {
stackSound = Optional.ofNullable(sounds.get("stack")).map(obj -> SoundData.create(obj, 1, 1)).orElse(null);
}
return new StackableBlockBehavior(block, amount, items, soundData);
Object itemsObj = ResourceConfigUtils.requireNonNullOrThrow(arguments.get("items"), "warning.config.block.behavior.stackable.missing_items");
List<Key> items = MiscUtils.getAsStringList(itemsObj).stream().map(Key::of).toList();
return new StackableBlockBehavior(block, amount, items, stackSound);
}
}
}

View File

@@ -1,10 +1,13 @@
package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.bukkit.util.DirectionUtils;
import net.momirealms.craftengine.core.block.BlockBehavior;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
@@ -15,10 +18,12 @@ import java.util.Map;
public class SturdyBaseBlockBehavior extends AbstractCanSurviveBlockBehavior {
public static final Factory FACTORY = new Factory();
private final Direction direction;
private final boolean stackable;
public SturdyBaseBlockBehavior(CustomBlock block, int delay, Direction direction) {
public SturdyBaseBlockBehavior(CustomBlock block, int delay, Direction direction, boolean stackable) {
super(block, delay);
this.direction = direction;
this.stackable = stackable;
}
@Override
@@ -28,10 +33,18 @@ public class SturdyBaseBlockBehavior extends AbstractCanSurviveBlockBehavior {
int z = FastNMS.INSTANCE.field$Vec3i$z(blockPos) + this.direction.stepZ();
Object targetPos = FastNMS.INSTANCE.constructor$BlockPos(x, y, z);
Object blockState = FastNMS.INSTANCE.method$BlockGetter$getBlockState(world, targetPos);
return (boolean) CoreReflections.method$BlockStateBase$isFaceSturdy.invoke(
blockState, world, blockPos, DirectionUtils.toNMSDirection(this.direction.opposite()),
if ((boolean) CoreReflections.method$BlockStateBase$isFaceSturdy.invoke(
blockState, world, targetPos, DirectionUtils.toNMSDirection(this.direction.opposite()),
CoreReflections.instance$SupportType$FULL
);
)) {
return true;
}
if (!this.stackable) {
return false;
}
ImmutableBlockState targetCustomState = BukkitBlockManager.instance().getImmutableBlockState(BlockStateUtils.blockStateToId(blockState));
if (targetCustomState == null || targetCustomState.isEmpty()) return false;
return targetCustomState.owner().value() == super.customBlock;
}
public static class Factory implements BlockBehaviorFactory {
@@ -39,8 +52,9 @@ public class SturdyBaseBlockBehavior extends AbstractCanSurviveBlockBehavior {
@Override
public BlockBehavior create(CustomBlock block, Map<String, Object> arguments) {
int delay = ResourceConfigUtils.getAsInt(arguments.getOrDefault("delay", 0), "delay");
Direction direction = Direction.valueOf(arguments.getOrDefault("direction", "down").toString().toUpperCase(Locale.ROOT));
return new SturdyBaseBlockBehavior(block, delay, direction);
Direction direction = Direction.valueOf(arguments.getOrDefault("direction", "down").toString().toUpperCase(Locale.ENGLISH));
boolean stackable = (boolean) arguments.getOrDefault("stackable", false);
return new SturdyBaseBlockBehavior(block, delay, direction, stackable);
}
}
}