Skip to content

Instantly share code, notes, and snippets.

@DerFrZocker
Created December 26, 2020 19:37
Show Gist options
  • Save DerFrZocker/7237fbc258aba20403e620641f75da70 to your computer and use it in GitHub Desktop.
Save DerFrZocker/7237fbc258aba20403e620641f75da70 to your computer and use it in GitHub Desktop.
A simple class to check if a Chunk is the start of a Fortress or not. For Spigot and Minecraft version 1.16.4.
/*
* MIT License
*
* Copyright (c) 2020 Marvin (DerFrZocker)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package de.derfrzocker.testplugin.fortress;
import net.minecraft.server.v1_16_R3.*;
import org.bukkit.craftbukkit.v1_16_R3.CraftServer;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.Properties;
import java.util.Random;
public class Test_v1_16_R3 implements Runnable {
private final static int LENGTH = 2000; // in chunks
private final static Random RANDOM = new Random(78923459872345L);
private final DedicatedServer server;
public Test_v1_16_R3(Plugin plugin) {
server = ((CraftServer) plugin.getServer()).getServer();
}
@Override
public void run() {
StructureManager structureManager = new StructureManager(null, null) {
@Override
public World getWorld() {
return null;
}
};
long seed = RANDOM.nextLong();
ChunkGenerator chunkGenerator = createChunkGenerator(seed, WorldDimension.THE_NETHER);
for (int z = 0; z < LENGTH; z++) {
for (int x = 0; x < LENGTH; x++) {
ChunkCoordIntPair pos = new ChunkCoordIntPair(x, z);
ProtoChunk protoChunk = new ProtoChunk(pos, ChunkConverter.a);
chunkGenerator.createStructures(server.getCustomRegistry(), structureManager, protoChunk, server.getDefinedStructureManager(), seed);
StructureStart<?> structureStart = protoChunk.a(StructureGenerator.FORTRESS);
if (structureStart != StructureStart.a && structureStart != null) {
// The chunk has the fortress as structure
} else {
// The chunk has not the fortress as structure
}
}
}
}
public ChunkGenerator createChunkGenerator(long seed, ResourceKey<WorldDimension> actualDimension) {
Properties properties = new Properties();
properties.put("level-seed", Objects.toString(seed));
properties.put("generate-structures", Objects.toString(true));
properties.put("level-type", "default");
GeneratorSettings generatorsettings = GeneratorSettings.a(server.getCustomRegistry(), properties);
RegistryMaterials<WorldDimension> registrymaterials = generatorsettings.d();
WorldDimension worlddimension = registrymaterials.a(actualDimension);
ChunkGenerator chunkgenerator;
if (worlddimension == null) {
chunkgenerator = GeneratorSettings.a(server.getCustomRegistry().b(IRegistry.ay), server.getCustomRegistry().b(IRegistry.ar), (new Random()).nextLong());
} else {
chunkgenerator = worlddimension.c();
}
try {
Field field = ChunkGenerator.class.getDeclaredField("injected");
field.setAccessible(true);
field.set(chunkgenerator, true);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
return chunkgenerator;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment