Skip to content

Commit

Permalink
Update updateSuppressionBlock for 1.19 (gnembon#1401)
Browse files Browse the repository at this point in the history
* Changed Rule: `updateSuppressionBlock` to match new update suppression

* Make it easier to use

* Carpets mixin file will be the death of me

* Renamed `WorldInterface` to `LevelInterface`
Merged LevelAccessor with LevelInterface

* Fix issues with 22w14a & resolve issues

* Add `@Unique` to Level_tickMixin.java

* Naming, brackets, and description

* Return null on invalid

* Fix remaining refactoring issues
  • Loading branch information
FxMorin authored May 17, 2022
1 parent a60bc38 commit 9daf72b
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 66 deletions.
48 changes: 12 additions & 36 deletions src/main/java/carpet/CarpetSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public class CarpetSettings
public static boolean chainStoneStickToAll = false;
public static Block structureBlockIgnoredBlock = Blocks.STRUCTURE_VOID;
public static final int vanillaStructureBlockLimit = 48;
public static int updateSuppressionBlockSetting = -1;

private static class LanguageValidator extends Validator<String> {
@Override public String validate(CommandSourceStack source, ParsedRule<String> currentRule, String newValue, String string) {
Expand Down Expand Up @@ -1035,57 +1034,34 @@ public String validate(CommandSourceStack source, ParsedRule<String> currentRule
@Rule(
desc = "Lightning kills the items that drop when lightning kills an entity",
extra = {"Setting to true will prevent lightning from killing drops", "Fixes [MC-206922](https://bugs.mojang.com/browse/MC-206922)."},
category = {BUGFIX}
category = BUGFIX
)
public static boolean lightningKillsDropsFix = false;

@Rule(
desc = "Placing an activator rail on top of a barrier block will update suppress when the rail turns off.",
extra = {"Entering an integer will make the update suppression block auto-reset","Integer entered is the delay in ticks for it to reset"},
category = {CREATIVE, "extras"},
options = {"false","true","1","6"},
desc = "Placing an activator rail on top of a barrier block will fill the neighbor updater stack when the rail turns off.",
extra = {"The integer entered is the amount of updates that should be left in the stack", "-1 turns it off"},
category = CREATIVE,
options = {"-1","0","10","50"},
strict = false,
validate = updateSuppressionBlockModes.class
validate = UpdateSuppressionBlockModes.class
)
public static String updateSuppressionBlock = "false";
public static int updateSuppressionBlock = -1;

@Rule(
desc = "Fixes update suppression causing server crashes.",
category = {BUGFIX}
category = BUGFIX
)
public static boolean updateSuppressionCrashFix = false;

public static int getInteger(String s) {
try {
return Integer.parseInt(s);
} catch(NumberFormatException e) {
return -1;
}
}

private static class updateSuppressionBlockModes extends Validator<String> {
private static class UpdateSuppressionBlockModes extends Validator<Integer> {
@Override
public String validate(CommandSourceStack source, ParsedRule<String> currentRule, String newValue, String string) {
if (!currentRule.get().equals(newValue)) {
if (newValue.equalsIgnoreCase("false")) {
updateSuppressionBlockSetting = -1;
} else if (newValue.equalsIgnoreCase("true")) {
updateSuppressionBlockSetting = 0;
} else {
int parsedInt = getInteger(newValue);
if (parsedInt <= 0) {
updateSuppressionBlockSetting = -1;
return "false";
} else {
updateSuppressionBlockSetting = parsedInt;
}
}
}
return newValue;
public Integer validate(CommandSourceStack source, ParsedRule<Integer> currentRule, Integer newValue, String string) {
return newValue < -1 ? null : newValue;
}
@Override
public String description() {
return "Cannot be negative, can be true, false, or # > 0";
return "This value represents the amount of updates required before the logger logs them. Must be -1 or larger";
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package carpet.fakes;

import net.minecraft.world.level.redstone.NeighborUpdater;
import org.jetbrains.annotations.Nullable;

import java.util.List;
Expand All @@ -12,11 +13,13 @@
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;

public interface WorldInterface
public interface LevelInterface
{
Map<EntityType<?>, Entity> getPrecookedMobs();

boolean setBlockStateWithBlockEntity(BlockPos blockPos, BlockState blockState, BlockEntity newBlockEntity, int int1);

List<Entity> getOtherEntitiesLimited(@Nullable Entity except, AABB box, Predicate<? super Entity> predicate, int limit);

NeighborUpdater getNeighborUpdater();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.fakes.LevelInterface;
import net.minecraft.world.level.redstone.NeighborUpdater;
import net.minecraft.util.RandomSource;
import org.spongepowered.asm.mixin.Mixin;

import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -23,33 +24,33 @@ public class BarrierBlock_updateSuppressionBlockMixin extends Block {
public BarrierBlock_updateSuppressionBlockMixin(Properties settings) { super(settings); }

@Override
public int getSignal(BlockState state, BlockGetter world, BlockPos pos, Direction direction) {
public int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) {
return (shouldPower && direction == Direction.DOWN) ? 15 : 0;
}

@Override
public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) {
if (CarpetSettings.updateSuppressionBlockSetting != -1) {
public void neighborChanged(BlockState state, Level level, BlockPos pos, Block block, BlockPos fromPos, boolean notify) {
if (CarpetSettings.updateSuppressionBlock != -1) {
if (fromPos.equals(pos.above())) {
BlockState stateAbove = world.getBlockState(fromPos);
BlockState stateAbove = level.getBlockState(fromPos);
if (stateAbove.is(Blocks.ACTIVATOR_RAIL) && !stateAbove.getValue(PoweredRailBlock.POWERED)) {
if (CarpetSettings.updateSuppressionBlockSetting > 0) {
world.scheduleTick(pos, this, CarpetSettings.updateSuppressionBlockSetting);
}
throw new StackOverflowError("updateSuppressionBlock");
level.scheduleTick(pos, this, 1);
NeighborUpdater updater = ((LevelInterface)level).getNeighborUpdater();
if (updater instanceof CollectingNeighborUpdaterAccessor cnua)
cnua.setCount(cnua.getMaxChainedNeighborUpdates()-CarpetSettings.updateSuppressionBlock);
}
}
}
super.neighborChanged(state, world, pos, block, fromPos, notify);
super.neighborChanged(state, level, pos, block, fromPos, notify);
}

@Override
public void tick(BlockState state, ServerLevel world, BlockPos pos, RandomSource random) {
public void tick(BlockState state, ServerLevel level, BlockPos pos, RandomSource random) {
BlockPos posAbove = pos.above();
BlockState stateAbove = world.getBlockState(posAbove);
BlockState stateAbove = level.getBlockState(posAbove);
if (stateAbove.is(Blocks.ACTIVATOR_RAIL) && !stateAbove.getValue(PoweredRailBlock.POWERED)) {
shouldPower = true;
world.setBlock(posAbove, stateAbove.setValue(PoweredRailBlock.POWERED, true), 3);
level.setBlock(posAbove, stateAbove.setValue(PoweredRailBlock.POWERED, true), Block.UPDATE_CLIENTS | Block.UPDATE_NONE);
shouldPower = false;
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/carpet/mixins/CollectingNeighborUpdaterAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package carpet.mixins;

import net.minecraft.world.level.redstone.CollectingNeighborUpdater;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(CollectingNeighborUpdater.class)
public interface CollectingNeighborUpdaterAccessor {
@Accessor("count")
void setCount(int count);
@Accessor("maxChainedNeighborUpdates")
int getMaxChainedNeighborUpdates();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package carpet.mixins;

import carpet.fakes.WorldInterface;
import carpet.fakes.LevelInterface;
import com.google.common.collect.Lists;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -18,7 +18,7 @@
import net.minecraft.world.phys.AABB;

@Mixin(Level.class)
public abstract class Level_getOtherEntitiesLimited implements WorldInterface {
public abstract class Level_getOtherEntitiesLimited implements LevelInterface {

private static final RuntimeException CONTROL_FLOW_EXCEPTION = new RuntimeException("Should be caught for control flow in World_getOtherEntitiesLimited!");

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/carpet/mixins/Level_movableBEMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package carpet.mixins;

import carpet.fakes.WorldChunkInterface;
import carpet.fakes.WorldInterface;
import carpet.fakes.LevelInterface;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ChunkHolder;
import net.minecraft.util.profiling.ProfilerFiller;
Expand All @@ -19,7 +19,7 @@
import org.spongepowered.asm.mixin.Shadow;

@Mixin(Level.class)
public abstract class Level_movableBEMixin implements WorldInterface, LevelAccessor
public abstract class Level_movableBEMixin implements LevelInterface, LevelAccessor
{
@Shadow
@Final
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/carpet/mixins/Level_tickMixin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package carpet.mixins;

import carpet.fakes.WorldInterface;
import carpet.fakes.LevelInterface;
import carpet.helpers.TickSpeed;
import carpet.utils.CarpetProfiler;
import net.minecraft.world.level.redstone.NeighborUpdater;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -19,14 +21,21 @@
import net.minecraft.world.level.Level;

@Mixin(Level.class)
public abstract class Level_tickMixin implements WorldInterface
public abstract class Level_tickMixin implements LevelInterface
{
@Shadow @Final public boolean isClientSide;
@Shadow @Final protected NeighborUpdater neighborUpdater;
CarpetProfiler.ProfilerToken currentSection;
CarpetProfiler.ProfilerToken entitySection;

Map<EntityType<?>, Entity> precookedMobs = new HashMap<>();

@Override
@Unique
public NeighborUpdater getNeighborUpdater() {
return this.neighborUpdater;
}

public Map<EntityType<?>, Entity> getPrecookedMobs()
{
return precookedMobs;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.fakes.WorldInterface;
import carpet.fakes.LevelInterface;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand Down Expand Up @@ -39,7 +39,7 @@ private void tickPushingReplacement(CallbackInfo ci) {
if (CarpetSettings.maxEntityCollisions > 0)
{
maxEntityCramming = this.level.getGameRules().getInt(GameRules.RULE_MAX_ENTITY_CRAMMING);
entities = ((WorldInterface) this.level).getOtherEntitiesLimited(
entities = ((LevelInterface) this.level).getOtherEntitiesLimited(
this,
this.getBoundingBox(),
EntitySelector.pushableBy(this),
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/carpet/mixins/NaturalSpawnerMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package carpet.mixins;

import carpet.CarpetSettings;
import carpet.fakes.WorldInterface;
import carpet.fakes.LevelInterface;
import carpet.utils.SpawnReporter;
import org.apache.commons.lang3.tuple.Pair;
import org.spongepowered.asm.mixin.Final;
Expand Down Expand Up @@ -139,7 +139,7 @@ private static Entity create(EntityType<?> entityType, Level world_1)
{
if (CarpetSettings.lagFreeSpawning)
{
Map<EntityType<?>, Entity> precookedMobs = ((WorldInterface)world_1).getPrecookedMobs();
Map<EntityType<?>, Entity> precookedMobs = ((LevelInterface)world_1).getPrecookedMobs();
if (precookedMobs.containsKey(entityType))
//this mob has been <init>'s but not used yet
return precookedMobs.get(entityType);
Expand All @@ -159,7 +159,7 @@ private static void spawnEntity(ServerLevel world, Entity entity_1,
{
if (CarpetSettings.lagFreeSpawning)
// we used the mob - next time we will create a new one when needed
((WorldInterface) world).getPrecookedMobs().remove(entity_1.getType());
((LevelInterface) world).getPrecookedMobs().remove(entity_1.getType());

if (SpawnReporter.track_spawns > 0L && SpawnReporter.local_spawns != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import carpet.CarpetSettings;
import carpet.fakes.BlockEntityInterface;
import carpet.fakes.PistonBlockEntityInterface;
import carpet.fakes.WorldInterface;
import carpet.fakes.LevelInterface;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
Expand Down Expand Up @@ -92,7 +92,7 @@ private static boolean movableTEsetBlockState0(
if (!CarpetSettings.movableBlockEntities)
return world.setBlock(blockPos_1, blockAState_2, int_1);
else
return ((WorldInterface) (world)).setBlockStateWithBlockEntity(blockPos_1, blockAState_2, ((PistonBlockEntityInterface)pistonBlockEntity).getCarriedBlockEntity(), int_1);
return ((LevelInterface) (world)).setBlockStateWithBlockEntity(blockPos_1, blockAState_2, ((PistonBlockEntityInterface)pistonBlockEntity).getCarriedBlockEntity(), int_1);
}

@Redirect(method = "finalTick", at = @At(value = "INVOKE",
Expand All @@ -103,7 +103,7 @@ private boolean movableTEsetBlockState1(Level world, BlockPos blockPos_1, BlockS
return world.setBlock(blockPos_1, blockState_2, int_1);
else
{
boolean ret = ((WorldInterface) (world)).setBlockStateWithBlockEntity(blockPos_1, blockState_2, this.carriedBlockEntity, int_1);
boolean ret = ((LevelInterface) (world)).setBlockStateWithBlockEntity(blockPos_1, blockState_2, this.carriedBlockEntity, int_1);
this.carriedBlockEntity = null; //this will cancel the finishHandleBroken
return ret;
}
Expand All @@ -122,7 +122,7 @@ private void finishHandleBroken(CallbackInfo cir)
blockState_2 = Blocks.AIR.defaultBlockState();
else
blockState_2 = Block.updateFromNeighbourShapes(this.movedState, this.level, this.worldPosition);
((WorldInterface) (this.level)).setBlockStateWithBlockEntity(this.worldPosition, blockState_2, this.carriedBlockEntity, 3);
((LevelInterface) (this.level)).setBlockStateWithBlockEntity(this.worldPosition, blockState_2, this.carriedBlockEntity, 3);
this.level.destroyBlock(this.worldPosition, false, null);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/carpet.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@
"MinecraftServer_updateSuppressionCrashFixMixin",
"ServerPlayer_updateSuppressionCrashFixMixin",
"ChunkMap_creativePlayersLoadChunksMixin",
"SculkSensorBlock_rangeMixin"
"SculkSensorBlock_rangeMixin",
"CollectingNeighborUpdaterAccessor"
],
"client": [
"Timer_tickSpeedMixin",
Expand Down

0 comments on commit 9daf72b

Please sign in to comment.