item placement

This commit is contained in:
gnembon committed 2019-06-25 12:17:04 -03:00
1 parent ff203a0e73
commit 4b1a1d41dc
3 files changed
+61 -56

No files matched your search

@@ -827,27 +827,31 @@ public class CarpetExpression
String itemString = lv.get(0).evalValue(c).getString(); String itemString = lv.get(0).evalValue(c).getString();
BlockValue.VectorLocator locator = BlockValue.locateVec(cc, lv, 1); BlockValue.VectorLocator locator = BlockValue.locateVec(cc, lv, 1);
ItemStackArgument stackArg = NBTSerializableValue.parseItem(itemString); ItemStackArgument stackArg = NBTSerializableValue.parseItem(itemString);
BlockPos where = new BlockPos(locator.vec); if (stackArg == null)
Direction side = Direction.UP;
if (lv.size() > locator.offset)
{ {
String key = lv.get(locator.offset).evalValue(c).getString(); throw new InternalExpressionException("unknown item: "+itemString);
side = BlockValue.DIRECTION_MAP.get(key);
if (side == null)
throw new InternalExpressionException("unknown direction: " + key);
} }
BlockPos where = new BlockPos(locator.vec);
String facing = "up";
if (lv.size() > locator.offset)
facing = lv.get(locator.offset).evalValue(c).getString();
boolean sneakPlace = false;
if (lv.size() > locator.offset+1)
sneakPlace = lv.get(locator.offset+1).evalValue(c).getBoolean();
if (stackArg.getItem() instanceof BlockItem) if (stackArg.getItem() instanceof BlockItem)
{ {
BlockItem blockItem = (BlockItem) stackArg.getItem(); BlockItem blockItem = (BlockItem) stackArg.getItem();
ItemPlacementContext ctx = null; BlockValue.PlacementContext ctx;
try try
{ {
ctx = new AutomaticItemPlacementContext(cc.s.getWorld(),new BlockPos(locator.vec), side, stackArg.createStack(1, false), side ); ctx = BlockValue.PlacementContext.from(cc.s.getWorld(), where, facing, sneakPlace, stackArg.createStack(1, false));
} }
catch (CommandSyntaxException e) catch (CommandSyntaxException e)
{ {
throw new InternalExpressionException(e.getMessage()); throw new InternalExpressionException(e.getMessage());
} }
if (!ctx.canPlace())
return (_c, _t) -> Value.FALSE;
BlockState placementState = blockItem.getBlock().getPlacementState(ctx); BlockState placementState = blockItem.getBlock().getPlacementState(ctx);
if (placementState != null) if (placementState != null)
{ {
+4 -4
View File
@@ -2770,10 +2770,10 @@ public class Expression implements Cloneable
{ {
return exit.retval; return exit.retval;
} }
catch (StackOverflowError ignored) //catch (StackOverflowError ignored)
{ //{
throw new ExpressionException("Your thoughts are too deep"); // throw new ExpressionException("Your thoughts are too deep");
} //}
catch (InternalExpressionException exc) catch (InternalExpressionException exc)
{ {
throw new ExpressionException("Your expression result is incorrect:"+exc.getMessage()); throw new ExpressionException("Your expression result is incorrect:"+exc.getMessage());
@@ -20,9 +20,11 @@ import net.minecraft.util.math.Vec3d;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraft.world.World; import net.minecraft.world.World;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors;
public class BlockValue extends Value public class BlockValue extends Value
{ {
@@ -255,80 +257,79 @@ public class BlockValue extends Value
} }
} }
/*public enum SpecificDirection { public enum SpecificDirection {
UP("up",0.5, 0.0, 0.5, Direction.UP), UP("up",0.5, 0.0, 0.5, Direction.UP),
UPNORTH ("upnorth", 0.5, 0.0, 0.4, Direction.NORTH), UPNORTH ("upnorth", 0.5, 0.0, 0.4, Direction.UP),
UPSOUTH ("upsouth", 0.5, 0.0, 0.6, Direction.SOUTH), UPSOUTH ("upsouth", 0.5, 0.0, 0.6, Direction.UP),
UPEAST("upeast", 0.6, 0.0, 0.5, Direction.EAST), UPEAST("upeast", 0.6, 0.0, 0.5, Direction.UP),
UPWEST("upwest", 0.4, 0.0, 0.5, Direction.WEST), UPWEST("upwest", 0.4, 0.0, 0.5, Direction.UP),
DOWN("down", 0.5, 1.0, 0.5, Direction.DOWN), DOWN("down", 0.5, 1.0, 0.5, Direction.DOWN),
DOWNNORTH ("downnorth", 0.5, 0.0, 0.4, Direction.NORTH), DOWNNORTH ("downnorth", 0.5, 1.0, 0.4, Direction.DOWN),
DOWNSOUTH ("downsouth", 0.5, 0.0, 0.6, Direction.SOUTH), DOWNSOUTH ("downsouth", 0.5, 1.0, 0.6, Direction.DOWN),
DOWNEAST("downeast", 0.6, 0.0, 0.5, Direction.EAST), DOWNEAST("downeast", 0.6, 1.0, 0.5, Direction.DOWN),
DOWNWEST("downwest", 0.4, 0.0, 0.5, Direction.WEST), DOWNWEST("downwest", 0.4, 1.0, 0.5, Direction.DOWN),
NORTH ("north", 0.5, 0.4, 1.0, Direction.NORTH), NORTH ("north", 0.5, 0.4, 1.0, Direction.NORTH),
SOUTH ("south", 0.5, 0.4, 0.0, Direction.SOUTH), SOUTH ("south", 0.5, 0.4, 0.0, Direction.SOUTH),
EAST("east", 0.0, 0.4, 0.5, Direction.EAST), EAST("east", 0.0, 0.4, 0.5, Direction.EAST),
WEST("west", 1.0, 0.4, 0.5, Direction.WEST), WEST("west", 1.0, 0.4, 0.5, Direction.WEST),
NORTHUP ("northup", 0.5, 0.6, 1.0, Direction.NORTH), NORTHUP ("northup", 0.5, 0.6, 1.0, Direction.NORTH),
SOUTHUP ("southup", 0.5, 0.6, 0.0, Direction.SOUTH), SOUTHUP ("southup", 0.5, 0.6, 0.0, Direction.SOUTH),
EASTUP("eastup", 0.0, 0.6, 0.5, Direction.EAST), EASTUP("eastup", 0.0, 0.6, 0.5, Direction.EAST),
WESTUP("westup", 1.0, 0.6, 0.5, Direction.WEST), WESTUP("westup", 1.0, 0.6, 0.5, Direction.WEST);
public String name;
public Vec3d hitOffset;
public Direction facing;
NORTHUP (), private static final Map<String, SpecificDirection> DIRECTION_MAP = Arrays.stream(values()).collect(Collectors.toMap(SpecificDirection::getName, d -> d));
SOUTHUP(),
EASTUP(),
WESTUP();
private static final Direction[] ALL = values();
private static final Map<String, Direction> 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) private SpecificDirection(String name, double hitx, double hity, double hitz, Direction blockFacing)
{ {
this.name = name;
this.hitOffset = new Vec3d(hitx, hity, hitz);
this.facing = blockFacing;
}
private String getName()
{
return name;
} }
} }
public class PlacementContext extends ItemPlacementContext { public static class PlacementContext extends ItemPlacementContext {
private final Direction facing; private final Direction facing;
private final boolean sneakPlace;
public static final Map<String, Direction> DIRECTION_MAP = new HashMap<String, Direction>() {{ public static PlacementContext from(World world, BlockPos pos, String direction, boolean sneakPlace, ItemStack itemStack)
put("up", Direction.UP); {
put("down", Direction.DOWN); SpecificDirection dir = SpecificDirection.DIRECTION_MAP.get(direction);
put("east", Direction.EAST); if (dir == null)
put("west", Direction.WEST); throw new InternalExpressionException("unknown block placement direction: "+direction);
put("north", Direction.NORTH); BlockHitResult hitres = new BlockHitResult(
put("south", Direction.SOUTH); new Vec3d(pos).add(dir.hitOffset),
}}; dir.facing,
public pos,
false);
public PlacementContext(World world_1, BlockPos blockPos_1, Direction direction_1, ItemStack itemStack_1, Direction direction_2) { return new PlacementContext(world, pos, dir.facing, sneakPlace, itemStack, hitres);
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)); }
public PlacementContext(World world_1, BlockPos blockPos_1, Direction direction_1, boolean sneakPlace, ItemStack itemStack_1, BlockHitResult hitres) {
super(world_1, null, Hand.MAIN_HAND, itemStack_1, hitres);
this.facing = direction_1; this.facing = direction_1;
this.sneakPlace = sneakPlace;
} }
public BlockPos getBlockPos() { public BlockPos getBlockPos() {
return this.hit.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() { public Direction getPlayerLookDirection() {
return Direction.DOWN; return facing.getOpposite();
} }
public Direction[] getPlacementDirections() { public Direction[] getPlacementDirections() {
@@ -354,11 +355,11 @@ public class BlockValue extends Value
} }
public boolean isPlayerSneaking() { public boolean isPlayerSneaking() {
return false; return sneakPlace;
} }
public float getPlayerYaw() { public float getPlayerYaw() {
return (float)(this.facing.getHorizontal() * 90); return (float)(this.facing.getHorizontal() * 90);
} }
}*/ }
} }