Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ArkoSammy12 committed Nov 3, 2023
1 parent c9e3f41 commit 4137816
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ private static int setBlockPlacementDelayCommand(CommandContext<ServerCommandSou
}

private static int getExplosionHealDelayCommand(CommandContext<ServerCommandSource> ctx){
ctx.getSource().sendMessage(Text.literal("Explosion heal delay currently set to: " + ((double)DelaysConfig.getExplosionHealDelay() / 20) + " second(s)"));
ctx.getSource().sendMessage(Text.literal("Explosion heal delay currently set to: " + ((double)DelaysConfig.getExplosionHealDelayAsTicks() / 20) + " second(s)"));
return Command.SINGLE_SUCCESS;
}

private static int getBlockPlacementDelayCommand(CommandContext<ServerCommandSource> ctx){
ctx.getSource().sendMessage(Text.literal("Block placement delay currently set to: " + ((double) DelaysConfig.getBlockPlacementDelay() / 20) + " second(s)"));
ctx.getSource().sendMessage(Text.literal("Block placement delay currently set to: " + ((double) DelaysConfig.getBlockPlacementDelayAsTicks() / 20) + " second(s)"));
return Command.SINGLE_SUCCESS;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ public ConfigEntry<Double> getEntry(){
private static final String TABLE_COMMENT = """
Configure the delays related to the healing of explosions.""";

public static long getExplosionHealDelay(){
public static long getExplosionHealDelayAsTicks(){
long rounded = Math.round(Math.max(EXPLOSION_HEAL_DELAY.getEntry().getValue(), 0) * 20L);
return rounded == 0 ? 20L : rounded;
}

public static long getBlockPlacementDelay(){
public static long getBlockPlacementDelayAsTicks(){
long rounded = Math.round(Math.max(BLOCK_PLACEMENT_DELAY.getEntry().getValue(), 0) * 20L);
return rounded == 0 ? 20L : rounded;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/xd/arkosammy/explosions/AffectedBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private AffectedBlock(BlockPos pos, BlockState state, RegistryKey<World> registr
}

public static AffectedBlock newAffectedBlock(BlockPos pos, World world){
return new AffectedBlock(pos, world.getBlockState(pos), world.getRegistryKey(), DelaysConfig.getBlockPlacementDelay(), false);
return new AffectedBlock(pos, world.getBlockState(pos), world.getRegistryKey(), DelaysConfig.getBlockPlacementDelayAsTicks(), false);
}

public void setAffectedBlockTimer(long delay){
Expand Down Expand Up @@ -145,7 +145,7 @@ public static void updateAffectedBlocksTimers(){
for(ExplosionEvent explosionEvent : ExplosionListHandler.getExplosionEventList()){
if(explosionEvent.getExplosionMode() == ExplosionHealingMode.DEFAULT_MODE) {
for (int i = explosionEvent.getAffectedBlockCounter() + 1; i < explosionEvent.getAffectedBlocksList().size(); i++) {
explosionEvent.getAffectedBlocksList().get(i).setAffectedBlockTimer(DelaysConfig.getBlockPlacementDelay());
explosionEvent.getAffectedBlocksList().get(i).setAffectedBlockTimer(DelaysConfig.getBlockPlacementDelayAsTicks());
}
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/main/java/xd/arkosammy/explosions/ExplosionEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private ExplosionEvent(List<AffectedBlock> affectedBlocksList, String explosionM
}

public static ExplosionEvent newExplosionEvent(List<AffectedBlock> affectedBlocksList, World world) {
ExplosionEvent explosionEvent = new ExplosionEvent(ExplosionUtils.sortAffectedBlocksList(affectedBlocksList, world.getServer()), ModeConfig.MODE.getEntry().getValue(), DelaysConfig.getExplosionHealDelay(), 0);
ExplosionEvent explosionEvent = new ExplosionEvent(ExplosionUtils.sortAffectedBlocksList(affectedBlocksList, world.getServer()), ModeConfig.MODE.getEntry().getValue(), DelaysConfig.getExplosionHealDelayAsTicks(), 0);
explosionEvent.setUpExplosionHealingMode(world);

Set<ExplosionEvent> collidingExplosions = ExplosionUtils.getCollidingWaitingExplosions(affectedBlocksList.stream().map(AffectedBlock::getPos).toList());
Expand Down Expand Up @@ -95,18 +95,19 @@ public static void tickExplosions(){
}
}

public AffectedBlock getCurrentAffectedBlock(){
public Optional<AffectedBlock> getCurrentAffectedBlock(){
if(this.affectedBlockCounter < this.getAffectedBlocksList().size()){
return this.getAffectedBlocksList().get(affectedBlockCounter);
return Optional.of(this.getAffectedBlocksList().get(affectedBlockCounter));
}
return null;
return Optional.empty();
}

public void delayAffectedBlock(AffectedBlock affectedBlockToDelay, MinecraftServer server){
int indexOfPostponed = this.getAffectedBlocksList().indexOf(affectedBlockToDelay);
if(indexOfPostponed != -1) {
Integer indexOfNextPlaceable = this.findNextPlaceableBlock(server);
if (indexOfNextPlaceable != null) {
Optional<Integer> indexOfNextPlaceableOptional = this.findNextPlaceableBlock(server);
if (indexOfNextPlaceableOptional.isPresent()) {
int indexOfNextPlaceable = indexOfNextPlaceableOptional.get();
Collections.swap(this.getAffectedBlocksList(), indexOfPostponed, indexOfNextPlaceable);
} else {
this.incrementCounter();
Expand All @@ -118,13 +119,13 @@ public void delayAffectedBlock(AffectedBlock affectedBlockToDelay, MinecraftServ
}
}

private Integer findNextPlaceableBlock(MinecraftServer server) {
private Optional<Integer> findNextPlaceableBlock(MinecraftServer server) {
for (int i = this.getCurrentAffectedBlockCounter(); i < this.getAffectedBlocksList().size(); i++) {
if (this.getAffectedBlocksList().get(i).canBePlaced(server)) {
return i;
return Optional.of(i);
}
}
return null;
return Optional.empty();
}

private void setUpExplosionHealingMode(World world){
Expand Down Expand Up @@ -158,8 +159,8 @@ private void setupDifficultyBasedHealingMode(World world){
case NORMAL -> 1;
case HARD -> 2;
};
long finalOffset = Math.max(1, (DelaysConfig.getBlockPlacementDelay()) + (difficultyOffset * 20));
long finalOffsetExplosion = Math.max(1, (DelaysConfig.getExplosionHealDelay()) + (difficultyOffset * 20));
long finalOffset = Math.max(1, (DelaysConfig.getBlockPlacementDelayAsTicks()) + (difficultyOffset * 20));
long finalOffsetExplosion = Math.max(1, (DelaysConfig.getExplosionHealDelayAsTicks()) + (difficultyOffset * 20));
this.setExplosionTimer(finalOffsetExplosion);
this.getAffectedBlocksList().forEach(affectedBlock -> affectedBlock.setAffectedBlockTimer(finalOffset));
}
Expand All @@ -176,7 +177,7 @@ private void setupBlastResistanceBasedHealingMode(World world){
double randomOffset = random.nextBetween(-2, 2);
double affectedBlockBlastResistance = Math.min(affectedBlock.getState().getBlock().getBlastResistance(), 9);
int offset = (int) (MathHelper.lerp(affectedBlockBlastResistance / 9, -2, 2) + randomOffset);
long finalOffset = Math.max(1, DelaysConfig.getBlockPlacementDelay() + (offset * 20L));
long finalOffset = Math.max(1, DelaysConfig.getBlockPlacementDelayAsTicks() + (offset * 20L));
affectedBlock.setAffectedBlockTimer(finalOffset);
});
}
Expand All @@ -186,7 +187,7 @@ private static ExplosionEvent combineCollidingExplosions(Set<ExplosionEvent> col
.flatMap(explosionEvent -> explosionEvent.getAffectedBlocksList().stream())
.collect(Collectors.toList());
ExplosionEvent explosionEvent = new ExplosionEvent(ExplosionUtils.sortAffectedBlocksList(combinedAffectedBlockList, world.getServer()), newestExplosion.getExplosionMode().getName(), newestExplosion.getExplosionTimer(), newestExplosion.getCurrentAffectedBlockCounter());
long newestExplosionBlockTimers = DelaysConfig.getBlockPlacementDelay();
long newestExplosionBlockTimers = DelaysConfig.getBlockPlacementDelayAsTicks();
explosionEvent.getAffectedBlocksList().forEach(affectedBlock -> affectedBlock.setAffectedBlockTimer(newestExplosionBlockTimers));
explosionEvent.setUpExplosionHealingMode(world);
return explosionEvent;
Expand Down
19 changes: 9 additions & 10 deletions src/main/java/xd/arkosammy/handlers/ExplosionListHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import xd.arkosammy.explosions.ExplosionEvent;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;

//Thanks to @dale8689 for helping me figure out how to use tick timers instead of ScheduledFutures
Expand All @@ -32,24 +33,20 @@ public static void handleExplosionList(MinecraftServer server){
}

private static void processExplosionEvent(ExplosionEvent currentExplosionEvent, MinecraftServer server){

AffectedBlock currentAffectedBlock = currentExplosionEvent.getCurrentAffectedBlock();

if(currentAffectedBlock == null) {
Optional<AffectedBlock> optionalAffectedBlock = currentExplosionEvent.getCurrentAffectedBlock();
if(optionalAffectedBlock.isEmpty()) {
getExplosionEventList().remove(currentExplosionEvent);
return;
}

AffectedBlock currentAffectedBlock = optionalAffectedBlock.get();
if(currentAffectedBlock.isAlreadyPlaced()){
currentExplosionEvent.incrementCounter();
return;
}

if(!currentAffectedBlock.canBePlaced(server)){
currentExplosionEvent.delayAffectedBlock(currentAffectedBlock, server);
return;
}

currentAffectedBlock.tickAffectedBlock();
if (currentAffectedBlock.getAffectedBlockTimer() < 0) {
handleBlockPlacement(currentAffectedBlock, currentExplosionEvent, server);
Expand All @@ -59,13 +56,15 @@ private static void processExplosionEvent(ExplosionEvent currentExplosionEvent,
private static void handleBlockPlacement(AffectedBlock currentAffectedBlock, ExplosionEvent currentExplosionEvent, MinecraftServer server){
if (!currentExplosionEvent.hasEnoughLightIfDaytimeHealingMode(server)) {
getExplosionEventList().remove(currentExplosionEvent);
return;
}
currentAffectedBlock.tryHealing(server, currentExplosionEvent);
currentAffectedBlock.setPlaced(true);
currentExplosionEvent.incrementCounter();
if(!currentExplosionEvent.shouldKeepHealingIfDifficultyBasedHealingMode(currentAffectedBlock.getWorld(server))) {
getExplosionEventList().remove(currentExplosionEvent);
return;
}
currentAffectedBlock.tryHealing(server, currentExplosionEvent);
currentAffectedBlock.setPlaced(true);
currentExplosionEvent.incrementCounter();
}

}

0 comments on commit 4137816

Please sign in to comment.