place_item can use non-block items. fixes #260

This commit is contained in:
gnembon committed 2020-05-05 17:18:04 -03:00
1 parent cfe1a9e729
commit aade01a3b3
3 files changed
+40 -14

No files matched your search

@@ -98,6 +98,7 @@ import net.minecraft.state.property.Property;
import net.minecraft.structure.StructurePiece;
import net.minecraft.structure.StructureStart;
import net.minecraft.text.LiteralText;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Clearable;
import net.minecraft.util.Identifier;
import net.minecraft.util.InvalidIdentifierException;
@@ -1008,6 +1009,7 @@ public class CarpetExpression
return success ? LazyValue.TRUE : LazyValue.FALSE;
});
// TODO rename to use_item
this.expr.addLazyFunction("place_item", -1, (c, t, lv) ->
{
if (lv.size()<2)
@@ -1023,18 +1025,28 @@ public class CarpetExpression
boolean sneakPlace = false;
if (lv.size() > locator.offset+1)
sneakPlace = lv.get(locator.offset+1).evalValue(c).getBoolean();
if (stackArg.getItem() instanceof BlockItem)
BlockValue.PlacementContext ctx;
try
{
ctx = BlockValue.PlacementContext.from(cc.s.getWorld(), where, facing, sneakPlace, stackArg.createStack(1, false));
}
catch (CommandSyntaxException e)
{
throw new InternalExpressionException(e.getMessage());
}
if (!(stackArg.getItem() instanceof BlockItem))
{
ActionResult useResult = ctx.getStack().useOnBlock(ctx);
if (useResult == ActionResult.CONSUME || useResult == ActionResult.SUCCESS)
{
return LazyValue.TRUE;
}
}
else
{ // not sure we need special case for block items, since useOnBlock can do that as well
BlockItem blockItem = (BlockItem) stackArg.getItem();
BlockValue.PlacementContext ctx;
try
{
ctx = BlockValue.PlacementContext.from(cc.s.getWorld(), where, facing, sneakPlace, stackArg.createStack(1, false));
}
catch (CommandSyntaxException e)
{
throw new InternalExpressionException(e.getMessage());
}
if (!ctx.canPlace())
return (_c, _t) -> Value.FALSE;
BlockState placementState = blockItem.getBlock().getPlacementState(ctx);