mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-31 04:46:37 +00:00
feat(bukkit): 一些改进
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package net.momirealms.craftengine.bukkit.block.behavior;
|
||||
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks;
|
||||
import net.momirealms.craftengine.bukkit.item.behavior.BlockItemBehavior;
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.core.block.BlockBehavior;
|
||||
import net.momirealms.craftengine.core.block.CustomBlock;
|
||||
@@ -11,60 +10,52 @@ import net.momirealms.craftengine.core.block.behavior.BlockBehaviorFactory;
|
||||
import net.momirealms.craftengine.core.block.properties.Property;
|
||||
import net.momirealms.craftengine.core.entity.player.InteractionResult;
|
||||
import net.momirealms.craftengine.core.entity.player.Player;
|
||||
import net.momirealms.craftengine.core.item.CustomItem;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.item.behavior.ItemBehavior;
|
||||
import net.momirealms.craftengine.core.item.context.UseOnContext;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.ResourceConfigUtils;
|
||||
import net.momirealms.craftengine.core.world.BlockPos;
|
||||
import net.momirealms.craftengine.core.world.World;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
public class StackableBlockBehavior extends BukkitBlockBehavior {
|
||||
public static final Factory FACTORY = new Factory();
|
||||
private final Property<Integer> amountProperty;
|
||||
private final Set<Key> allowItems;
|
||||
|
||||
public StackableBlockBehavior(CustomBlock block,Property<Integer> amountProperty) {
|
||||
public StackableBlockBehavior(CustomBlock block,Property<Integer> amountProperty, Set<Key> allowItems) {
|
||||
super(block);
|
||||
this.amountProperty = amountProperty;
|
||||
this.allowItems = allowItems;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public InteractionResult useOnBlock(UseOnContext context, ImmutableBlockState state) {
|
||||
Player player = context.getPlayer();
|
||||
if (player.isSneaking()) {
|
||||
if (player.isSecondaryUseActive()) {
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
Item<ItemStack> item = (Item<ItemStack>) context.getItem();
|
||||
if (item == null) {
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
Optional<CustomItem<ItemStack>> optionalCustomItem = item.getCustomItem();
|
||||
if (optionalCustomItem.isEmpty()) {
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
CustomItem<ItemStack> customItem = optionalCustomItem.get();
|
||||
for (ItemBehavior behavior : customItem.behaviors()) {
|
||||
if (behavior instanceof BlockItemBehavior blockItemBehavior && blockItemBehavior.blockId().equals(customBlock.id())) {
|
||||
int amount = state.get(this.amountProperty);
|
||||
if (amount < this.amountProperty.possibleValues().getLast()) {
|
||||
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, true)) {
|
||||
FastNMS.INSTANCE.method$ItemStack$consume(item.getLiteralObject(), 1, player.serverPlayer());
|
||||
player.swingHand(context.getHand());
|
||||
}
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
return InteractionResult.PASS;
|
||||
if (this.allowItems.contains(item.id()) && state.get(this.amountProperty) < this.amountProperty.possibleValues().getLast()) {
|
||||
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, true)) {
|
||||
FastNMS.INSTANCE.method$ItemStack$consume(item.getLiteralObject(), 1, player.serverPlayer());
|
||||
player.swingHand(context.getHand());
|
||||
}
|
||||
return InteractionResult.FAIL;
|
||||
}
|
||||
return InteractionResult.PASS;
|
||||
}
|
||||
@@ -75,7 +66,14 @@ public class StackableBlockBehavior extends BukkitBlockBehavior {
|
||||
@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");
|
||||
return new StackableBlockBehavior(block, amount);
|
||||
Set<Key> allowItems = new HashSet<>();
|
||||
if (arguments.get("allow-items") instanceof List<?> list) {
|
||||
for (Object obj : list) {
|
||||
if (obj == null) continue;
|
||||
allowItems.add(Key.of(obj.toString()));
|
||||
}
|
||||
}
|
||||
return new StackableBlockBehavior(block, amount, allowItems);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user