Skip to content

Commit

Permalink
fix: easy place allowing placement of invalid block states by introdu…
Browse files Browse the repository at this point in the history
…cing a Black List.
  • Loading branch information
sakura-ryoko committed Dec 28, 2024
1 parent a0ebaef commit b23668d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 6 deletions.
52 changes: 47 additions & 5 deletions src/main/java/fi/dy/masa/litematica/util/PlacementHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.List;
import java.util.Optional;
import com.google.common.collect.ImmutableSet;

import net.minecraft.block.*;
import net.minecraft.block.enums.BlockHalf;
import net.minecraft.block.enums.ComparatorMode;
Expand Down Expand Up @@ -39,9 +40,8 @@ public class PlacementHandler
Properties.INVERTED,
Properties.OPEN,
//Properties.PERSISTENT,
// TODO --> TEST (Boolean)
Properties.POWERED,
Properties.LOCKED,
//Properties.POWERED,
//Properties.LOCKED,
// EnumProperty:
// ATTACHMENT - Bells
// AXIS - Pillar
Expand Down Expand Up @@ -81,6 +81,14 @@ public class PlacementHandler
Properties.ROTATION
);

/**
* BlackList for Block States. Entries here will be reset to their default value.
*/
public static final ImmutableSet<Property<?>> BLACKLISTED_PROPERTIES = ImmutableSet.of(
Properties.WATERLOGGED,
Properties.POWERED
);

public static EasyPlaceProtocol getEffectiveProtocolVersion()
{
EasyPlaceProtocol protocol = (EasyPlaceProtocol) Configs.Generic.EASY_PLACE_PROTOCOL.getOptionListValue();
Expand Down Expand Up @@ -181,7 +189,7 @@ else if (block instanceof ComparatorBlock)
{
state = state.with(Properties.BLOCK_HALF, protocolValue > 0 ? BlockHalf.TOP : BlockHalf.BOTTOM);
}

//System.out.printf("[PHv2] stateOut: %s\n", state.toString());

return state;
Expand Down Expand Up @@ -237,10 +245,23 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
{
for (Property<?> p : propList)
{
//if (((p instanceof EnumProperty<?> ep) && ep.getType().equals(Direction.class) == false) &&
//System.out.printf("[PHv3] check property [%s], whitelisted [%s], blacklisted [%s]\n", p.getName(), WHITELISTED_PROPERTIES.contains(p), BLACKLISTED_PROPERTIES.contains(p));

if ((property.isPresent() && !property.get().equals(p)) ||
(property.isEmpty()) &&
WHITELISTED_PROPERTIES.contains(p))
//WHITELISTED_PROPERTIES.contains(p) &&
//!BLACKLISTED_PROPERTIES.contains(p))

/*
if (property.isPresent() && property.get().equals(p))
{
//System.out.printf("[PHv3] skipping prot val: 0x%08X [Property %s]\n", protocolValue, p.getName());
continue;
}
else if (WHITELISTED_PROPERTIES.contains(p) &&
!BLACKLISTED_PROPERTIES.contains(p))
*/
{
@SuppressWarnings("unchecked")
Property<T> prop = (Property<T>) p;
Expand All @@ -250,6 +271,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("[PHv3] trying to apply valInd: %d, bits: %d, prot val: 0x%08X [Property %s]\n", valueIndex, requiredBits, protocolValue, prop.getName());

if (valueIndex >= 0 && valueIndex < list.size())
Expand Down Expand Up @@ -277,13 +299,33 @@ public static <T extends Comparable<T>> BlockState applyPlacementProtocolV3(Bloc
protocolValue >>>= requiredBits;
}
}
/*
else
{
System.out.printf("[PHv3] skipping prot val: 0x%08X [Property %s]\n", protocolValue, p.getName());
}
*/
}
}
catch (Exception e)
{
Litematica.logger.warn("Exception trying to apply placement protocol value", e);
}

// Strip Blacklisted properties, and use the Block's default state.
// This needs to be done after the initial loop, or it breaks compatibility
for (Property<?> p : BLACKLISTED_PROPERTIES)
{
if (state.contains(p))
{
@SuppressWarnings("unchecked")
Property<T> prop = (Property<T>) p;
BlockState def = state.getBlock().getDefaultState();
state = state.with(prop, def.get(prop));
//System.out.printf("[PHv3] blacklisted state [%s] found, setting default value\n", prop.getName());
}
}

if (state.canPlaceAt(context.getWorld(), context.getPos()))
{
//System.out.printf("[PHv3] validator passed for \"%s\"\n", state);
Expand Down
21 changes: 20 additions & 1 deletion src/main/java/fi/dy/masa/litematica/util/WorldUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -882,10 +882,23 @@ public static <T extends Comparable<T>> Vec3d applyPlacementProtocolV3(BlockPos
{
for (Property<?> p : propList)
{
//if (((p instanceof EnumProperty<?> ep) && ep.getType().equals(Direction.class) == false) &&
//System.out.printf("(WorldUtils):v3: check property [%s], whitelisted [%s], blacklisted [%s]\n", p.getName(), PlacementHandler.WHITELISTED_PROPERTIES.contains(p), PlacementHandler.BLACKLISTED_PROPERTIES.contains(p));

/*
if ((property.isPresent() && !property.get().equals(p)) ||
(property.isEmpty()) &&
PlacementHandler.WHITELISTED_PROPERTIES.contains(p))
//PlacementHandler.WHITELISTED_PROPERTIES.contains(p) &&
//!PlacementHandler.BLACKLISTED_PROPERTIES.contains(p))
*/

if (property.isPresent() && property.get().equals(p))
{
//System.out.printf("(WorldUtils):v3: skipping prot val: 0x%08X [Property %s]\n", protocolValue, p.getName());
continue;
}
if (PlacementHandler.WHITELISTED_PROPERTIES.contains(p) &&
!PlacementHandler.BLACKLISTED_PROPERTIES.contains(p))
{
@SuppressWarnings("unchecked")
Property<T> prop = (Property<T>) p;
Expand All @@ -905,6 +918,12 @@ public static <T extends Comparable<T>> Vec3d applyPlacementProtocolV3(BlockPos
++propCount;
}
}
/*
else
{
System.out.printf("(WorldUtils):v3: skipping prot val: 0x%08X [Property %s]\n", protocolValue, p.getName());
}
*/
}
}
catch (Exception e)
Expand Down

0 comments on commit b23668d

Please sign in to comment.