Portals fixes and optimization

This commit is contained in:
Aleksey 2020-10-25 18:27:01 +03:00
parent 886be5bbc6
commit b18c9ed96c
3 changed files with 45 additions and 66 deletions

View file

@ -32,7 +32,7 @@ import ru.betterend.util.PortalFrameHelper;
public class EndPortalBlock extends NetherPortalBlock implements IRenderTypeable {
public EndPortalBlock() {
super(FabricBlockSettings.copyOf(Blocks.NETHER_PORTAL).luminance(state -> {
super(FabricBlockSettings.copyOf(Blocks.NETHER_PORTAL).resistance(Blocks.BEDROCK.getBlastResistance()).luminance(state -> {
return 12;
}));
}

View file

@ -1,22 +1,26 @@
package ru.betterend.blocks;
import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import ru.betterend.blocks.basis.BlockBase;
import ru.betterend.registry.BlockRegistry;
import ru.betterend.util.BlocksHelper;
import ru.betterend.util.PortalFrameHelper;
public class RunedFlavolite extends BlockBase {
public static final BooleanProperty ACTIVATED = BooleanProperty.of("active");
public RunedFlavolite() {
super(FabricBlockSettings.copyOf(BlockRegistry.FLAVOLITE.polished).luminance(state -> {
super(FabricBlockSettings.copyOf(BlockRegistry.FLAVOLITE.polished).resistance(Blocks.OBSIDIAN.getBlastResistance()).luminance(state -> {
return state.get(ACTIVATED) ? 8 : 0;
}));
this.setDefaultState(stateManager.getDefaultState().with(ACTIVATED, false));
@ -29,17 +33,18 @@ public class RunedFlavolite extends BlockBase {
@Override
public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) {
super.onBreak(world, pos, state, player);
if(state.get(ACTIVATED)) {
for (BlockPos position : BlockPos.iterate(pos.add(-3, -4, -3), pos.add(3, 4, 3))) {
if (position.equals(pos)) continue;
BlockState posState = world.getBlockState(position);
if (posState.getBlock() instanceof RunedFlavolite) {
BlocksHelper.setWithoutUpdate(world, position, posState.with(ACTIVATED, false));
} else if (posState.isOf(BlockRegistry.END_PORTAL_BLOCK)) {
world.removeBlock(position, false);
}
BlockPos bottom = PortalFrameHelper.findBottomCorner((World) world, pos, this);
BlockPos top = PortalFrameHelper.findTopCorner((World) world, pos, this);
if (bottom == null || top == null) return;
for (BlockPos position : BlockPos.iterate(bottom, top)) {
if (position.equals(pos)) continue;
BlockState posState = world.getBlockState(position);
if (posState.getBlock() instanceof RunedFlavolite && posState.get(ACTIVATED)) {
BlocksHelper.setWithoutUpdate(world, position, posState.with(ACTIVATED, false));
} else if (posState.isOf(BlockRegistry.END_PORTAL_BLOCK)) {
world.removeBlock(position, false);
}
}
super.onBreak(world, pos, state, player);
}
}

View file

@ -8,6 +8,7 @@ import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import ru.betterend.registry.BlockRegistry;
import ru.betterend.registry.FeatureRegistry;
@ -22,34 +23,29 @@ public class PortalFrameHelper {
boolean valid = true;
int width = 1, height = 1;
Direction.Axis axis = Direction.Axis.X;
BlockPos checkPos = bottomCorner.up();
BlockPos.Mutable checkPos = bottomCorner.up().mutableCopy();
Direction moveDir = Direction.UP;
while(valid && !checkPos.equals(bottomCorner)) {
valid = world.getBlockState(checkPos).isOf(frameBlock);
if (!valid) {
checkPos = checkPos.move(moveDir.getOpposite());
switch(moveDir) {
case UP: {
checkPos = checkPos.down();
if (world.getBlockState(checkPos.east()).isOf(frameBlock)) {
checkPos = checkPos.east();
moveDir = Direction.EAST;
valid = true;
} else if (world.getBlockState(checkPos.south()).isOf(frameBlock)) {
axis = Direction.Axis.Z;
checkPos = checkPos.south();
moveDir = Direction.SOUTH;
valid = true;
}
break;
}
case DOWN: {
checkPos = checkPos.up();
if (world.getBlockState(checkPos.west()).isOf(frameBlock)) {
checkPos = checkPos.west();
moveDir = Direction.WEST;
valid = true;
} else if (world.getBlockState(checkPos.north()).isOf(frameBlock)) {
checkPos = checkPos.north();
moveDir = Direction.NORTH;
valid = true;
}
@ -57,9 +53,7 @@ public class PortalFrameHelper {
}
case SOUTH:
case EAST: {
checkPos = moveDir.equals(Direction.SOUTH) ? checkPos.north() : checkPos.west();
if (world.getBlockState(checkPos.down()).isOf(frameBlock)) {
checkPos = checkPos.down();
moveDir = Direction.DOWN;
valid = true;
}
@ -69,39 +63,17 @@ public class PortalFrameHelper {
return false;
}
if (!valid) return false;
checkPos = checkPos.move(moveDir);
} else {
switch(moveDir) {
case UP: {
height++;
checkPos = checkPos.up();
break;
}
case DOWN: {
checkPos = checkPos.down();
break;
}
case NORTH: {
checkPos = checkPos.north();
break;
}
case SOUTH: {
width++;
checkPos = checkPos.south();
break;
}
case EAST: {
width++;
checkPos = checkPos.east();
break;
}
case WEST: {
checkPos = checkPos.west();
break;
}
if (moveDir.equals(Direction.EAST) || moveDir.equals(Direction.SOUTH)) {
width++;
} else if (moveDir.equals(Direction.UP)) {
height++;
}
checkPos = checkPos.move(moveDir);
}
}
if (width < 4 || height < 5 || width > 40 || height > 40) return false;
if (width < 4 || height < 5 || width > 20 || height > 25) return false;
if (axis.equals(Direction.Axis.X)) {
if(!checkIsAreaEmpty(world, bottomCorner.add(1, 1, 0), topCorner.add(-1, -1, 0))) return false;
} else {
@ -124,27 +96,28 @@ public class PortalFrameHelper {
BlockState south = world.getBlockState(pos.south());
BlockState west = world.getBlockState(pos.west());
BlockState east = world.getBlockState(pos.east());
BlockPos.Mutable mutable = pos instanceof BlockPos.Mutable ? (BlockPos.Mutable) pos : pos.mutableCopy();
if (up.isOf(frameBlock) && !down.isOf(frameBlock)) {
if (south.isOf(frameBlock) || east.isOf(frameBlock)) {
return pos;
return pos.toImmutable();
} else if (west.isOf(frameBlock)) {
return findBottomCorner(world, pos.west(), frameBlock);
return findBottomCorner(world, mutable.move(Direction.WEST), frameBlock);
} else if (north.isOf(frameBlock)){
return findBottomCorner(world, pos.north(), frameBlock);
return findBottomCorner(world, mutable.move(Direction.NORTH), frameBlock);
}
return null;
} else if (down.isOf(frameBlock)) {
if (west.isOf(frameBlock)) {
return findBottomCorner(world, pos.west(), frameBlock);
return findBottomCorner(world, mutable.move(Direction.WEST), frameBlock);
} else if (north.isOf(frameBlock)) {
return findBottomCorner(world, pos.north(), frameBlock);
return findBottomCorner(world, mutable.move(Direction.NORTH), frameBlock);
} else {
return findBottomCorner(world, pos.down(), frameBlock);
return findBottomCorner(world, mutable.move(Direction.DOWN), frameBlock);
}
} else if (west.isOf(frameBlock)) {
return findBottomCorner(world, pos.west(), frameBlock);
return findBottomCorner(world, mutable.move(Direction.WEST), frameBlock);
} else if (north.isOf(frameBlock)) {
return findBottomCorner(world, pos.north(), frameBlock);
return findBottomCorner(world, mutable.move(Direction.NORTH), frameBlock);
}
return null;
}
@ -156,27 +129,28 @@ public class PortalFrameHelper {
BlockState south = world.getBlockState(pos.south());
BlockState west = world.getBlockState(pos.west());
BlockState east = world.getBlockState(pos.east());
BlockPos.Mutable mutable = pos instanceof BlockPos.Mutable ? (BlockPos.Mutable) pos : pos.mutableCopy();
if (!up.isOf(frameBlock) && down.isOf(frameBlock)) {
if (north.isOf(frameBlock) || west.isOf(frameBlock)) {
return pos;
return pos.toImmutable();
} else if (east.isOf(frameBlock)) {
return findTopCorner(world, pos.east(), frameBlock);
return findTopCorner(world, mutable.move(Direction.EAST), frameBlock);
} else if (south.isOf(frameBlock)){
return findTopCorner(world, pos.south(), frameBlock);
return findTopCorner(world, mutable.move(Direction.SOUTH), frameBlock);
}
return null;
} else if (up.isOf(frameBlock)) {
if (east.isOf(frameBlock)) {
return findTopCorner(world, pos.east(), frameBlock);
return findTopCorner(world, mutable.move(Direction.EAST), frameBlock);
} else if (south.isOf(frameBlock)){
return findTopCorner(world, pos.south(), frameBlock);
return findTopCorner(world, mutable.move(Direction.SOUTH), frameBlock);
} else {
return findTopCorner(world, pos.up(), frameBlock);
return findTopCorner(world, mutable.move(Direction.UP), frameBlock);
}
} else if (east.isOf(frameBlock)) {
return findTopCorner(world, pos.east(), frameBlock);
return findTopCorner(world, mutable.move(Direction.EAST), frameBlock);
} else if (south.isOf(frameBlock)){
return findTopCorner(world, pos.south(), frameBlock);
return findTopCorner(world, mutable.move(Direction.SOUTH), frameBlock);
}
return null;
}