Skip to content

Commit

Permalink
Extend single block FakeLevel (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightenom authored Sep 29, 2024
1 parent a065a4f commit 91ffe66
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 88 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/ldtteam/blockui/BOGuiGraphics.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.client.renderer.MultiBufferSource.BufferSource;
Expand Down Expand Up @@ -184,4 +185,9 @@ public void popMvPose()
RenderSystem.getModelViewStack().popMatrix();
RenderSystem.applyModelViewMatrix();
}

public static double getAltSpeedFactor()
{
return Screen.hasAltDown() ? 5 : 1;
}
}
17 changes: 0 additions & 17 deletions src/main/java/com/ldtteam/blockui/mod/ClientEventSubscriber.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.ldtteam.blockui.mod.container.ContainerHook;
import com.ldtteam.blockui.util.resloc.OutOfJarResourceLocation;
import com.ldtteam.blockui.views.BOWindow;
import com.ldtteam.common.util.BlockToItemHelper;
import com.mojang.blaze3d.platform.InputConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
Expand All @@ -22,7 +21,6 @@
import net.neoforged.bus.api.EventPriority;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModList;
import net.neoforged.neoforge.client.event.ClientPlayerNetworkEvent;
import net.neoforged.neoforge.client.event.ClientTickEvent;
import net.neoforged.neoforge.client.event.RenderGuiLayerEvent;
import net.neoforged.neoforge.client.event.InputEvent.MouseScrollingEvent;
Expand Down Expand Up @@ -175,19 +173,4 @@ public static void renderOverlay(final RenderGuiLayerEvent.Pre event)
event.setCanceled(true);
}
}

@SubscribeEvent
public static void onClientPlayerLoggingOut(final ClientPlayerNetworkEvent.LoggingOut event)
{
BlockToItemHelper.releaseFakeLevelInstance();
}

@SubscribeEvent
public static void onClientPlayerDimChange(final ClientPlayerNetworkEvent.Clone event)
{
if (event.getNewPlayer().level() != event.getOldPlayer().level())
{
BlockToItemHelper.releaseFakeLevelInstance();
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/com/ldtteam/blockui/views/ScrollingView.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ldtteam.blockui.views;

import com.ldtteam.blockui.BOGuiGraphics;
import com.ldtteam.blockui.Pane;
import com.ldtteam.blockui.PaneParams;
import com.ldtteam.blockui.controls.Scrollbar;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void setSize(final int w, final int h)
@Override
public boolean scrollInput(final double horizontalWheel, final double verticalWheel, final double mx, final double my)
{
return setScrollY(getScrollY() - verticalWheel);
return setScrollY(getScrollY() - verticalWheel * BOGuiGraphics.getAltSpeedFactor());
}

public ScrollingContainer getContainer()
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/ldtteam/blockui/views/ZoomDragView.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ public boolean onMouseDrag(final double startX, final double startY, final int s
final boolean childResult = super.onMouseDrag(startX, startY, speed, calcRelativeX(x), calcRelativeY(y));
if (!childResult && dragEnabled)
{
setScrollX(scrollX - x * dragFactor);
setScrollY(scrollY - y * dragFactor);
setScrollX(scrollX - x * dragFactor * BOGuiGraphics.getAltSpeedFactor());
setScrollY(scrollY - y * dragFactor * BOGuiGraphics.getAltSpeedFactor());
return true;
}
return childResult;
Expand All @@ -250,6 +250,7 @@ public boolean scrollInput(final double horizontalWheel, final double verticalWh
final double childY = my - y;
final double oldX = (childX + scrollX) / scale;
final double oldY = (childY + scrollY) / scale;
final double zoomFactor = this.zoomFactor * BOGuiGraphics.getAltSpeedFactor();
scale = verticalWheel < 0 ? scale / zoomFactor : scale * zoomFactor;

// try to round if around whole number (cuz of text texture)
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/ldtteam/common/fakelevel/FakeChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,19 @@ public class FakeChunk extends LevelChunk
{
private final FakeLevel<?> fakeLevel;

// section cache
int lastY;
LevelChunkSection lastSection = null;

public FakeChunk(final FakeLevel<?> worldIn, final int x, final int z)
{
super(worldIn, new ChunkPos(x, z));
this.fakeLevel = worldIn;

// set itself to cache
fakeLevel.lastX = x;
fakeLevel.lastZ = z;
fakeLevel.lastChunk = this;
}

// ========================================
Expand Down Expand Up @@ -220,6 +229,10 @@ public LevelChunkSection[] getSections()
@Override
public LevelChunkSection getSection(int yIdx)
{
if (lastY == yIdx && lastSection != null)
{
return lastSection;
}
return new FakeLevelChunkSection(this, yIdx);
}

Expand Down
23 changes: 18 additions & 5 deletions src/main/java/com/ldtteam/common/fakelevel/FakeLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ public class FakeLevel<SOURCE extends IFakeLevelBlockGetter> extends Level
*/
protected BlockPos worldPos = BlockPos.ZERO;

// chunk cache
int lastX, lastZ;
ChunkAccess lastChunk = null;

/**
* @param levelSource data source, also try to set block entities/entities collections
* @param lightProvider light source
Expand All @@ -131,7 +135,7 @@ public FakeLevel(final SOURCE levelSource,
realLevel.registryAccess(),
realLevel.dimensionTypeRegistration(),
realLevel.getProfilerSupplier(),
true,
realLevel.isClientSide(),
false,
0,
0);
Expand All @@ -158,6 +162,11 @@ public void setRealLevel(final Level realLevel)
return;
}

if (realLevel != null && realLevel.isClientSide != this.isClientSide)
{
throw new IllegalArgumentException("Received wrong sided realLevel - fakeLevel.isClientSide = " + this.isClientSide);
}

this.realLevel = realLevel;
((FakeLevelData) this.getLevelData()).vanillaLevelData = realLevel == null ? null : realLevel.getLevelData();
}
Expand Down Expand Up @@ -201,7 +210,7 @@ public BlockPos getWorldPos()

/**
* For better block entity handling in chunk methods. If set then {@link IFakeLevelBlockGetter#getBlockEntity(BlockPos)
* levelSource.getBlockEntity(BlockPos)} is not used
* levelSource.getBlockEntity(BlockPos)} is not used. Reset with empty collection
*
* @param blockEntities all block entities, should be data equivalent to levelSource
*/
Expand All @@ -211,11 +220,11 @@ public void setBlockEntities(final Map<BlockPos, BlockEntity> blockEntities)
}

/**
* @param entities all entities, their level should be this fake level instance
* @param entities all entities, their level should be this fake level instance. Reset with empty collection
*/
public void setEntities(final Collection<? extends Entity> entities)
{
levelEntityGetter = FakeLevelEntityGetterAdapter.ofEntities(entities);
levelEntityGetter = entities.isEmpty() ? FakeLevelEntityGetterAdapter.EMPTY : FakeLevelEntityGetterAdapter.ofEntities(entities);
}

// ========================================
Expand Down Expand Up @@ -296,6 +305,10 @@ public BlockState getBlockState(final BlockPos pos)
@Override
public ChunkAccess getChunk(int x, int z, ChunkStatus requiredStatus, boolean nonnull)
{
if (lastX == x && lastZ == z && lastChunk != null)
{
return lastChunk;
}
return nonnull || hasChunk(x, z) ? new FakeChunk(this, x, z) : null;
}

Expand Down Expand Up @@ -368,7 +381,7 @@ public boolean isInWorldBounds(final BlockPos pos)
@Override
public CrashReportCategory fillReportDetails(CrashReport report)
{
CrashReportCategory crashreportcategory = report.addCategory("Structurize fake level");
CrashReportCategory crashreportcategory = report.addCategory("BlockUI fake level");
levelSource.describeSelfInCrashReport(crashreportcategory);
return crashreportcategory;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public FakeLevelChunkSection(final FakeChunk fakeChunk, final int yIdx)
super(null, null);
this.fakeChunk = fakeChunk;
this.yIdx = yIdx;

// set itself to cache
fakeChunk.lastY = yIdx;
fakeChunk.lastSection = this;
}

private BlockPos formGlobalPos(int x, int y, int z)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.minecraft.CrashReportCategory;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
Expand Down Expand Up @@ -126,40 +125,4 @@ default AABB getAABB()
{
return new AABB(getMinX(), getMinBuildHeight(), getMinZ(), getMaxX(), getMaxBuildHeight(), getMaxZ());
}

public static class SingleBlockFakeLevelGetter implements IFakeLevelBlockGetter
{
public BlockState blockState = null;
public BlockEntity blockEntity = null;

@Override
public BlockEntity getBlockEntity(final BlockPos pos)
{
return blockEntity;
}

@Override
public BlockState getBlockState(final BlockPos pos)
{
return blockState;
}

@Override
public int getHeight()
{
return 1;
}

@Override
public int getSizeX()
{
return 1;
}

@Override
public int getSizeZ()
{
return 1;
}
}
}
Loading

0 comments on commit 91ffe66

Please sign in to comment.