-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
191 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
src/main/java/com/ldtteam/common/fakelevel/SingleBlockFakeLevel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
110 changes: 0 additions & 110 deletions
110
src/main/java/com/ldtteam/common/fakelevel/SingleBlockFakeLevelGetter.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.