add inject lock

This commit is contained in:
dfsek committed 2022-05-30 15:53:37 -07:00
1 parent 223db88462
commit 2f189d94ac
2 files changed
+17 -30

No files matched your search

@@ -94,45 +94,17 @@ public class BukkitChunkGeneratorWrapper extends org.bukkit.generator.ChunkGener
@Override @Override
public boolean shouldGenerateDecorations() { public boolean shouldGenerateDecorations() {
return false; return false;
//return pack.vanillaFlora();
} }
@Override
public @Nullable Location getFixedSpawnLocation(@NotNull World world, @NotNull Random random) {
LOGGER.info("Getting spawn location...");
int x = random.nextInt(-500, 500);
int z = random.nextInt(-500, 500);
int y = world.getMaxHeight() - 1;
int max = 500;
for(int i = 0; i < max; i++) {
BukkitWorldProperties properties = new BukkitWorldProperties(world);
while(y > world.getMinHeight() && delegate.getBlock(properties, x, y, z, pack.getBiomeProvider()).isAir()) y--;
if(((BlockData) delegate.getBlock(properties, x, y, z, pack.getBiomeProvider()).getHandle()).getMaterial().isSolid()) {
LOGGER.info("Found spawn on attempt {}", i + 1);
break;
} else {
x = random.nextInt(-500, 500);
z = random.nextInt(-500, 500);
y = world.getMaxHeight() - 1;
if(i % 25 == 0) LOGGER.info("Spawn finding attempt {}/{} failed. Retrying...", i, max);
}
}
return new Location(world, x, y, z);
}
@Override @Override
public boolean shouldGenerateMobs() { public boolean shouldGenerateMobs() {
return true; return true;
//return pack.vanillaMobs();
} }
@Override @Override
public boolean shouldGenerateStructures() { public boolean shouldGenerateStructures() {
return true; return true;
//return pack.vanillaStructures();
} }
public ConfigPack getPack() { public ConfigPack getPack() {
@@ -1,25 +1,39 @@
package com.dfsek.terra.bukkit.nms; package com.dfsek.terra.bukkit.nms;
import com.dfsek.terra.api.block.state.BlockState;
import com.dfsek.terra.api.config.ConfigPack; import com.dfsek.terra.api.config.ConfigPack;
import com.dfsek.terra.api.world.chunk.generation.ProtoChunk;
import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper; import com.dfsek.terra.bukkit.generator.BukkitChunkGeneratorWrapper;
import com.dfsek.terra.bukkit.world.BukkitWorldProperties;
import net.minecraft.server.level.WorldServer; import net.minecraft.server.level.WorldServer;
import net.minecraft.world.level.chunk.ChunkGenerator; import net.minecraft.world.level.chunk.ChunkGenerator;
import org.bukkit.World;
import org.bukkit.craftbukkit.v1_18_R2.CraftWorld; import org.bukkit.craftbukkit.v1_18_R2.CraftWorld;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldInitEvent; import org.bukkit.event.world.WorldInitEvent;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
public class NMSInjectListener implements Listener { public class NMSInjectListener implements Listener {
private static final Logger LOGGER = LoggerFactory.getLogger(NMSInjectListener.class); private static final Logger LOGGER = LoggerFactory.getLogger(NMSInjectListener.class);
private static final Set<World> INJECTED = new HashSet<>();
private static final ReentrantLock INJECT_LOCK = new ReentrantLock();
@EventHandler @EventHandler
public void onWorldInit(WorldInitEvent event) { public void onWorldInit(WorldInitEvent event) {
if (event.getWorld().getGenerator() instanceof BukkitChunkGeneratorWrapper bukkitChunkGeneratorWrapper) { if (!INJECTED.contains(event.getWorld()) && event.getWorld().getGenerator() instanceof BukkitChunkGeneratorWrapper bukkitChunkGeneratorWrapper) {
LOGGER.info("A great evil has fallen upon this land: {}", event.getWorld().getName()); INJECT_LOCK.lock();
INJECTED.add(event.getWorld());
LOGGER.info("Preparing to take over the world: {}", event.getWorld().getName());
CraftWorld craftWorld = (CraftWorld) event.getWorld(); CraftWorld craftWorld = (CraftWorld) event.getWorld();
WorldServer serverWorld = craftWorld.getHandle(); WorldServer serverWorld = craftWorld.getHandle();
@@ -34,6 +48,7 @@ public class NMSInjectListener implements Listener {
serverWorld.k().a.u = custom; serverWorld.k().a.u = custom;
LOGGER.info("Successfully injected into world."); LOGGER.info("Successfully injected into world.");
INJECT_LOCK.unlock();
} }
} }
} }