semi-auto cleanup

This commit is contained in:
gnembon committed 2023-02-04 13:26:21 +01:00
1 parent cad80f82c6
commit 8f61271606
99 files changed
+11463 -8007

No files matched your search

+102 -79
View File
@@ -7,12 +7,14 @@ import carpet.script.exception.Throwables;
import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;
import net.minecraft.commands.arguments.blocks.BlockStateParser;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
@@ -44,42 +46,51 @@ public class BlockValue extends Value
{
private BlockState blockState;
private final BlockPos pos;
@NotNull private final Level world;
@NotNull
private final Level world;
private CompoundTag data;
public static BlockValue fromCoords(CarpetContext c, int x, int y, int z)
public static BlockValue fromCoords(final CarpetContext c, final int x, final int y, final int z)
{
BlockPos pos = locateBlockPos(c, x,y,z);
final BlockPos pos = locateBlockPos(c, x, y, z);
return new BlockValue(null, c.level(), pos);
}
private static final Map<String, BlockValue> bvCache= new HashMap<>();
public static BlockValue fromString(String str, Level level)
private static final Map<String, BlockValue> bvCache = new HashMap<>();
public static BlockValue fromString(final String str, final Level level)
{
try
{
BlockValue bv = bvCache.get(str); // [SCARY SHIT] persistent caches over server reloads
if (bv != null) return bv;
BlockStateParser.BlockResult foo = BlockStateParser.parseForBlock(level.registryAccess().lookupOrThrow(Registries.BLOCK), new StringReader(str), true );
if (bv != null)
{
return bv;
}
final BlockStateParser.BlockResult foo = BlockStateParser.parseForBlock(level.registryAccess().lookupOrThrow(Registries.BLOCK), new StringReader(str), true);
if (foo.blockState() != null)
{
CompoundTag bd = foo.nbt();
if (bd == null)
{
bd = new CompoundTag();
bv = new BlockValue(foo.blockState(), level, null, bd );
if (bvCache.size()>10000)
}
bv = new BlockValue(foo.blockState(), level, null, bd);
if (bvCache.size() > 10000)
{
bvCache.clear();
}
bvCache.put(str, bv);
return bv;
}
}
catch (CommandSyntaxException ignored)
catch (final CommandSyntaxException ignored)
{
}
throw new ThrowStatement(str, Throwables.UNKNOWN_BLOCK);
}
public static BlockPos locateBlockPos(CarpetContext c, int xpos, int ypos, int zpos)
public static BlockPos locateBlockPos(final CarpetContext c, final int xpos, final int ypos, final int zpos)
{
final BlockPos pos = c.origin();
return new BlockPos(pos.getX() + xpos, pos.getY() + ypos, pos.getZ() + zpos);
@@ -99,13 +110,13 @@ public class BlockValue extends Value
throw new InternalExpressionException("Attempted to fetch block state without world or stored block state");
}
public static BlockEntity getBlockEntity(Level level, BlockPos pos)
public static BlockEntity getBlockEntity(final Level level, final BlockPos pos)
{
if (level instanceof ServerLevel serverLevel) {
if (serverLevel.getServer().isSameThread())
return serverLevel.getBlockEntity(pos);
else
return serverLevel.getChunkAt(pos).getBlockEntity(pos, LevelChunk.EntityCreationType.IMMEDIATE);
if (level instanceof final ServerLevel serverLevel)
{
return serverLevel.getServer().isSameThread()
? serverLevel.getBlockEntity(pos)
: serverLevel.getChunkAt(pos).getBlockEntity(pos, LevelChunk.EntityCreationType.IMMEDIATE);
}
return null;
}
@@ -115,13 +126,11 @@ public class BlockValue extends Value
{
if (data != null)
{
if (data.isEmpty())
return null;
return data;
return data.isEmpty() ? null : data;
}
if (pos != null)
{
BlockEntity be = getBlockEntity(world, pos);
final BlockEntity be = getBlockEntity(world, pos);
if (be == null)
{
data = new CompoundTag();
@@ -134,7 +143,7 @@ public class BlockValue extends Value
}
public BlockValue(BlockState state, @NotNull Level world, BlockPos position)
public BlockValue(final BlockState state, @NotNull final Level world, final BlockPos position)
{
this.world = world;
blockState = state;
@@ -142,7 +151,7 @@ public class BlockValue extends Value
data = null;
}
public BlockValue(BlockState state, @NotNull Level world, BlockPos position, CompoundTag nbt)
public BlockValue(final BlockState state, @NotNull final Level world, final BlockPos position, final CompoundTag nbt)
{
this.world = world;
blockState = state;
@@ -154,7 +163,7 @@ public class BlockValue extends Value
@Override
public String getString()
{
Registry<Block> blockRegistry = world.registryAccess().registryOrThrow(Registries.BLOCK);
final Registry<Block> blockRegistry = world.registryAccess().registryOrThrow(Registries.BLOCK);
return nameFromRegistryId(blockRegistry.getKey(getBlockState().getBlock()));
}
@@ -179,9 +188,9 @@ public class BlockValue extends Value
@Override
public int hashCode()
{
if (pos != null )
return GlobalPos.of(world.dimension() , pos).hashCode(); //getDimension().getType()
return ("b"+getString()).hashCode();
return pos != null
? GlobalPos.of(world.dimension(), pos).hashCode()
: ("b" + getString()).hashCode();
}
public BlockPos getPos()
@@ -189,29 +198,35 @@ public class BlockValue extends Value
return pos;
}
public Level getWorld() { return world;}
public Level getWorld()
{
return world;
}
@Override
public Tag toTag(boolean force)
public Tag toTag(final boolean force)
{
if (!force) throw new NBTSerializableValue.IncompatibleTypeException(this);
if (!force)
{
throw new NBTSerializableValue.IncompatibleTypeException(this);
}
// follows falling block convertion
CompoundTag tag = new CompoundTag();
CompoundTag state = new CompoundTag();
BlockState s = getBlockState();
final CompoundTag tag = new CompoundTag();
final CompoundTag state = new CompoundTag();
final BlockState s = getBlockState();
state.put("Name", StringTag.valueOf(world.registryAccess().registryOrThrow(Registries.BLOCK).getKey(s.getBlock()).toString()));
Collection<Property<?>> properties = s.getProperties();
final Collection<Property<?>> properties = s.getProperties();
if (!properties.isEmpty())
{
CompoundTag props = new CompoundTag();
for (Property<?> p: properties)
final CompoundTag props = new CompoundTag();
for (final Property<?> p : properties)
{
props.put(p.getName(), StringTag.valueOf(s.getValue(p).toString().toLowerCase(Locale.ROOT)));
}
state.put("Properties", props);
}
tag.put("BlockState", state);
CompoundTag dataTag = getData();
final CompoundTag dataTag = getData();
if (dataTag != null)
{
tag.put("TileEntityData", dataTag);
@@ -219,29 +234,30 @@ public class BlockValue extends Value
return tag;
}
public enum SpecificDirection {
UP("up",0.5, 0.0, 0.5, Direction.UP),
public enum SpecificDirection
{
UP("up", 0.5, 0.0, 0.5, Direction.UP),
UPNORTH ("up-north", 0.5, 0.0, 0.4, Direction.UP),
UPSOUTH ("up-south", 0.5, 0.0, 0.6, Direction.UP),
UPNORTH("up-north", 0.5, 0.0, 0.4, Direction.UP),
UPSOUTH("up-south", 0.5, 0.0, 0.6, Direction.UP),
UPEAST("up-east", 0.6, 0.0, 0.5, Direction.UP),
UPWEST("up-west", 0.4, 0.0, 0.5, Direction.UP),
DOWN("down", 0.5, 1.0, 0.5, Direction.DOWN),
DOWNNORTH ("down-north", 0.5, 1.0, 0.4, Direction.DOWN),
DOWNSOUTH ("down-south", 0.5, 1.0, 0.6, Direction.DOWN),
DOWNNORTH("down-north", 0.5, 1.0, 0.4, Direction.DOWN),
DOWNSOUTH("down-south", 0.5, 1.0, 0.6, Direction.DOWN),
DOWNEAST("down-east", 0.6, 1.0, 0.5, Direction.DOWN),
DOWNWEST("down-west", 0.4, 1.0, 0.5, Direction.DOWN),
NORTH ("north", 0.5, 0.4, 1.0, Direction.NORTH),
SOUTH ("south", 0.5, 0.4, 0.0, Direction.SOUTH),
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 ("north-up", 0.5, 0.6, 1.0, Direction.NORTH),
SOUTHUP ("south-up", 0.5, 0.6, 0.0, Direction.SOUTH),
NORTHUP("north-up", 0.5, 0.6, 1.0, Direction.NORTH),
SOUTHUP("south-up", 0.5, 0.6, 0.0, Direction.SOUTH),
EASTUP("east-up", 0.0, 0.6, 0.5, Direction.EAST),
WESTUP("west-up", 1.0, 0.6, 0.5, Direction.WEST);
@@ -252,87 +268,94 @@ public class BlockValue extends Value
private static final Map<String, SpecificDirection> DIRECTION_MAP = Arrays.stream(values()).collect(Collectors.toMap(SpecificDirection::getName, d -> d));
SpecificDirection(String name, double hitx, double hity, double hitz, Direction blockFacing)
SpecificDirection(final String name, final double hitx, final double hity, final double hitz, final Direction blockFacing)
{
this.name = name;
this.hitOffset = new Vec3(hitx, hity, hitz);
this.facing = blockFacing;
}
private String getName()
{
return name;
}
}
public static class PlacementContext extends BlockPlaceContext {
public static class PlacementContext extends BlockPlaceContext
{
private final Direction facing;
private final boolean sneakPlace;
public static PlacementContext from(Level world, BlockPos pos, String direction, boolean sneakPlace, ItemStack itemStack)
public static PlacementContext from(final Level world, final BlockPos pos, final String direction, final boolean sneakPlace, final ItemStack itemStack)
{
SpecificDirection dir = SpecificDirection.DIRECTION_MAP.get(direction);
final SpecificDirection dir = SpecificDirection.DIRECTION_MAP.get(direction);
if (dir == null)
throw new InternalExpressionException("unknown block placement direction: "+direction);
BlockHitResult hitres = new BlockHitResult(Vec3.atLowerCornerOf(pos).add(dir.hitOffset), dir.facing, pos, false);
{
throw new InternalExpressionException("unknown block placement direction: " + direction);
}
final BlockHitResult hitres = new BlockHitResult(Vec3.atLowerCornerOf(pos).add(dir.hitOffset), dir.facing, pos, false);
return new PlacementContext(world, dir.facing, sneakPlace, itemStack, hitres);
}
private PlacementContext(Level world_1, Direction direction_1, boolean sneakPlace, ItemStack itemStack_1, BlockHitResult hitres) {
private PlacementContext(final Level world_1, final Direction direction_1, final boolean sneakPlace, final ItemStack itemStack_1, final BlockHitResult hitres)
{
super(world_1, null, InteractionHand.MAIN_HAND, itemStack_1, hitres);
this.facing = direction_1;
this.sneakPlace = sneakPlace;
}
@Override
public BlockPos getClickedPos() {
boolean prevcanReplaceExisting = replaceClicked;
public BlockPos getClickedPos()
{
final boolean prevcanReplaceExisting = replaceClicked;
replaceClicked = true;
BlockPos ret = super.getClickedPos();
final BlockPos ret = super.getClickedPos();
replaceClicked = prevcanReplaceExisting;
return ret;
}
@Override
public Direction getNearestLookingDirection() {
public Direction getNearestLookingDirection()
{
return facing.getOpposite();
}
@Override
public Direction[] getNearestLookingDirections() {
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[] getNearestLookingDirections()
{
return switch (this.facing)
{
case DOWN -> new Direction[]{Direction.DOWN, Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST, Direction.UP};
case UP -> new Direction[]{Direction.DOWN, Direction.UP, Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST};
case NORTH -> new Direction[]{Direction.DOWN, Direction.NORTH, Direction.EAST, Direction.WEST, Direction.UP, Direction.SOUTH};
case SOUTH -> new Direction[]{Direction.DOWN, Direction.SOUTH, Direction.EAST, Direction.WEST, Direction.UP, Direction.NORTH};
case WEST -> new Direction[]{Direction.DOWN, Direction.WEST, Direction.SOUTH, Direction.UP, Direction.NORTH, Direction.EAST};
case EAST -> new Direction[]{Direction.DOWN, Direction.EAST, Direction.SOUTH, Direction.UP, Direction.NORTH, Direction.WEST};
};
}
@Override
public Direction getHorizontalDirection() {
public Direction getHorizontalDirection()
{
return this.facing.getAxis() == Direction.Axis.Y ? Direction.NORTH : this.facing;
}
@Override
public Direction getNearestLookingVerticalDirection() {
public Direction getNearestLookingVerticalDirection()
{
return facing.getAxis() == Axis.Y ? facing : Direction.UP;
}
@Override
public boolean isSecondaryUseActive() {
public boolean isSecondaryUseActive()
{
return sneakPlace;
}
@Override
public float getRotation() {
return (float)(this.facing.get2DDataValue() * 90);
public float getRotation()
{
return (float) (this.facing.get2DDataValue() * 90);
}
}
}