Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update updateSuppressionBlock for 1.19 #1401

Merged
merged 12 commits into from
May 17, 2022
43 changes: 8 additions & 35 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 @@ -1045,53 +1044,27 @@ public String validate(CommandSourceStack source, ParsedRule<String> currentRule
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
)
public static String updateSuppressionBlock = "false";
public static int updateSuppressionBlock = -1;

@Rule(
desc = "Fixes update suppression causing server crashes.",
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> {
FxMorin marked this conversation as resolved.
Show resolved Hide resolved
@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;
}
}
}
public Integer validate(CommandSourceStack source, ParsedRule<Integer> currentRule, Integer newValue, String string) {
if (newValue < -1) newValue = -1;
FxMorin marked this conversation as resolved.
Show resolved Hide resolved
return newValue;
}
@Override
public String description() {
return "Cannot be negative, can be true, false, or # > 0";
}
FxMorin marked this conversation as resolved.
Show resolved Hide resolved
}

@Rule(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package carpet.mixins;

import carpet.CarpetSettings;
import net.minecraft.world.level.redstone.CollectingNeighborUpdater;
import net.minecraft.world.level.redstone.NeighborUpdater;
import org.spongepowered.asm.mixin.Mixin;

import java.util.Random;
Expand All @@ -22,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 = ((LevelAccessor)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, Random random) {
public void tick(BlockState state, ServerLevel level, BlockPos pos, Random 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();
altrisi marked this conversation as resolved.
Show resolved Hide resolved
}
12 changes: 12 additions & 0 deletions src/main/java/carpet/mixins/LevelAccessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package carpet.mixins;

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

@Mixin(Level.class)
public interface LevelAccessor {
FxMorin marked this conversation as resolved.
Show resolved Hide resolved
@Accessor("neighborUpdater")
NeighborUpdater getNeighborUpdater();
}
4 changes: 3 additions & 1 deletion src/main/resources/carpet.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@
"MinecraftServer_updateSuppressionCrashFixMixin",
"ServerPlayer_updateSuppressionCrashFixMixin",
"ChunkMap_creativePlayersLoadChunksMixin",
"SculkSensorBlock_rangeMixin"
"SculkSensorBlock_rangeMixin",
"LevelAccessor",
"CollectingNeighborUpdaterAccessor"
],
"client": [
"Timer_tickSpeedMixin",
Expand Down