Skip to content

Commit

Permalink
Load dat files from config folder
Browse files Browse the repository at this point in the history
This will allow you to copy down the small dat files from your vanilla server, drop them in the folder and when you log in to the vanilla server it'll load all the structures.

It tries to do villages but this only works with villages that were loaded when the files were copied (like in spawn chunks)
  • Loading branch information
irtimaled committed Feb 4, 2015
1 parent 76be025 commit 1918012
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 14 deletions.
7 changes: 7 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxCache.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.irtimaled.bbor;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -23,6 +24,12 @@ public void addBoundingBox(BoundingBox key, Set<BoundingBox> boundingBoxes) {
cache.put(key, boundingBoxes);
}

public void addBoundingBox(BoundingBox key) {
Set<BoundingBox> boundingBoxes = new HashSet<BoundingBox>();
boundingBoxes.add(key);
addBoundingBox(key, boundingBoxes);
}

public void removeBoundingBox(BoundingBox key) {
if (cache.containsKey(key)) {
cache.remove(key);
Expand Down
6 changes: 6 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public static BoundingBoxStructure from(StructureBoundingBox bb, Color color) {
return new BoundingBoxStructure(minBlockPos, maxBlockPos, color);
}

public static BoundingBoxStructure from(int[] bb, Color color) {
BlockPos minBlockPos = new BlockPos(bb[0], bb[1], bb[2]);
BlockPos maxBlockPos = new BlockPos(bb[3], bb[4], bb[5]);
return new BoundingBoxStructure(minBlockPos, maxBlockPos, color);
}

public static BoundingBoxStructure from(BlockPos minBlockPos, BlockPos maxBlockPos, Color color) {
return new BoundingBoxStructure(minBlockPos, maxBlockPos, color);
}
Expand Down
152 changes: 152 additions & 0 deletions java/com/irtimaled/bbor/ClientProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
import net.minecraft.client.renderer.WorldRenderer;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.*;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;
import net.minecraft.world.ChunkCoordIntPair;
import net.minecraft.world.World;
import net.minecraftforge.client.event.RenderWorldLastEvent;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.InputEvent;
import net.minecraftforge.fml.common.network.FMLNetworkEvent;
Expand All @@ -20,6 +23,9 @@
import org.lwjgl.opengl.GL11;

import java.awt.*;
import java.io.*;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
Expand Down Expand Up @@ -76,6 +82,152 @@ public void renderWorldLastEvent(RenderWorldLastEvent event) {
}
}

@SubscribeEvent
public void clientConnectionToServerEvent(FMLNetworkEvent.ClientConnectedToServerEvent evt) {
if (!evt.isLocal) {
SocketAddress remoteAddress = evt.manager.getRemoteAddress();
if (remoteAddress instanceof InetSocketAddress) {
InetSocketAddress socketAddress = (InetSocketAddress) remoteAddress;
loadLocalStructures(socketAddress.getHostString(), socketAddress.getPort());
}
}
}

private void loadLocalStructures(String host, int port) {
String path = String.format("BBOutlineReloaded%s%s", File.separator, host);
File localStructuresFolder = new File(configManager.configDir, path);
if (!localStructuresFolder.exists()) {
path = String.format("%s,%d", path, port);
localStructuresFolder = new File(configManager.configDir, path);
}
if (!localStructuresFolder.exists())
return;
loadLevelDat(localStructuresFolder);
loadNetherStructures(localStructuresFolder);
loadOverworldStructures(localStructuresFolder);
loadEndStructures(localStructuresFolder);
}

private void loadOverworldStructures(File localStructuresFolder) {
BoundingBoxCache cache = new BoundingBoxCache();
if (configManager.drawDesertTemples.getBoolean()) {
loadStructureNbtFile(localStructuresFolder, cache, "Temple.dat", StructureType.DesertTemple.getColor(), "TeDP");
}
if (configManager.drawJungleTemples.getBoolean()) {
loadStructureNbtFile(localStructuresFolder, cache, "Temple.dat", StructureType.JungleTemple.getColor(), "TeJP");
}
if (configManager.drawWitchHuts.getBoolean()) {
loadStructureNbtFile(localStructuresFolder, cache, "Temple.dat", StructureType.WitchHut.getColor(), "TeSH");
}
if(configManager.drawOceanMonuments.getBoolean()){
loadStructureNbtFile(localStructuresFolder, cache, "Monument.dat", StructureType.OceanMonument.getColor(), "*");
}
if(configManager.drawMineShafts.getBoolean()){
loadStructureNbtFile(localStructuresFolder, cache, "Mineshaft.dat", StructureType.MineShaft.getColor(), "*");
}
if(configManager.drawStrongholds.getBoolean()){
loadStructureNbtFile(localStructuresFolder, cache, "Stronghold.dat", StructureType.Stronghold.getColor(), "*");
}
if(configManager.drawVillages.getBoolean()){
loadVillageNbtFile(localStructuresFolder, cache, "Villages.dat");
}

boundingBoxCacheMap.put(0, cache);
}

private void loadNetherStructures(File localStructuresFolder) {
BoundingBoxCache cache = new BoundingBoxCache();
if(configManager.drawNetherFortresses.getBoolean())
loadStructureNbtFile(localStructuresFolder, cache, "Fortress.dat", StructureType.NetherFortress.getColor(), "*");
if(configManager.drawVillages.getBoolean()){
loadVillageNbtFile(localStructuresFolder, cache, "villages_nether.dat");
}
boundingBoxCacheMap.put(-1, cache);
}

private void loadEndStructures(File localStructuresFolder) {
BoundingBoxCache cache = new BoundingBoxCache();
if(configManager.drawVillages.getBoolean()){
loadVillageNbtFile(localStructuresFolder, cache, "Villages_end.dat");
}
boundingBoxCacheMap.put(1, cache);
}

private void loadVillageNbtFile(File localStructuresFolder, BoundingBoxCache cache, String fileName) {
File file = new File(localStructuresFolder, fileName);
NBTTagCompound nbt = loadNbtFile(file);
if (nbt == null)
return;

NBTTagCompound[] villages = getChildCompoundTags(nbt.getCompoundTag("data"), "Villages");
for (NBTTagCompound village : villages) {
BlockPos center = new BlockPos(village.getInteger("CX"), village.getInteger("CY"), village.getInteger("CZ"));
int radius = village.getInteger("Radius");
int numVillagers = village.getInteger("PopSize");
int numVillageDoors = village.getTagList("Doors", Constants.NBT.TAG_COMPOUND).tagCount();
BoundingBox boundingBox = BoundingBoxVillage.from(center, radius, numVillagers, numVillageDoors);
cache.addBoundingBox(boundingBox);
}

FMLLog.info("Loaded %s (%d villages)", fileName, villages.length);
}

private void loadStructureNbtFile(File localStructuresFolder, BoundingBoxCache cache, String fileName, Color color, String id) {
File file = new File(localStructuresFolder, fileName);
NBTTagCompound nbt = loadNbtFile(file);
if (nbt == null)
return;

NBTTagCompound features = nbt.getCompoundTag("data")
.getCompoundTag("Features");
int loadedStructureCount=0;
for (Object key : features.getKeySet()) {
NBTTagCompound feature = features.getCompoundTag((String) key);
BoundingBox structure = BoundingBoxStructure.from(feature.getIntArray("BB"), color);
Set<BoundingBox> boundingBoxes = new HashSet<BoundingBox>();
NBTTagCompound[] children = getChildCompoundTags(feature, "Children");
for (NBTTagCompound child : children) {
if (id.equals(child.getString("id")) || id.equals("*"))
boundingBoxes.add(BoundingBoxStructure.from(child.getIntArray("BB"), color));
}
if(boundingBoxes.size()>0)
++loadedStructureCount;
cache.addBoundingBox(structure, boundingBoxes);
}

FMLLog.info("Loaded %s (%d structures with type %s)", fileName, loadedStructureCount, id);
}

private NBTTagCompound[] getChildCompoundTags(NBTTagCompound parent, String key) {
NBTTagList tagList = parent.getTagList(key, Constants.NBT.TAG_COMPOUND);
NBTTagCompound[] result = new NBTTagCompound[tagList.tagCount()];
for (int index = 0; index < tagList.tagCount(); index++) {
result[index] = tagList.getCompoundTagAt(index);
}
return result;
}

private void loadLevelDat(File localStructuresFolder) {
File file = new File(localStructuresFolder, "level.dat");
NBTTagCompound nbt = loadNbtFile(file);
if (nbt == null)
return;

NBTTagCompound data = nbt.getCompoundTag("Data");
setWorldData(data.getLong("RandomSeed"), data.getInteger("SpawnX"), data.getInteger("SpawnZ"));
FMLLog.info("Loaded level.dat (seed: %d, spawn: %d,%d)", seed, spawnX, spawnZ);
}

private NBTTagCompound loadNbtFile(File file) {
if (!file.exists())
return null;
try {
return CompressedStreamTools.readCompressed(new FileInputStream(file));
} catch (IOException e) {
}
return null;
}

@SubscribeEvent
public void clientDisconnectionFromServerEvent(FMLNetworkEvent.ClientDisconnectionFromServerEvent evt) {
active = false;
Expand Down
31 changes: 17 additions & 14 deletions java/com/irtimaled/bbor/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.File;

public class ConfigManager {
public Configuration config;
public final File configDir;

public Property showDebugInfo;
public Property fill;
Expand All @@ -27,21 +27,10 @@ public class ConfigManager {
public Property drawWorldSpawn;
public Property worldSpawnMaxY;

private Property SetupBooleanProperty(Configuration config, String category, String configName, Boolean defaultValue, String comment) {
Property property = config.get(category, configName, defaultValue);
property.comment = comment;
property.set(property.getBoolean(defaultValue));
return property;
}

private Property SetupIntegerProperty(Configuration config, String category, String configName, int defaultValue, String comment) {
Property property = config.get(category, configName, defaultValue);
property.comment = comment;
property.set(property.getInt(defaultValue));
return property;
}
private Configuration config;

public ConfigManager(File configDir) {
this.configDir = configDir;
config = new Configuration(new File(configDir, "BBOutlineReloaded.cfg"));
config.load();

Expand All @@ -65,4 +54,18 @@ public ConfigManager(File configDir) {
worldSpawnMaxY = SetupIntegerProperty(config, "features", "worldSpawnMaxY", 0, "The maximum top of the world spawn bounding boxes. If set to -1 it will use the value when activated, if set to 0 it will always track the players feet. (default: 0)");
config.save();
}

private Property SetupBooleanProperty(Configuration config, String category, String configName, Boolean defaultValue, String comment) {
Property property = config.get(category, configName, defaultValue);
property.comment = comment;
property.set(property.getBoolean(defaultValue));
return property;
}

private Property SetupIntegerProperty(Configuration config, String category, String configName, int defaultValue, String comment) {
Property property = config.get(category, configName, defaultValue);
property.comment = comment;
property.set(property.getInt(defaultValue));
return property;
}
}

0 comments on commit 1918012

Please sign in to comment.