From ff203a0e730c77c29ddf1c23b6d1d69424bb4a8a Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 25 Jun 2019 09:33:13 -0300 Subject: [PATCH] inventories --- .../java/carpet/script/CarpetExpression.java | 64 +++++++-- .../java/carpet/script/value/BlockValue.java | 122 ++++++++++++++++++ 2 files changed, 178 insertions(+), 8 deletions(-) diff --git a/src/main/java/carpet/script/CarpetExpression.java b/src/main/java/carpet/script/CarpetExpression.java index 8d8f14b2..734ee0fa 100644 --- a/src/main/java/carpet/script/CarpetExpression.java +++ b/src/main/java/carpet/script/CarpetExpression.java @@ -36,6 +36,10 @@ import net.minecraft.entity.ai.brain.task.LookTargetUtil; import net.minecraft.entity.decoration.ArmorStandEntity; import net.minecraft.entity.passive.AbstractTraderEntity; import net.minecraft.entity.passive.VillagerEntity; +import net.minecraft.item.AutomaticItemPlacementContext; +import net.minecraft.item.BlockItem; +import net.minecraft.item.ItemPlacementContext; +import net.minecraft.item.ItemUsageContext; import net.minecraft.item.Items; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.command.arguments.ParticleArgumentType; @@ -51,6 +55,7 @@ import net.minecraft.state.property.Property; import net.minecraft.state.StateFactory; import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.text.LiteralText; +import net.minecraft.util.Hand; import net.minecraft.util.Identifier; import net.minecraft.sound.SoundCategory; import net.minecraft.util.math.*; @@ -816,6 +821,47 @@ public class CarpetExpression Value retval = new StringValue(state.get(property).toString()); return (_c, _t) -> retval; }); + this.expr.addLazyFunction("place_item", -1, (c, t, lv) -> + { + CarpetContext cc = (CarpetContext) c; + String itemString = lv.get(0).evalValue(c).getString(); + BlockValue.VectorLocator locator = BlockValue.locateVec(cc, lv, 1); + ItemStackArgument stackArg = NBTSerializableValue.parseItem(itemString); + BlockPos where = new BlockPos(locator.vec); + Direction side = Direction.UP; + if (lv.size() > locator.offset) + { + String key = lv.get(locator.offset).evalValue(c).getString(); + side = BlockValue.DIRECTION_MAP.get(key); + if (side == null) + throw new InternalExpressionException("unknown direction: " + key); + } + if (stackArg.getItem() instanceof BlockItem) + { + BlockItem blockItem = (BlockItem) stackArg.getItem(); + ItemPlacementContext ctx = null; + try + { + ctx = new AutomaticItemPlacementContext(cc.s.getWorld(),new BlockPos(locator.vec), side, stackArg.createStack(1, false), side ); + } + catch (CommandSyntaxException e) + { + throw new InternalExpressionException(e.getMessage()); + } + BlockState placementState = blockItem.getBlock().getPlacementState(ctx); + if (placementState != null) + { + if (placementState.canPlaceAt(cc.s.getWorld(), where)) + { + CarpetSettings.impendingFillSkipUpdates = !CarpetSettings.fillUpdates; + cc.s.getWorld().setBlockState(where, placementState, 2); + CarpetSettings.impendingFillSkipUpdates = false; + return (_c, _t) -> Value.TRUE; + } + } + } + return (_c, _t) -> Value.FALSE; + }); } @@ -940,7 +986,7 @@ public class CarpetExpression CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); - if (lv.size() <= inventoryLocator.offset+2) + if (lv.size() < inventoryLocator.offset+2) throw new InternalExpressionException("inventory_set requires at least slot number and new stack size, and optional new item"); int slot = (int) NumericValue.asNumber(lv.get(inventoryLocator.offset+0).evalValue(c)).getLong(); slot = NBTSerializableValue.validateSlot(slot, inventoryLocator.inventory); @@ -952,7 +998,7 @@ public class CarpetExpression //Value res = ListValue.fromItemStack(removedStack); return (_c, _t) -> ListValue.fromItemStack(removedStack); // that tuple will be read only but cheaper if noone cares } - if (lv.size() <= inventoryLocator.offset+3) + if (lv.size() < inventoryLocator.offset+3) { ItemStack previousStack = inventoryLocator.inventory.getInvStack(slot); ItemStack newStack = previousStack.copy(); @@ -980,18 +1026,20 @@ public class CarpetExpression CarpetContext cc = (CarpetContext) c; NBTSerializableValue.InventoryLocator inventoryLocator = NBTSerializableValue.locateInventory(cc, lv, 0); - if (lv.size() <= inventoryLocator.offset+1) + if (lv.size() <= inventoryLocator.offset) throw new InternalExpressionException("inventory_set requires at least an item to be removed"); ItemStackArgument searchItem = NBTSerializableValue.parseItem(lv.get(inventoryLocator.offset).evalValue(c).getString()); int amount = 1; - if (lv.size() > inventoryLocator.offset+2) + if (lv.size() > inventoryLocator.offset+1) { - amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset+2).evalValue(c)).getLong(); + amount = (int)NumericValue.asNumber(lv.get(inventoryLocator.offset+1).evalValue(c)).getLong(); } // not enough - if ( (amount == 1) && (inventoryLocator.inventory.containsAnyInInv(Sets.newHashSet(searchItem.getItem()))) || - inventoryLocator.inventory.getInvAmountOf(searchItem.getItem()) >= amount ) + if (((amount == 1) && (!inventoryLocator.inventory.containsAnyInInv(Sets.newHashSet(searchItem.getItem())))) + || (inventoryLocator.inventory.countInInv(searchItem.getItem()) < amount)) + { return (_c, _t) -> Value.FALSE; + } for (int i = 0, maxi = inventoryLocator.inventory.getInvSize(); i < maxi; i++) { ItemStack stack = inventoryLocator.inventory.getInvStack(i); @@ -1017,9 +1065,9 @@ public class CarpetExpression return (_c, _t) -> Value.TRUE; }); - } + /** *

Entity API

*
diff --git a/src/main/java/carpet/script/value/BlockValue.java b/src/main/java/carpet/script/value/BlockValue.java index f113788c..045622f5 100644 --- a/src/main/java/carpet/script/value/BlockValue.java +++ b/src/main/java/carpet/script/value/BlockValue.java @@ -8,7 +8,14 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.minecraft.block.BlockState; import net.minecraft.command.arguments.BlockArgumentParser; import net.minecraft.block.Blocks; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.AutomaticItemPlacementContext; +import net.minecraft.item.ItemPlacementContext; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.Direction; import net.minecraft.util.math.Vec3d; import net.minecraft.util.registry.Registry; import net.minecraft.world.World; @@ -19,6 +26,14 @@ import java.util.Map; public class BlockValue extends Value { + public static final Map DIRECTION_MAP = new HashMap() {{ + put("up", Direction.UP); + put("down", Direction.DOWN); + put("east", Direction.EAST); + put("west", Direction.WEST); + put("north", Direction.NORTH); + put("south", Direction.SOUTH); + }}; public static final BlockValue AIR = new BlockValue(Blocks.AIR.getDefaultState(), null, BlockPos.ORIGIN); public static final BlockValue NULL = new BlockValue(null, null, null); private BlockState blockState; @@ -239,4 +254,111 @@ public class BlockValue extends Value pitch = p; } } + + /*public enum SpecificDirection { + UP("up",0.5, 0.0, 0.5, Direction.UP), + + UPNORTH ("upnorth", 0.5, 0.0, 0.4, Direction.NORTH), + UPSOUTH ("upsouth", 0.5, 0.0, 0.6, Direction.SOUTH), + UPEAST("upeast", 0.6, 0.0, 0.5, Direction.EAST), + UPWEST("upwest", 0.4, 0.0, 0.5, Direction.WEST), + + DOWN("down", 0.5, 1.0, 0.5, Direction.DOWN), + + DOWNNORTH ("downnorth", 0.5, 0.0, 0.4, Direction.NORTH), + DOWNSOUTH ("downsouth", 0.5, 0.0, 0.6, Direction.SOUTH), + DOWNEAST("downeast", 0.6, 0.0, 0.5, Direction.EAST), + DOWNWEST("downwest", 0.4, 0.0, 0.5, Direction.WEST), + + + NORTH ("north", 0.5, 0.4, 1.0, Direction.NORTH), + SOUTH ("south", 0.5, 0.4, 0.0, Direction.SOUTH), + EAST("east", 0.0, 0.4, 0.5, Direction.EAST), + WEST("west", 1.0, 0.4, 0.5, Direction.WEST), + NORTHUP ("northup", 0.5, 0.6, 1.0, Direction.NORTH), + SOUTHUP ("southup", 0.5, 0.6, 0.0, Direction.SOUTH), + EASTUP("eastup", 0.0, 0.6, 0.5, Direction.EAST), + WESTUP("westup", 1.0, 0.6, 0.5, Direction.WEST), + + + NORTHUP (), + SOUTHUP(), + EASTUP(), + WESTUP(); + + private static final Direction[] ALL = values(); + private static final Map NAME_MAP = (Map)Arrays.stream(ALL).collect(Collectors.toMap(Direction::getName, (direction_1) -> { + return direction_1; + })); + + private SpecificDirection(String name, double hitx, double hity, double hitz, Direction blockFacing) + { + + } + } + + public class PlacementContext extends ItemPlacementContext { + private final Direction facing; + + public static final Map DIRECTION_MAP = new HashMap() {{ + put("up", Direction.UP); + put("down", Direction.DOWN); + put("east", Direction.EAST); + put("west", Direction.WEST); + put("north", Direction.NORTH); + put("south", Direction.SOUTH); + }}; + public + + public PlacementContext(World world_1, BlockPos blockPos_1, Direction direction_1, ItemStack itemStack_1, Direction direction_2) { + super(world_1, (PlayerEntity)null, Hand.MAIN_HAND, itemStack_1, new BlockHitResult(new Vec3d((double)blockPos_1.getX() + 0.5D, (double)blockPos_1.getY(), (double)blockPos_1.getZ() + 0.5D), direction_2, blockPos_1, false)); + this.facing = direction_1; + } + + public BlockPos getBlockPos() { + return this.hit.getBlockPos(); + } + + public boolean canPlace() { + return this.world.getBlockState(this.hit.getBlockPos()).canReplace(this); + } + + public boolean canReplaceExisting() { + return this.canPlace(); + } + + public Direction getPlayerLookDirection() { + return Direction.DOWN; + } + + public Direction[] getPlacementDirections() { + switch(this.facing) { + case DOWN: + default: + return new Direction[]{Direction.DOWN, Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, Direction.UP}; + case UP: + return new Direction[]{Direction.DOWN, Direction.UP, Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST}; + case NORTH: + return new Direction[]{Direction.DOWN, Direction.NORTH, Direction.EAST, Direction.WEST, Direction.UP, Direction.SOUTH}; + case SOUTH: + return new Direction[]{Direction.DOWN, Direction.SOUTH, Direction.EAST, Direction.WEST, Direction.UP, Direction.NORTH}; + case WEST: + return new Direction[]{Direction.DOWN, Direction.WEST, Direction.SOUTH, Direction.UP, Direction.NORTH, Direction.EAST}; + case EAST: + return new Direction[]{Direction.DOWN, Direction.EAST, Direction.SOUTH, Direction.UP, Direction.NORTH, Direction.WEST}; + } + } + + public Direction getPlayerFacing() { + return this.facing.getAxis() == Direction.Axis.Y ? Direction.NORTH : this.facing; + } + + public boolean isPlayerSneaking() { + return false; + } + + public float getPlayerYaw() { + return (float)(this.facing.getHorizontal() * 90); + } + }*/ }