Skip to content

Commit

Permalink
fix: check block states to avoid invalid states
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jul 8, 2024
1 parent e976a99 commit ba117ed
Showing 1 changed file with 42 additions and 13 deletions.
55 changes: 42 additions & 13 deletions src/main/java/fi/dy/masa/tweakeroo/tweaks/PlacementHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import net.minecraft.block.*;
import net.minecraft.block.enums.BlockHalf;
Expand Down Expand Up @@ -32,36 +35,27 @@ public class PlacementHandler
// BooleanProperty:
// INVERTED - DaylightDetector
// OPEN - Barrel, Door, FenceGate, Trapdoor
// PERSISTENT - Leaves
// CAN_SUMMON - Shrieker
Properties.INVERTED,
Properties.OPEN,
Properties.PERSISTENT,
Properties.CAN_SUMMON,
// EnumProperty:
// ATTACHMENT - Bell
// AXIS - Pillar
// BED_PART - Beds
// BLOCK_HALF - Stairs, Trapdoor
// BLOCK_FACE - Button, Grindstone, Lever
// CHEST_TYPE - Chest
// COMPARATOR_MODE - Comparator
// DOOR_HINGE - Door
// DOUBLE_BLOCK_HALF - Doors, Plants
// ORIENTATION - Crafter
// RAIL_SHAPE / STRAIGHT_RAIL_SHAPE - Rails
// SLAB_TYPE - Slab - PARTIAL ONLY: TOP and BOTTOM, not DOUBLE
// STAIR_SHAPE - Stairs (needed to get the correct state, otherwise the player facing would be a factor)
// BLOCK_FACE - Button, Grindstone, Lever
Properties.ATTACHMENT,
Properties.AXIS,
Properties.BED_PART,
Properties.BLOCK_HALF,
Properties.BLOCK_FACE,
Properties.CHEST_TYPE,
Properties.COMPARATOR_MODE,
Properties.DOOR_HINGE,
Properties.DOUBLE_BLOCK_HALF,
Properties.ORIENTATION,
Properties.RAIL_SHAPE,
Properties.STRAIGHT_RAIL_SHAPE,
Expand All @@ -78,6 +72,16 @@ public class PlacementHandler
Properties.ROTATION
);

public static final ImmutableMap<Property<?>, BiFunction<BlockState, UseContext, Boolean>> PROPERTY_VALIDATORS = ImmutableMap.<Property<?>, BiFunction<BlockState, UseContext, Boolean>>builder()
.put(Properties.HORIZONTAL_FACING, (value, ctx) -> {
if (value.getProperties().contains(Properties.BLOCK_FACE))
{
return value.canPlaceAt(ctx.getWorld(), ctx.getPos());
}
return true;
})
.build();

public static EasyPlacementProtocol getEffectiveProtocolVersion()
{
EasyPlacementProtocol protocol = (EasyPlacementProtocol) Configs.Generic.ACCURATE_PLACEMENT_PROTOCOL_MODE.getOptionListValue();
Expand Down Expand Up @@ -185,9 +189,10 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
//System.out.printf("hit vec.x %s, pos.x: %s\n", context.getHitVec().getX(), context.getPos().getX());
//System.out.printf("raw protocol value in: 0x%08X\n", protocolValue);

BlockState oldState = state;
if (protocolValue < 0)
{
return state;
return oldState;
}

@Nullable DirectionProperty property = fi.dy.masa.malilib.util.BlockUtils.getFirstDirectionProperty(state);
Expand All @@ -197,6 +202,18 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
{
//System.out.printf("applying: 0x%08X\n", protocolValue);
state = applyDirectionProperty(state, context, property, protocolValue);
var validator = PROPERTY_VALIDATORS.get(property);

if ((validator == null || validator.apply(state, context)) && state.canPlaceAt(context.getWorld(), context.getPos()))
{
System.out.printf("validator passed for \"%s\"\n", property.getName());
oldState = state;
}
else
{
System.out.printf("validator failed for \"%s\"\n", property.getName());
state = oldState;
}

if (state == null)
{
Expand Down Expand Up @@ -227,7 +244,7 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
int requiredBits = MathHelper.floorLog2(MathHelper.smallestEncompassingPowerOfTwo(list.size()));
int bitMask = ~(0xFFFFFFFF << requiredBits);
int valueIndex = protocolValue & bitMask;
//System.out.printf("trying to apply valInd: %d, bits: %d, prot val: 0x%08X\n", valueIndex, requiredBits, protocolValue);
System.out.printf("trying to apply valInd: %d, bits: %d, prot val: 0x%08X\n", valueIndex, requiredBits, protocolValue);

if (valueIndex >= 0 && valueIndex < list.size())
{
Expand All @@ -236,8 +253,20 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
if (state.get(prop).equals(value) == false &&
value != SlabType.DOUBLE) // don't allow duping slabs by forcing a double slab via the protocol
{
//System.out.printf("applying %s: %s\n", prop.getName(), value);
System.out.printf("applying \"%s\": %s\n", prop.getName(), value);
state = state.with(prop, value);

BiFunction<BlockState, UseContext, Boolean> validator = PROPERTY_VALIDATORS.get(prop);
if ((validator == null || validator.apply(state, context)) && state.canPlaceAt(context.getWorld(), context.getPos()))
{
System.out.printf("validator passed for \"%s\"\n", prop.getName());
oldState = state;
}
else
{
System.out.printf("validator failed for \"%s\"\n", prop.getName());
state = oldState;
}
}

protocolValue >>>= requiredBits;
Expand Down Expand Up @@ -274,7 +303,7 @@ else if (decodedFacingIndex >= 0 && decodedFacingIndex <= 5)
}
}

//System.out.printf("plop facing: %s -> %s (raw: %d, dec: %d)\n", facingOrig, facing, protocolValue, decodedFacingIndex);
System.out.printf("plop facing: %s -> %s (raw: %d, dec: %d)\n", facingOrig, facing, protocolValue, decodedFacingIndex);

if (facing != facingOrig && property.getValues().contains(facing))
{
Expand Down

0 comments on commit ba117ed

Please sign in to comment.