Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Nightenom committed Sep 29, 2024
1 parent 1aa278d commit f7cebc0
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 156 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
6 changes: 4 additions & 2 deletions src/main/java/com/ldtteam/common/fakelevel/FakeChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ 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));
Expand Down Expand Up @@ -222,8 +226,6 @@ public LevelChunkSection[] getSections()
return new LevelChunkSection[0];
}

int lastY;
LevelChunkSection lastSection = null;
@Override
public LevelChunkSection getSection(int yIdx)
{
Expand Down
6 changes: 4 additions & 2 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 Down Expand Up @@ -293,8 +297,6 @@ public BlockState getBlockState(final BlockPos pos)
return levelSource.isPosInside(pos) ? levelSource.getBlockState(pos) : Blocks.AIR.defaultBlockState();
}

int lastX, lastZ;
ChunkAccess lastChunk = null;
@Override
public ChunkAccess getChunk(int x, int z, ChunkStatus requiredStatus, boolean nonnull)
{
Expand Down
158 changes: 158 additions & 0 deletions src/main/java/com/ldtteam/common/fakelevel/SingleBlockFakeLevel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package com.ldtteam.common.fakelevel;

import net.minecraft.CrashReportCategory;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.function.Consumer;
import java.util.function.Function;

/**
* Simple implementation of {@link IFakeLevelBlockGetter} mostly for usage in methods where {@link Level} is needed for virtual
* BE/entities etc.
*/
public class SingleBlockFakeLevel extends FakeLevel<SingleBlockFakeLevel.SingleBlockFakeLevelGetter>
{
/**
* Creates simple fakeLevel instance
*
* @param realLevel actual valid vanilla instance to provide eg. registries
*/
public SingleBlockFakeLevel(final Level realLevel)
{
super(new SingleBlockFakeLevelGetter(), IFakeLevelLightProvider.USE_CLIENT_LEVEL, realLevel, null, true);
}

/**
* Do not forget to unset to prevent potential memory leaks
*
* @param blockState related to blockEntity
* @param blockEntity related to blockState
* @param realLevel actual valid vanilla instance to provide eg. registries
* @see #unset(FakeLevel, BlockEntity)
* @see FakeLevel#setEntities(Collection) FakeLevel#setEntities(Collection) if you want to add entities, do not forget to reset
*/
public void prepare(final BlockState blockState, @Nullable final BlockEntity blockEntity, final Level realLevel)
{
getLevelSource().blockEntity = blockEntity;
getLevelSource().blockState = blockState;
setRealLevel(realLevel);

if (blockEntity != null)
{
blockEntity.setLevel(this);
}
}

/**
* @param blockEntity to unlink level if needed
* @see #prepare(FakeLevel, BlockState, BlockEntity, Level)
*/
public void unset(@Nullable final BlockEntity blockEntity)
{
getLevelSource().blockEntity = null;
getLevelSource().blockState = null;
setRealLevel(null);

if (blockEntity != null)
{
try
{
blockEntity.setLevel(null);
}
catch (final NullPointerException e)
{
// setLevel impls sometimes violates nullability of level field
}
}
}

/**
* See related methods for more information.
*
* @param blockState related to blockEntity
* @param blockEntity related to blockState
* @param realLevel actual valid vanilla instance to provide eg. registries
* @param action context action
* @see #prepare(FakeLevel, BlockState, BlockEntity, Level)
* @see #unset(FakeLevel, BlockEntity)
*/
public void withFakeLevelContext(final BlockState blockState,
@Nullable final BlockEntity blockEntity,
final Level realLevel,
final Consumer<Level> action)
{
prepare(blockState, blockEntity, realLevel);
action.accept(this);
unset(blockEntity);
}

/**
* See related methods for more information.
*
* @param blockState related to blockEntity
* @param blockEntity related to blockState
* @param realLevel actual valid vanilla instance to provide eg. registries
* @param action context action
* @see #prepare(FakeLevel, BlockState, BlockEntity, Level)
* @see #unset(FakeLevel, BlockEntity)
*/
public <T> T useFakeLevelContext(final BlockState blockState,
@Nullable final BlockEntity blockEntity,
final Level realLevel,
final Function<Level, T> action)
{
prepare(blockState, blockEntity, realLevel);
final T result = action.apply(this);
unset(blockEntity);
return result;
}

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;
}

@Override
public void describeSelfInCrashReport(final CrashReportCategory category)
{
category.setDetail("Single block", blockState::toString);
category.setDetail("Single block entity type",
() -> blockEntity == null ? null : BuiltInRegistries.BLOCK_ENTITY_TYPE.getKey(blockEntity.getType()).toString());
}
}
}

This file was deleted.

Loading

0 comments on commit f7cebc0

Please sign in to comment.