add chaining, fix particles, clean up logging

This commit is contained in:
QuImUfu 2023-05-30 00:08:21 +02:00
parent 086cddfc5e
commit 580d46a36c
11 changed files with 178 additions and 55 deletions

View File

@ -38,14 +38,12 @@ import quimufu.colourful_portals.portal_fluid.PortalFluidBucketItem;
import java.util.HashSet;
public class ColourfulPortalsMod implements ModInitializer, ClientModInitializer {
// This logger is used to write text to the console and the log file.
// It is considered best practice to use your mod id as the logger's name.
// That way, it's clear which mod wrote info, warnings, and errors.
public static final Logger LOGGER = LoggerFactory.getLogger("colourful_portals");
public static final String MOD_ID = "colourful_portals";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final HashSet<Identifier> PORTAL_BLOCKS = new HashSet<>();
public static final PortalBlock PORTAL_BLOCK = new PortalBlock(FabricBlockSettings.of(Material.GLASS).strength(-1.0f, 3600000.8f).dropsNothing().nonOpaque().luminance(15).allowsSpawning(ColourfulPortalsMod::never).solidBlock(ColourfulPortalsMod::never).suffocates(ColourfulPortalsMod::never).blockVision(ColourfulPortalsMod::never));
public static final PortalBlock PORTAL_BLOCK = new PortalBlock(FabricBlockSettings.of(Material.GLASS).strength(-1.0f, 3600000.8f).dropsNothing().nonOpaque().luminance(15).allowsSpawning(ColourfulPortalsMod::never).solidBlock(ColourfulPortalsMod::never).suffocates(ColourfulPortalsMod::never).blockVision(ColourfulPortalsMod::never).ticksRandomly());
public static final BlockItem PORTAL_BLOCK_ITEM = new BlockItem(PORTAL_BLOCK, new FabricItemSettings().rarity(Rarity.EPIC));
public static final Item BLOB_DARK = new Item(new FabricItemSettings());
public static final Item BLOB_BRIGHT = new Item(new FabricItemSettings());
@ -64,10 +62,7 @@ public class ColourfulPortalsMod implements ModInitializer, ClientModInitializer
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
LOGGER.info("Hello Fabric world!");
LOGGER.info("Colourizing Portals...");
for (String id : ColourfulPortalConfig.portalBlocks.keySet()) {
PORTAL_BLOCKS.add(Identifier.tryParse(id));
@ -91,6 +86,7 @@ public class ColourfulPortalsMod implements ModInitializer, ClientModInitializer
ItemGroupEvents.modifyEntriesEvent(ItemGroups.TOOLS)
.register(ColourfulPortalsMod::addTtTools);
LOGGER.info("Portals Colourful!");
}
private static void addTtTools(FabricItemGroupEntries entries) {

View File

@ -2,9 +2,13 @@ package quimufu.colourful_portals;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.FluidFillable;
import net.minecraft.block.ShapeContext;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.Items;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.EnumProperty;
import net.minecraft.state.property.Properties;
@ -15,9 +19,12 @@ import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import quimufu.colourful_portals.portal.PortalManager;
public class PortalBlock extends Block {
public class PortalBlock extends Block implements FluidFillable {
public static final EnumProperty<Direction.Axis> AXIS = Properties.AXIS;
public static final EnumProperty<DyeColor> DYE_COLOR = EnumProperty.of("colour", DyeColor.class);;
@ -56,7 +63,8 @@ public class PortalBlock extends Block {
BlockState state, BlockView world, BlockPos blockPos, ShapeContext shapeContext
) {
if(shapeContext.isHolding(Items.DEBUG_STICK)
|| shapeContext.isHolding(ColourfulPortalsMod.PORTAL_BLOCK_ITEM)){
|| shapeContext.isHolding(ColourfulPortalsMod.PORTAL_BLOCK_ITEM)
|| shapeContext.isHolding(ColourfulPortalsMod.PORTAL_FLUID_BUCKET_ITEM)){
return switch (state.get(AXIS)) {
case Z -> Z_AABB;
case Y -> Y_AABB;
@ -112,4 +120,23 @@ public class PortalBlock extends Block {
return true;
}
@Override
public boolean canFillWithFluid(BlockView world, BlockPos pos, BlockState state, Fluid fluid) {
if(((World)world).isClient() && fluid == ColourfulPortalsMod.PORTAL_FLUID){
return true;
}
return fluid == ColourfulPortalsMod.PORTAL_FLUID
&& (world instanceof ServerWorld)
&& PortalManager.canExtend((ServerWorld) world, pos);
}
@Override
public boolean tryFillWithFluid(WorldAccess world, BlockPos pos, BlockState state, FluidState fluidState) {
if(((World)world).isClient() && fluidState.isOf(ColourfulPortalsMod.PORTAL_FLUID)){
return true;
}
return fluidState.isOf(ColourfulPortalsMod.PORTAL_FLUID)
&& (world instanceof ServerWorld)
&& PortalManager.extend((ServerWorld) world, pos);
}
}

View File

@ -21,10 +21,10 @@ public class PortalFluidRenderHandler implements FluidRenderHandler, CommonPorta
private final CommonPortalFluidRenderer commonPortalFluidRenderer = new CommonPortalFluidRenderer();
private Sprite sprite = null;
private VertexConsumer vertexConsumer;
private int i;
private final ThreadLocal<VertexConsumer> vertexConsumer = new ThreadLocal<>();
private final ThreadLocal<Integer> i = new ThreadLocal<>();
private final float[][] vertices = new float[4][5];
private final ThreadLocal<float[][]> vertices = new ThreadLocal<>();
@Override
public Sprite[] getFluidSprites(@Nullable BlockRenderView view, @Nullable BlockPos pos, FluidState state) {
@ -38,12 +38,12 @@ public class PortalFluidRenderHandler implements FluidRenderHandler, CommonPorta
@Override
public void renderFluid(BlockPos pos, BlockRenderView world, VertexConsumer vertexConsumer, BlockState blockState, FluidState fluidState) {
this.vertexConsumer = vertexConsumer;
this.vertexConsumer.set(vertexConsumer);
BlockPos offset = new BlockPos(pos.getX() & 0xF, pos.getY() & 0xF, pos.getZ() & 0xF);
if (vertices.get() == null) {
vertices.set(new float[4][5]);
}
commonPortalFluidRenderer.render(world, fluidState, pos, offset, this);
}
@ -60,12 +60,14 @@ public class PortalFluidRenderHandler implements FluidRenderHandler, CommonPorta
@Override
public void eatVertex(float x, float y, float z, float u, float v) {
vertices[i][0] = x;
vertices[i][1] = y;
vertices[i][2] = z;
vertices[i][3] = u;
vertices[i][4] = v;
i = (i + 1) % 4;
float[][] floats = vertices.get();
int idx = i.get() == null ? 0 : i.get();
floats[idx][0] = x;
floats[idx][1] = y;
floats[idx][2] = z;
floats[idx][3] = u;
floats[idx][4] = v;
i.set((idx + 1) % 4);
}
@Override
@ -73,9 +75,9 @@ public class PortalFluidRenderHandler implements FluidRenderHandler, CommonPorta
float offX = offset.getX();
float offY = offset.getY();
float offZ = offset.getZ();
for (float[] v : vertices) {
for (float[] v : vertices.get()) {
vertexConsumer
vertexConsumer.get()
.vertex(offX + v[0], offY + v[1], offZ + v[2])
.color(1f, 1f, 1f, 1.0f)
.texture(v[3], v[4])

View File

@ -26,16 +26,14 @@ public class BlockChangeMixin {
// This code is injected into the end of ServerWorld.onBlockChanged()V
if (oldBlock.getBlock() != newBlock.getBlock()) {
if (PORTAL_BLOCKS.contains(Registries.BLOCK.getId(newBlock.getBlock()))) {
LOGGER.info("onBlockNew {} -> {}", oldBlock, newBlock);
LOGGER.debug("onBlockNew {} -> {}", oldBlock, newBlock);
Identifier blockId = Registries.BLOCK.getId(newBlock.getBlock());
ColourfulPortalsMod.LOGGER.info("Block change, new portal block: " + blockId);
PortalManager.onPortalBlockPlaced(world, pos, blockId);
}
if (PORTAL_BLOCKS.contains(Registries.BLOCK.getId(oldBlock.getBlock()))) {
LOGGER.info("onBlockOld {} -> {}", oldBlock, newBlock);
LOGGER.debug("onBlockOld {} -> {}", oldBlock, newBlock);
Identifier blockId = Registries.BLOCK.getId(oldBlock.getBlock());
ColourfulPortalsMod.LOGGER.info("Block change, removed portal block: " + blockId);
PortalManager.onPortalBlockBroken(world, pos, blockId);
}
}

View File

@ -34,16 +34,16 @@ public class PortalHelper {
}
static boolean isValidPortal(ServerWorld world, BlockBox portal, Identifier blockId) {
return isValidPortal(world, portal, blockId, true);
return isValidPortal(world, portal, blockId, true, false);
}
static boolean isValidCandidate(ServerWorld world, BlockBox portal, Identifier blockId) {
return isValidPortal(world, portal, blockId, false);
return isValidPortal(world, portal, blockId, false, false);
}
private static boolean isValidPortal(ServerWorld world, BlockBox portal, Identifier blockId, boolean empty) {
private static boolean isValidPortal(ServerWorld world, BlockBox portal, Identifier blockId, boolean empty, boolean placementCheck) {
Direction axisW = getAxisW(portal);
return isValidPortal(world, Registries.BLOCK.get(blockId), new BlockPos(portal.getMinX(), portal.getMinY(), portal.getMinZ()), empty, axisW);
return isValidPortal(world, Registries.BLOCK.get(blockId), new BlockPos(portal.getMinX(), portal.getMinY(), portal.getMinZ()), empty, axisW, placementCheck);
}
static Direction getAxisW(BlockBox fromPortalBox) {
@ -167,13 +167,17 @@ public class PortalHelper {
private static Optional<BlockBox> getPortalStartingAt(ServerWorld world, Block block, BlockPos lowerCorner, Direction direction) {
Vec3i sidewardsOffset = direction.getVector().multiply(3);
Vec3i upwardsOffset = Direction.UP.getVector().multiply(4);
if (isValidPortal(world, block, lowerCorner, false, direction)) {
if (isValidPortal(world, block, lowerCorner, false, direction, false)) {
return Optional.of(BlockBox.create(lowerCorner, lowerCorner.add(sidewardsOffset).add(upwardsOffset)));
}
return Optional.empty();
}
private static boolean isValidPortal(ServerWorld world, Block block, BlockPos lowerCorner, boolean hasToBeFilledCorrectly, Direction direction) {
public static boolean isPortalPlaceable(ServerWorld world, BlockBox portal, Identifier blockId) {
return isValidPortal(world, portal, blockId, false, true);
}
private static boolean isValidPortal(ServerWorld world, Block block, BlockPos lowerCorner, boolean hasToBeFilledCorrectly, Direction direction, boolean placementCheck) {
//maybe use BlockPosIterator, but make sure Performance is OK
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 4; j++) {
@ -194,6 +198,9 @@ public class PortalHelper {
if (hasToBeFilledCorrectly && !(blockState.getFluidState().isOf(PORTAL_FLUID) || blockState.getBlock().equals(PORTAL_BLOCK))) {
return false;
}
if(placementCheck && !(blockState.isReplaceable() || blockState.getFluidState().isOf(PORTAL_FLUID) || blockState.getBlock().equals(PORTAL_BLOCK))){
return false;
}
if (blockState.isOf(block)) {
return false;
}
@ -205,6 +212,7 @@ public class PortalHelper {
return true;
}
private static class BlockPosIterator implements Iterator<BlockPos> {
private final BlockPos startPos;
private final Vec3i dir;

View File

@ -70,7 +70,7 @@ public class PortalListComponent implements Component {
}
}
LOGGER.info("portals {}", tag.toString());
LOGGER.debug("portals {}", tag.toString());
}
@ -120,4 +120,9 @@ public class PortalListComponent implements Component {
List<Pair<BlockBox, Identifier>> portals = portalsPerPortalBlock.computeIfAbsent(blockId, (i) -> new ArrayList<>());
return portals.contains(portal);
}
public Pair<BlockBox, Identifier> getLast(Identifier blockId) {
List<Pair<BlockBox, Identifier>> portals = portalsPerPortalBlock.computeIfAbsent(blockId, (i) -> new ArrayList<>());
return portals.get(portals.size()-1);
}
}

View File

@ -161,6 +161,9 @@ public class PortalManager {
BlockState blockState = portalWorld.getBlockState(blockPos);
if (blockState.isAir() || blockState.getFluidState().isOf(PORTAL_FLUID)) {
portalWorld.setBlockState(blockPos, portalBlockState);
} else if (blockState.isReplaceable()) {
portalWorld.breakBlock(blockPos,true);
portalWorld.setBlockState(blockPos, portalBlockState);
}
});
}
@ -254,14 +257,75 @@ public class PortalManager {
List<BlockBox> portalCandidates = portalCandidateList.getContainingPortals(blockId, pos, dim);
for (BlockBox portalCandidate : portalCandidates) {
if (PortalHelper.isValidPortal(world, portalCandidate, blockId)) {
Pair<BlockBox, Identifier> next = portalCandidateList.getNext(blockId, portalCandidate, dim);
Pair<BlockBox, Identifier> current = Pair.of(portalCandidate, dim);
Pair<BlockBox, Identifier> next = portalCandidateList.getNext(blockId, portalCandidate, dim);
ServerWorld nextWorld = world.getServer().getWorld(RegistryKey.of(RegistryKeys.WORLD, next.second));
if (!next.equals(current)
&& (!portalList.containsPortal(blockId, current) || !portalList.containsPortal(blockId, next))) {
&& (!portalList.containsPortal(blockId, current) || !portalList.containsPortal(blockId, next))
&& PortalHelper.isPortalPlaceable(nextWorld, next.first, blockId)) {
portalList.createPortal(blockId, current);
portalList.createPortal(blockId, next);
ret = true;
}
fixPortalGroup(blockId, portalList, world.getServer());
}
}
}
world.getProfiler().pop();
return ret;
}
public static boolean canExtend(ServerWorld world, BlockPos pos) {
world.getProfiler().push("portal extension check");
PortalListComponent portalCandidateList = PORTAL_CANDIDATE_LIST.get(world.getLevelProperties());
PortalListComponent portalList = PORTAL_LIST.get(world.getLevelProperties());
Set<Identifier> blockIds = portalList.getBlockIds();
Identifier dim = world.getDimensionKey().getValue();
for (Identifier blockId : blockIds) {
List<BlockBox> portals = portalList.getContainingPortals(blockId, pos, dim);
for (BlockBox portal : portals) {
if (PortalHelper.isValidPortal(world, portal, blockId)) {
Pair<BlockBox, Identifier> last = portalList.getLast(blockId);
Pair<BlockBox, Identifier> next = portalCandidateList.getNext(blockId, last.first, last.second);
ServerWorld nextWorld = world.getServer().getWorld(RegistryKey.of(RegistryKeys.WORLD, next.second));
if(!portalList.containsPortal(blockId, next)
&& PortalHelper.isValidPortal(world, portal, blockId)
&& PortalHelper.isPortalPlaceable(nextWorld, next.first, blockId)){
world.getProfiler().pop();
return true;
}
}
}
}
world.getProfiler().pop();
return false;
}
public static boolean extend(ServerWorld world, BlockPos pos) {
world.getProfiler().push("portal extension");
PortalListComponent portalCandidateList = PORTAL_CANDIDATE_LIST.get(world.getLevelProperties());
PortalListComponent portalList = PORTAL_LIST.get(world.getLevelProperties());
Set<Identifier> blockIds = portalList.getBlockIds();
Identifier dim = world.getDimensionKey().getValue();
boolean ret = false;
for (Identifier blockId : blockIds) {
List<BlockBox> portals = portalList.getContainingPortals(blockId, pos, dim);
for (BlockBox portal : portals) {
if (PortalHelper.isValidPortal(world, portal, blockId)) {
Pair<BlockBox, Identifier> last = portalList.getLast(blockId);
Pair<BlockBox, Identifier> next = portalCandidateList.getNext(blockId, last.first, last.second);
ServerWorld nextWorld = world.getServer().getWorld(RegistryKey.of(RegistryKeys.WORLD, next.second));
if(!portalList.containsPortal(blockId, next)
&& PortalHelper.isValidPortal(world, portal, blockId)
&& PortalHelper.isPortalPlaceable(nextWorld, next.first, blockId)){
portalList.createPortal(blockId, next);
ret = true;
}
fixPortalGroup(blockId, portalList, world.getServer());
}

View File

@ -63,7 +63,7 @@ public class PortalFluid extends Fluid {
@Override
protected void onRandomTick(World world, BlockPos pos, FluidState state, Random random) {
if (!world.isClient && !world.getFluidTickScheduler().isQueued(pos, this)) {
//world.scheduleFluidTick(pos, this, this.getTickRate(world));
world.scheduleFluidTick(pos, this, this.getTickRate(world));
}
}
@ -137,7 +137,7 @@ public class PortalFluid extends Fluid {
newStateMe = state.with(AXIS, NullableAxis.of(currentAxis));
}
if (amount == 0 || (amount <= 14 && targetDir != null)) {
if (amount <= 14) {
//*might* change my state in world, returned value is what new state should be
newStateMe = steal(world, pos, newStateMe, currentAxis);
}
@ -147,7 +147,6 @@ public class PortalFluid extends Fluid {
if (amount == 0) {
//we couldn't steal anything to keep ourselves alive. This ?should? not happen.
world.setBlockState(pos, Blocks.AIR.getDefaultState());
world.updateNeighborsAlways(pos, ColourfulPortalsMod.PORTAL_FLUID_BLOCk);
world.getProfiler().pop();
return;
}
@ -177,14 +176,15 @@ public class PortalFluid extends Fluid {
}
if (world.getBlockState(pos).getFluidState() != newStateMe) {
FluidState actual = world.getBlockState(pos).getFluidState();
BlockState blockState = newStateMe.getBlockState();
if (actual != newStateMe) {
world.setBlockState(pos, blockState);
world.updateNeighborsAlways(pos, blockState.getBlock());
world.scheduleFluidTick(pos, this, getTickRate(world));
} else if (world.getBlockState(pos).getFluidState() != state) {
//we changed, so we schedule an update for ourselves
world.scheduleFluidTick(pos, this, getTickRate(world));
} else if (actual != state && Math.abs(state.get(AMOUNT) - actual.get(AMOUNT)) > 1) {
//we changed by more than one, informing Neighbors, so they can steal as well
world.updateNeighborsAlways(pos, blockState.getBlock());
}
world.getProfiler().pop();
}
@ -507,10 +507,11 @@ public class PortalFluid extends Fluid {
if (!world.isClient()) {
for (Map.Entry<BlockPos, FluidState> update : updated.entrySet()) {
world.setBlockState(update.getKey(), update.getValue().getBlockState(), Block.NOTIFY_LISTENERS);
if (!world.getFluidTickScheduler().isQueued(update.getKey(), this)) {
world.scheduleFluidTick(update.getKey(), this, this.getTickRate(world));
}
world.updateNeighbors(update.getKey(), update.getValue().getBlockState().getBlock());
}
}
return true;

View File

@ -19,6 +19,7 @@ import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
import quimufu.colourful_portals.ColourfulPortalsMod;
import java.util.Collections;
import java.util.List;
@ -58,14 +59,14 @@ public class PortalFluidBlock
@Override
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
if (!world.getFluidTickScheduler().isQueued(pos, fluid)) {
if (oldState.getFluidState().getFluid() != fluid && !world.getFluidTickScheduler().isQueued(pos, fluid)) {
world.scheduleFluidTick(pos, fluid, this.fluid.getTickRate(world));
}
}
@Override
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
if (!world.getFluidTickScheduler().isQueued(pos, fluid)) {
if (!neighborState.getFluidState().isOf(fluid) && !world.getFluidTickScheduler().isQueued(pos, fluid)) {
world.scheduleFluidTick(pos, fluid, this.fluid.getTickRate(world));
}
return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);

View File

@ -30,13 +30,31 @@ public class PortalFluidBucketItem extends BucketItem {
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
BlockHitResult blockHitResult = BucketItem.raycast(world, user, RaycastContext.FluidHandling.ANY);
BlockHitResult blockHitResult = BucketItem.raycast(world, user, RaycastContext.FluidHandling.NONE);
ItemStack itemStack = user.getStackInHand(hand);
if (blockHitResult.getType() == HitResult.Type.BLOCK) {
BlockPos pos = blockHitResult.getBlockPos();
if (world.getBlockState(pos).getFluidState().isOf(fluid)) {
placeFluid(user, world, pos, blockHitResult);
BlockState blockState = world.getBlockState(pos);
Block block = blockState.getBlock();
if (block instanceof FluidFillable && ((FluidFillable) block).canFillWithFluid(world, pos, blockState, fluid)) {
if (placeFluid(user, world, pos, blockHitResult)) {
return TypedActionResult.success(BucketItem.getEmptiedStack(itemStack, user), world.isClient());
}
}
}
blockHitResult = BucketItem.raycast(world, user, RaycastContext.FluidHandling.ANY);
itemStack = user.getStackInHand(hand);
if (blockHitResult.getType() == HitResult.Type.BLOCK) {
BlockPos pos = blockHitResult.getBlockPos();
BlockState blockState = world.getBlockState(pos);
if (blockState.getFluidState().isOf(fluid)) {
if (placeFluid(user, world, pos, blockHitResult)) {
return TypedActionResult.success(BucketItem.getEmptiedStack(itemStack, user), world.isClient());
}
}
}
return super.use(world, user, hand);
}

View File

@ -13,5 +13,8 @@
}
}
}
]
],
"textures": {
"particle": "#pane"
}
}