From 8c84d83a64e63ef310bd3c0df85f4982ccfd6968 Mon Sep 17 00:00:00 2001 From: CreepyCre Date: Fri, 29 Oct 2021 00:59:54 +0200 Subject: [PATCH] significally boost decay performance by using a Hashmap --- .../dimdoors/datagen/LimboDecayProvider.java | 1 - .../dimdoors/world/decay/DecayPattern.java | 7 +++++ .../dimdoors/world/decay/DecayPredicate.java | 11 ++++++- .../dimdoors/world/decay/LimboDecay.java | 31 +++++++++++-------- .../predicates/SimpleDecayPredicate.java | 9 +++++- 5 files changed, 43 insertions(+), 16 deletions(-) diff --git a/src/datagen/java/org/dimdev/dimdoors/datagen/LimboDecayProvider.java b/src/datagen/java/org/dimdev/dimdoors/datagen/LimboDecayProvider.java index f422e3b4..01a56164 100644 --- a/src/datagen/java/org/dimdev/dimdoors/datagen/LimboDecayProvider.java +++ b/src/datagen/java/org/dimdev/dimdoors/datagen/LimboDecayProvider.java @@ -157,7 +157,6 @@ public class LimboDecayProvider implements DataProvider { createSimplePattern(new Identifier("dimdoors:jungle_wood"), JUNGLE_WOOD, JUNGLE_LOG).run(consumer); createSimplePattern(new Identifier("dimdoors:acacia_wood"), ACACIA_WOOD, ACACIA_LOG).run(consumer); createSimplePattern(new Identifier("dimdoors:dark_oak_wood"), DARK_OAK_WOOD, DARK_OAK_LOG).run(consumer); - createSimplePattern(new Identifier("dimdoors:bedrock"), BEDROCK, SOLID_STATIC).run(consumer); } private DecayPatternData turnIntoSelf(Identifier identifier, Block before) { diff --git a/src/main/java/org/dimdev/dimdoors/world/decay/DecayPattern.java b/src/main/java/org/dimdev/dimdoors/world/decay/DecayPattern.java index 01595b6a..394ee35f 100644 --- a/src/main/java/org/dimdev/dimdoors/world/decay/DecayPattern.java +++ b/src/main/java/org/dimdev/dimdoors/world/decay/DecayPattern.java @@ -1,5 +1,6 @@ package org.dimdev.dimdoors.world.decay; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.nbt.NbtCompound; import net.minecraft.util.math.BlockPos; @@ -8,6 +9,8 @@ import net.minecraft.world.World; import net.fabricmc.fabric.api.event.Event; import net.fabricmc.fabric.api.event.EventFactory; +import java.util.Set; + public class DecayPattern { public static final Event ENTROPY_EVENT = EventFactory.createArrayBacked(EntropyEvent.class, (world, pos, entorpy) -> {}, entropyEvents -> (world, pos, entorpy) -> { for (EntropyEvent event : entropyEvents) event.entropy(world, pos, entorpy); @@ -37,6 +40,10 @@ public class DecayPattern { ENTROPY_EVENT.invoker().entropy(world, pos, processor.process(world, pos, origin, target)); } + public Set constructApplicableBlocks() { + return predicate.constructApplicableBlocks(); + } + public static DecayPattern.Builder builder() { return new DecayPattern.Builder(); } diff --git a/src/main/java/org/dimdev/dimdoors/world/decay/DecayPredicate.java b/src/main/java/org/dimdev/dimdoors/world/decay/DecayPredicate.java index 98179b32..ee12d552 100644 --- a/src/main/java/org/dimdev/dimdoors/world/decay/DecayPredicate.java +++ b/src/main/java/org/dimdev/dimdoors/world/decay/DecayPredicate.java @@ -1,8 +1,10 @@ package org.dimdev.dimdoors.world.decay; +import java.util.Set; import java.util.function.Supplier; import com.mojang.serialization.Lifecycle; +import net.minecraft.block.Block; import net.minecraft.nbt.NbtCompound; import org.dimdev.dimdoors.DimensionalDoorsInitializer; import org.dimdev.dimdoors.world.decay.predicates.SimpleDecayPredicate; @@ -43,7 +45,12 @@ public interface DecayPredicate { public boolean test(World world, BlockPos pos, BlockState origin, BlockState target) { return false; } - }; + + @Override + public Set constructApplicableBlocks() { + return Set.of(); + } + }; static DecayPredicate deserialize(NbtCompound nbt) { Identifier id = Identifier.tryParse(nbt.getString("type")); @@ -67,6 +74,8 @@ public interface DecayPredicate { boolean test(World world, BlockPos pos, BlockState origin, BlockState target); + Set constructApplicableBlocks(); + interface DecayPredicateType { DecayPredicateType NONE_PREDICATE_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY); DecayPredicateType SIMPLE_PREDICATE_TYPE = register(new Identifier("dimdoors", SimpleDecayProcesor.KEY), SimpleDecayPredicate::new); diff --git a/src/main/java/org/dimdev/dimdoors/world/decay/LimboDecay.java b/src/main/java/org/dimdev/dimdoors/world/decay/LimboDecay.java index 17e3b289..75f7dd0e 100644 --- a/src/main/java/org/dimdev/dimdoors/world/decay/LimboDecay.java +++ b/src/main/java/org/dimdev/dimdoors/world/decay/LimboDecay.java @@ -6,17 +6,15 @@ import java.util.stream.Collectors; import net.fabricmc.fabric.api.resource.SimpleSynchronousResourceReloadListener; +import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; -import net.minecraft.network.packet.s2c.play.BlockBreakingProgressS2CPacket; import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.resource.ResourceManager; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.SoundCategory; -import net.minecraft.sound.SoundEvent; -import net.minecraft.sound.SoundEvents; import net.minecraft.util.math.Direction; import net.minecraft.util.registry.RegistryKey; import org.apache.logging.log4j.LogManager; @@ -26,7 +24,6 @@ import org.dimdev.dimdoors.network.ExtendedServerPlayNetworkHandler; import org.dimdev.dimdoors.network.packet.s2c.RenderBreakBlockS2CPacket; import org.dimdev.dimdoors.sound.ModSoundEvents; import org.dimdev.dimdoors.util.ResourceUtil; -import org.jetbrains.annotations.NotNull; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -68,12 +65,15 @@ public final class LimboDecay { * Checks if a block can be decayed and, if so, changes it to the next block ID along the decay sequence. */ private static void decayBlock(ServerWorld world, BlockPos pos, BlockState origin) { - @NotNull Collection patterns = DecayLoader.getInstance().getPatterns(); - - if(patterns.isEmpty()) return; - BlockState target = world.getBlockState(pos); + Collection patterns = DecayLoader.getInstance().getPatterns(target.getBlock()); + + if(patterns == null || patterns.isEmpty()) { + return; + } + + for(DecayPattern pattern : patterns) { if (!pattern.test(world, pos, origin, target)) { continue; @@ -109,7 +109,7 @@ public final class LimboDecay { public static class DecayLoader implements SimpleSynchronousResourceReloadListener { private static final Logger LOGGER = LogManager.getLogger(); private static final DecayLoader INSTANCE = new DecayLoader(); - private List patterns = new ArrayList<>(); + private final Map> patterns = new HashMap(); private DecayLoader() { } @@ -121,16 +121,21 @@ public final class LimboDecay { @Override public void reload(ResourceManager manager) { patterns.clear(); - CompletableFuture> futurePatternMap = ResourceUtil.loadResourcePathToCollection(manager, "decay_patterns", ".json", new ArrayList<>(), ResourceUtil.NBT_READER.andThenReader(this::loadPattern)); - patterns = futurePatternMap.join(); + CompletableFuture> futurePatternList = ResourceUtil.loadResourcePathToCollection(manager, "decay_patterns", ".json", new ArrayList<>(), ResourceUtil.NBT_READER.andThenReader(this::loadPattern)); + for (DecayPattern pattern : futurePatternList.join()) { + for (Block block : pattern.constructApplicableBlocks()) { + patterns.computeIfAbsent(block, (b) -> new ArrayList<>()); + patterns.get(block).add(pattern); + } + } } private DecayPattern loadPattern(NbtElement nbt, Identifier ignored) { return DecayPattern.deserialize((NbtCompound) nbt); } - public @NotNull Collection getPatterns() { - return patterns; + public Collection getPatterns(Block block) { + return patterns.get(block); } @Override diff --git a/src/main/java/org/dimdev/dimdoors/world/decay/predicates/SimpleDecayPredicate.java b/src/main/java/org/dimdev/dimdoors/world/decay/predicates/SimpleDecayPredicate.java index aa1ba796..f34f567b 100644 --- a/src/main/java/org/dimdev/dimdoors/world/decay/predicates/SimpleDecayPredicate.java +++ b/src/main/java/org/dimdev/dimdoors/world/decay/predicates/SimpleDecayPredicate.java @@ -13,6 +13,8 @@ import net.minecraft.util.math.BlockPos; import net.minecraft.util.registry.Registry; import net.minecraft.world.World; +import java.util.Set; + public class SimpleDecayPredicate implements DecayPredicate { public static final String KEY = "simple"; @@ -54,7 +56,12 @@ public class SimpleDecayPredicate implements DecayPredicate { return state.getBlock() == block; } - public static Builder builder() { + @Override + public Set constructApplicableBlocks() { + return Set.of(block); + } + + public static Builder builder() { return new Builder(); }