Skip to content

Commit

Permalink
Merge pull request #7 from BumbleTree/mc/1.20.1
Browse files Browse the repository at this point in the history
Update AcceleratedDecay.java
  • Loading branch information
MichaelHillcox authored Nov 13, 2023
2 parents 23ee420 + a778c55 commit d419ce3
Showing 1 changed file with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,56 +25,50 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Iterator;

public class AcceleratedDecay {
public static final String MOD_ID = "accelerateddecay";

private static final List<TimedDimBlockPos> timeBasedScanLocations = new ArrayList<>();
private static final ConcurrentHashMap<TimedDimBlockPos, Boolean> timeBasedScanLocations = new ConcurrentHashMap<>();

public static void init() {
BlockEvent.BREAK.register(AcceleratedDecay::breakHandler);
TickEvent.SERVER_LEVEL_POST.register(AcceleratedDecay::levelTick);
}

private static void levelTick(ServerLevel serverLevel) {
if (timeBasedScanLocations.isEmpty()) {
return;
}

Instant now = Instant.now();
List<TimedDimBlockPos> locations = timeBasedScanLocations.stream()
.filter(e -> !e.checkAfter.isAfter(now) && serverLevel.dimension().equals(e.dim))
.toList();

if (locations.isEmpty()) {
return;
}
for (Iterator<TimedDimBlockPos> iterator = timeBasedScanLocations.keySet().iterator(); iterator.hasNext(); ) {
TimedDimBlockPos location = iterator.next();
if (!location.checkAfter.isAfter(now) && serverLevel.dimension().equals(location.dim)) {
if (location.player == null || !location.player.isAlive()) {
iterator.remove();
continue;
}

for (TimedDimBlockPos location : locations) {
if (location.player == null || !location.player.isAlive()) {
continue;
}
Set<BlockPos> yeetLeaves = seekLeaves(serverLevel, location.pos);

Set<BlockPos> yeetLeaves = seekLeaves(serverLevel, location.pos);
boolean isFirst = true;
for (BlockPos yeetLeaf : yeetLeaves) {
BlockState blockState = serverLevel.getBlockState(yeetLeaf);
if (!blockState.is(BlockTags.LEAVES)) {
continue;
}

boolean isFirst = true;
for (BlockPos yeetLeaf : yeetLeaves) {
BlockState blockState = serverLevel.getBlockState(yeetLeaf);
if (!blockState.is(BlockTags.LEAVES)) {
continue;
}
EventResult eventResult = BlockEvent.BREAK.invoker().breakBlock(serverLevel, yeetLeaf, blockState, location.player, null);
if (eventResult.isFalse()) {
continue;
}

// Allow events to block us
EventResult eventResult = BlockEvent.BREAK.invoker().breakBlock(serverLevel, yeetLeaf, blockState, location.player, null);
if (eventResult.isFalse()) {
continue;
destroyBlockWithOptionalSoundAndParticles(serverLevel, yeetLeaf, true, 512, location.player, isFirst);
isFirst = false;
}

destroyBlockWithOptionalSoundAndParticles(serverLevel, yeetLeaf, true, 512, location.player, isFirst);
isFirst = false;
iterator.remove();
}

timeBasedScanLocations.remove(location);
}
}

Expand All @@ -83,7 +77,7 @@ private static EventResult breakHandler(Level level, BlockPos blockPos, BlockSta
return EventResult.pass();
}

timeBasedScanLocations.add(new TimedDimBlockPos(Instant.now().plus(1, ChronoUnit.SECONDS), blockPos, level.dimension(), player));
timeBasedScanLocations.put(new TimedDimBlockPos(Instant.now().plus(1, ChronoUnit.SECONDS), blockPos, level.dimension(), player), Boolean.TRUE);
return EventResult.pass();
}

Expand Down Expand Up @@ -131,10 +125,8 @@ public static void destroyBlockWithOptionalSoundAndParticles(Level level, BlockP
Block.dropResources(blockState, level, blockPos, blockEntity, player, ItemStack.EMPTY);
}

boolean bl2 = level.setBlock(blockPos, fluidState.createLegacyBlock(), 3, i);
if (bl2) {
level.gameEvent(player, GameEvent.BLOCK_DESTROY, blockPos);
}
level.setBlock(blockPos, fluidState.createLegacyBlock(), 3, i);
level.gameEvent(player, GameEvent.BLOCK_DESTROY, blockPos);
}

record TimedDimBlockPos(
Expand Down

0 comments on commit d419ce3

Please sign in to comment.