significally boost decay performance by using a Hashmap

This commit is contained in:
CreepyCre 2021-10-29 00:59:54 +02:00
parent 8dac8ceff2
commit 8c84d83a64
5 changed files with 43 additions and 16 deletions

View file

@ -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) {

View file

@ -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<EntropyEvent> 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<Block> constructApplicableBlocks() {
return predicate.constructApplicableBlocks();
}
public static DecayPattern.Builder builder() {
return new DecayPattern.Builder();
}

View file

@ -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<Block> 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<Block> constructApplicableBlocks();
interface DecayPredicateType<T extends DecayPredicate> {
DecayPredicateType<DecayPredicate> NONE_PREDICATE_TYPE = register(new Identifier("dimdoors", "none"), () -> DUMMY);
DecayPredicateType<SimpleDecayPredicate> SIMPLE_PREDICATE_TYPE = register(new Identifier("dimdoors", SimpleDecayProcesor.KEY), SimpleDecayPredicate::new);

View file

@ -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<DecayPattern> patterns = DecayLoader.getInstance().getPatterns();
if(patterns.isEmpty()) return;
BlockState target = world.getBlockState(pos);
Collection<DecayPattern> 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<DecayPattern> patterns = new ArrayList<>();
private final Map<Block, List<DecayPattern>> patterns = new HashMap();
private DecayLoader() {
}
@ -121,16 +121,21 @@ public final class LimboDecay {
@Override
public void reload(ResourceManager manager) {
patterns.clear();
CompletableFuture<List<DecayPattern>> futurePatternMap = ResourceUtil.loadResourcePathToCollection(manager, "decay_patterns", ".json", new ArrayList<>(), ResourceUtil.NBT_READER.andThenReader(this::loadPattern));
patterns = futurePatternMap.join();
CompletableFuture<List<DecayPattern>> 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<DecayPattern> getPatterns() {
return patterns;
public Collection<DecayPattern> getPatterns(Block block) {
return patterns.get(block);
}
@Override

View file

@ -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<Block> constructApplicableBlocks() {
return Set.of(block);
}
public static Builder builder() {
return new Builder();
}