Skip to content

Commit

Permalink
Refactoring, Fixed multiplyChance dividing instead of multiplying. Close
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Sep 6, 2021
1 parent d025861 commit de6c40c
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 162 deletions.
32 changes: 3 additions & 29 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,25 @@ buildscript {
maven {
name = "forge"
url = "http://files.minecraftforge.net/maven"
}
maven {
name = "Modmuss50"
url = "http://maven.modmuss50.me/"
}
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
classpath 'ModsIOUpload:ModsIOUpload:+'
}
}
plugins {
id "com.matthewprenger.cursegradle" version "1.4.0"
}
apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'net.cazzar.gradle.modsio'

allprojects {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

version = "2.3.0"
group= "the_fireplace.wgblockreplacer"
archivesBaseName = "WorldgenBlockReplacer-1.12.2"
version = "2.3.1+1.12.2"
group= "dev.the-fireplace"
archivesBaseName = "WorldGen-Block-Replacer"

minecraft {
version = "1.12.2-14.23.5.2847"
Expand Down Expand Up @@ -86,25 +80,5 @@ curseforge {
mainArtifact(jar) {
displayName = jar.archiveName.replace(".jar", "")
}
addArtifact(sourceJar) {
displayName = sourceJar.archiveName.replace(".jar", "")
changelog = "*This is a file for mod developers. If you don't know what to do with it, don't use it!*"
}
addArtifact(deobfJar) {
displayName = deobfJar.archiveName.replace(".jar", "")
changelog = "*This is a file for mod developers. If you don't know what to do with it, don't use it!*"
}
}
}

modsIO {
key = project.hasProperty("modsioApiKey")? project.modsioApiKey : "empty"
project{
artifact = new File(jar.destinationDir, archivesBaseName+"-"+version+".jar")
modid = "1276"
minecraft = "1.12.2"
try{changelog = file('changelog.txt').text}catch(Exception e){changelog=""}
tag = ""
current = true
}
}
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Improved block replacement performance
Fixed multiplyChance dividing instead of multiplying
138 changes: 5 additions & 133 deletions src/main/java/the_fireplace/wgblockreplacer/events/CommonEvents.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package the_fireplace.wgblockreplacer.events;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
import net.minecraftforge.event.world.ChunkEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.tuple.Pair;
import the_fireplace.wgblockreplacer.WGBlockReplacer;
import the_fireplace.wgblockreplacer.api.config.ConfigAccess;
import the_fireplace.wgblockreplacer.api.config.ConfigValidator;
import the_fireplace.wgblockreplacer.api.world.ChunkReplacementData;

import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import the_fireplace.wgblockreplacer.logic.ReplacementQueue;
import the_fireplace.wgblockreplacer.logic.Replacer;

@Mod.EventBusSubscriber(modid = WGBlockReplacer.MODID)
public final class CommonEvents {
private static final Random rand = new Random();

@SuppressWarnings("Duplicates")
@SubscribeEvent(priority = EventPriority.LOWEST)
public static void onEvent(ChunkEvent.Load event) {
if (event.getWorld().isRemote
Expand All @@ -38,128 +24,14 @@ public static void onEvent(ChunkEvent.Load event) {
return;
}
if (ConfigAccess.getInstance().getLateReplacementTicks() > 0) {
queueReplacement(event.getWorld(), event.getChunk());
ReplacementQueue.add(event.getWorld(), event.getChunk());
} else {
doReplacement(event.getWorld(), event.getChunk());
Replacer.doReplacement(event.getWorld(), event.getChunk());
}
}

@SubscribeEvent
public static void onWorldTick(TickEvent.ServerTickEvent event) {
for (Map.Entry<Pair<World, Chunk>, Integer> p: TIMERS.entrySet()) {
if (p.getValue() <= 0) {
TIMERS.remove(p.getKey());
doReplacement(p.getKey().getLeft(), p.getKey().getRight());
} else {
TIMERS.put(p.getKey(), p.getValue() - 1);
}
}
}

private static final Map<Pair<World, Chunk>, Integer> TIMERS = new ConcurrentHashMap<>();

private static void queueReplacement(World world, Chunk chunk) {
Pair<World, Chunk> p = Pair.of(world, chunk);
TIMERS.putIfAbsent(p, ConfigAccess.getInstance().getLateReplacementTicks());
}

private static void doReplacement(World world, Chunk chunk) {
for (int i = 0; i < ConfigAccess.getInstance().getReplaceBlockIds().length; i++) {
if (!canReplaceInDimension(world, ConfigAccess.getInstance().getDimensionLists()[i])) {
continue;
}

if (!ConfigAccess.getInstance().useBiomePrecision()) {
Biome approximateBiome = chunk.getBiome(new BlockPos(chunk.getPos().getXStart(), 64, chunk.getPos().getZStart()), world.getBiomeProvider());
if (!canReplaceInBiome(approximateBiome, ConfigAccess.getInstance().getBiomeFilterLists()[i]))
continue;
}

Block toBlock = Block.getBlockFromName(ConfigAccess.getInstance().getReplaceWithBlockIds()[i]);
Block fromBlock = Block.getBlockFromName(ConfigAccess.getInstance().getReplaceBlockIds()[i]);
int fromMeta = ConfigAccess.getInstance().getReplaceBlockMetas()[i];
int toMeta = ConfigAccess.getInstance().getReplaceWithMetas()[i];
if (toBlock == fromBlock && fromMeta == toMeta) {
continue;
}

assert fromBlock != null;
assert toBlock != null;

IBlockState fromState;
if (fromMeta == -1)
fromState = fromBlock.getDefaultState();
else
fromState = fromBlock.getStateFromMeta(fromMeta);

IBlockState toState;
if (toMeta == -1)
toState = toBlock.getDefaultState();
else
toState = toBlock.getStateFromMeta(toMeta);

int storageYIndex = 0;
for (ExtendedBlockStorage storage : chunk.getBlockStorageArray()) {
if (storage != null) {
for (int storageX = 0; storageX < 16; storageX++) {
for (int storageY = 0; storageY < 16; storageY++) {
for (int storageZ = 0; storageZ < 16; storageZ++) {
if (storage.get(storageX, storageY, storageZ).equals(fromState)) {
int worldY = storageYIndex * 16 + storageY;
if (ConfigAccess.getInstance().getMinYs()[i] <= worldY
&& ConfigAccess.getInstance().getMaxYs()[i] >= worldY
&& rand.nextDouble() * (ConfigAccess.getInstance().getMultiplyChances()[i] ? worldY : 1) <= ConfigAccess.getInstance().getReplaceChances()[i]
) {
if (ConfigAccess.getInstance().useBiomePrecision()) {
Biome biome = chunk.getBiome(new BlockPos(storageX, storageY, storageZ), world.getBiomeProvider());
if (!canReplaceInBiome(biome, ConfigAccess.getInstance().getBiomeFilterLists()[i])) {
continue;
}
}
storage.set(storageX, storageY, storageZ, toState);
}
}
}
}
}
}
storageYIndex++;
}
chunk.markDirty();
}
ChunkReplacementData.getInstance().markAsReplaced(chunk);
}

private static boolean canReplaceInBiome(Biome biome1, String biomeList) {
boolean doEvent = ArrayUtils.contains(biomeList.split(","), "*");
for (String biome : biomeList.split(",")) {
if ("*".equals(biome)) {
continue;
}
if (biome1 == Biome.REGISTRY.getObject(new ResourceLocation(biome))) {
doEvent = !doEvent;
break;
}
}

return doEvent;
}

private static boolean canReplaceInDimension(World world, String s) {
boolean doEvent = ArrayUtils.contains(s.split(","), "*");
for (String dim : s.split(",")) {
try {
if (world.provider.getDimension() == Integer.parseInt(dim)) {
doEvent = !doEvent;
break;
}
} catch (NumberFormatException e) {
if (!dim.equals("*") && world.provider.getDimensionType().getName().equalsIgnoreCase(dim)) {
doEvent = !doEvent;
break;
}
}
}
return doEvent;
ReplacementQueue.tick();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package the_fireplace.wgblockreplacer.logic;

import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import org.apache.commons.lang3.tuple.Pair;
import the_fireplace.wgblockreplacer.api.config.ConfigAccess;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public final class ReplacementQueue {
private static final Map<Pair<World, Chunk>, Integer> TIMERS = new ConcurrentHashMap<>();

public static void tick() {
for (Map.Entry<Pair<World, Chunk>, Integer> p : TIMERS.entrySet()) {
if (p.getValue() <= 0) {
TIMERS.remove(p.getKey());
Replacer.doReplacement(p.getKey().getLeft(), p.getKey().getRight());
} else {
TIMERS.put(p.getKey(), p.getValue() - 1);
}
}
}

public static void add(World world, Chunk chunk) {
Pair<World, Chunk> p = Pair.of(world, chunk);
TIMERS.putIfAbsent(p, ConfigAccess.getInstance().getLateReplacementTicks());
}
}
Loading

0 comments on commit de6c40c

Please sign in to comment.