Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
irtimaled committed Jan 31, 2015
0 parents commit 0583514
Show file tree
Hide file tree
Showing 17 changed files with 1,176 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
69 changes: 69 additions & 0 deletions java/com/irtimaled/bbor/BoundingBox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.irtimaled.bbor;

import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.BlockPos;

import java.awt.*;

public abstract class BoundingBox {
private final Color color;
private final BlockPos minBlockPos;
private final BlockPos maxBlockPos;

protected BoundingBox(BlockPos minBlockPos, BlockPos maxBlockPos, Color color) {
this.minBlockPos = minBlockPos;
this.maxBlockPos = maxBlockPos;
this.color = color;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + minBlockPos.hashCode();
result = prime * result + maxBlockPos.hashCode();
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
BoundingBox other = (BoundingBox) obj;
if (!minBlockPos.equals(other.minBlockPos))
return false;
if (!maxBlockPos.equals(other.maxBlockPos))
return false;
return true;
}

@Override
public String toString() {
return "(" + minBlockPos.toString() + "; " + maxBlockPos.toString() + ")";
}

public AxisAlignedBB toAxisAlignedBB() {
return AxisAlignedBB.fromBounds(minBlockPos.getX(),
minBlockPos.getY(),
minBlockPos.getZ(),
maxBlockPos.getX(),
maxBlockPos.getY(),
maxBlockPos.getZ());
}

public BlockPos getMinBlockPos() {
return minBlockPos;
}

public BlockPos getMaxBlockPos() {
return maxBlockPos;
}

public Color getColor() {
return color;
}
}
17 changes: 17 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxCache.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.irtimaled.bbor;

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

public class BoundingBoxCache {

protected ConcurrentHashMap<BoundingBox, Set<BoundingBox>> cache = new ConcurrentHashMap<BoundingBox, Set<BoundingBox>>();

public Map<BoundingBox, Set<BoundingBox>> getBoundingBoxes() {
return cache;
}

public synchronized void refresh() {
}
}
51 changes: 51 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxDeserializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.irtimaled.bbor;

import io.netty.buffer.ByteBuf;
import net.minecraft.util.BlockPos;
import net.minecraftforge.fml.common.network.ByteBufUtils;

import java.awt.*;

public class BoundingBoxDeserializer {
public static BoundingBox deserialize(ByteBuf buf) {
char type = (char) ByteBufUtils.readVarShort(buf);
switch (type) {
case 'V':
return deserializeVillage(buf);
case 'S':
return deserializeStructure(buf);
case 'C':
return deserializeSlimeChunk(buf);
}
return null;
}

private static BoundingBox deserializeSlimeChunk(ByteBuf buf) {
BlockPos minBlockPos = deserializeBlockPos(buf);
BlockPos maxBlockPos = deserializeBlockPos(buf);
Color color = new Color(ByteBufUtils.readVarInt(buf, 5));
return BoundingBoxSlimeChunk.from(minBlockPos, maxBlockPos, color);
}

private static BoundingBox deserializeStructure(ByteBuf buf) {
BlockPos minBlockPos = deserializeBlockPos(buf);
BlockPos maxBlockPos = deserializeBlockPos(buf);
Color color = new Color(ByteBufUtils.readVarInt(buf, 5));
return BoundingBoxStructure.from(minBlockPos, maxBlockPos, color);
}

private static BoundingBox deserializeVillage(ByteBuf buf) {
BlockPos center = deserializeBlockPos(buf);
int radius = ByteBufUtils.readVarInt(buf, 5);
boolean spawnsIronGolems = ByteBufUtils.readVarShort(buf) == 1;
Color color = new Color(ByteBufUtils.readVarInt(buf, 5));
return BoundingBoxVillage.from(center, radius, spawnsIronGolems, color);
}

private static BlockPos deserializeBlockPos(ByteBuf buf) {
int x = ByteBufUtils.readVarInt(buf, 5);
int y = ByteBufUtils.readVarInt(buf, 5);
int z = ByteBufUtils.readVarInt(buf, 5);
return new BlockPos(x, y, z);
}
}
58 changes: 58 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.irtimaled.bbor;

import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

import java.util.HashSet;
import java.util.Set;

public class BoundingBoxMessage implements IMessage {
private int dimension;
private BoundingBox key;
private Set<BoundingBox> boundingBoxes;

public static BoundingBoxMessage from(int dimension, BoundingBox key, Set<BoundingBox> boundingBoxes) {
BoundingBoxMessage message = new BoundingBoxMessage();
message.dimension = dimension;
message.key = key;
message.boundingBoxes = boundingBoxes;
return message;
}

@Override
public void fromBytes(ByteBuf buf) {
dimension = ByteBufUtils.readVarShort(buf);
key = BoundingBoxDeserializer.deserialize(buf);
boundingBoxes = new HashSet<BoundingBox>();
while (buf.isReadable()) {
BoundingBox boundingBox = BoundingBoxDeserializer.deserialize(buf);
boundingBoxes.add(boundingBox);
}
if (boundingBoxes.size() == 0)
boundingBoxes.add(key);
}

@Override
public void toBytes(ByteBuf buf) {
ByteBufUtils.writeVarShort(buf, dimension);
BoundingBoxSerializer.serialize(key, buf);
if (boundingBoxes.size() > 1) {
for (BoundingBox boundingBox : boundingBoxes) {
BoundingBoxSerializer.serialize(boundingBox, buf);
}
}
}

public int getDimension() {
return dimension;
}

public BoundingBox getKey() {
return key;
}

public Set<BoundingBox> getBoundingBoxes() {
return boundingBoxes;
}
}
22 changes: 22 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxMessageHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.irtimaled.bbor;

import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

import java.util.Map;

public class BoundingBoxMessageHandler implements IMessageHandler<BoundingBoxMessage, IMessage> {
@Override
public IMessage onMessage(BoundingBoxMessage message, MessageContext ctx) {

Map<Integer, BoundingBoxCache> boundingBoxCacheMap = BoundingBoxOutlineReloaded.proxy.boundingBoxCacheMap;
int dimension = message.getDimension();
if (!boundingBoxCacheMap.containsKey(dimension)) {
boundingBoxCacheMap.put(dimension, new BoundingBoxCache());
}

boundingBoxCacheMap.get(dimension).getBoundingBoxes().put(message.getKey(), message.getBoundingBoxes());
return null;
}
}
45 changes: 45 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxOutlineReloaded.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.irtimaled.bbor;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;


@Mod(modid = BoundingBoxOutlineReloaded.MODID, name = BoundingBoxOutlineReloaded.NAME, version = BoundingBoxOutlineReloaded.VERSION)
public class BoundingBoxOutlineReloaded {

public static final String MODID = "bbor";
public static final String NAME = "Bounding Box Outline Reloaded";
public static final String VERSION = "1.0.0";

private ConfigManager configManager;

public SimpleNetworkWrapper network;

@Mod.Instance()
public static BoundingBoxOutlineReloaded instance;

@SidedProxy(clientSide = "ClientProxy", serverSide = "ServerProxy")
public static CommonProxy proxy;


@EventHandler
public void preInit(FMLPreInitializationEvent evt) {
configManager = new ConfigManager(evt.getModConfigurationDirectory());
}

@EventHandler
public void load(FMLInitializationEvent evt) {
MinecraftForge.EVENT_BUS.register(proxy);
FMLCommonHandler.instance().bus().register(proxy);

proxy.configManager = configManager;
proxy.init();
}
}

111 changes: 111 additions & 0 deletions java/com/irtimaled/bbor/BoundingBoxSerializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package com.irtimaled.bbor;

import io.netty.buffer.ByteBuf;
import net.minecraft.util.BlockPos;
import net.minecraftforge.fml.common.network.ByteBufUtils;

import java.awt.*;

public class BoundingBoxSerializer {

public static void serialize(BoundingBox boundingBox, ByteBuf buf) {
if (boundingBox instanceof BoundingBoxSlimeChunk) {
serializeSlimeChunk((BoundingBoxSlimeChunk) boundingBox, buf);
} else if (boundingBox instanceof BoundingBoxVillage) {
serializeVillage((BoundingBoxVillage) boundingBox, buf);
} else if (boundingBox instanceof BoundingBoxStructure) {
serializeStructure((BoundingBoxStructure) boundingBox, buf);
}
}


private static void serializeVillage(BoundingBoxVillage boundingBox, ByteBuf buf) {
ByteBufUtils.writeVarShort(buf, 'V');
serializeBlockPos(boundingBox.getCenter(), buf);
ByteBufUtils.writeVarInt(buf, boundingBox.getRadius(), 5);
ByteBufUtils.writeVarShort(buf, boundingBox.getSpawnsIronGolems() ? 1 : 0);
serializeColor(boundingBox.getColor(), buf);
}

private static void serializeStructure(BoundingBoxStructure boundingBox, ByteBuf buf) {
ByteBufUtils.writeVarShort(buf, 'S');
serializeCuboid(boundingBox, buf);
serializeColor(boundingBox.getColor(), buf);
}

private static void serializeSlimeChunk(BoundingBoxSlimeChunk boundingBox, ByteBuf buf) {
ByteBufUtils.writeVarShort(buf, 'C');
serializeCuboid(boundingBox, buf);
serializeColor(boundingBox.getColor(), buf);
}

private static void serializeColor(Color color, ByteBuf buf) {
ByteBufUtils.writeVarInt(buf, color.getRGB(), 5);
}

private static void serializeCuboid(BoundingBox boundingBox, ByteBuf buf) {
serializeBlockPos(boundingBox.getMinBlockPos(), buf);
serializeBlockPos(boundingBox.getMaxBlockPos(), buf);
}

private static void serializeBlockPos(BlockPos blockPos, ByteBuf buf) {
ByteBufUtils.writeVarInt(buf, blockPos.getX(), 5);
ByteBufUtils.writeVarInt(buf, blockPos.getY(), 5);
ByteBufUtils.writeVarInt(buf, blockPos.getZ(), 5);
}
/*
public static void serialize(BoundingBox boundingBox, StringBuilder sb) {
if (boundingBox instanceof BoundingBoxSlimeChunk) {
serializeSlimeChunk((BoundingBoxSlimeChunk) boundingBox, sb);
} else if (boundingBox instanceof BoundingBoxVillage) {
serializeVillage((BoundingBoxVillage) boundingBox, sb);
} else if (boundingBox instanceof BoundingBoxStructure) {
serializeStructure((BoundingBoxStructure) boundingBox, sb);
}
}
private static void serializeVillage(BoundingBoxVillage boundingBox, StringBuilder sb) {
sb.append("V/");
serializeBlockPos(boundingBox.getCenter(), sb);
sb.append('/');
sb.append(boundingBox.getRadius());
sb.append('/');
sb.append(boundingBox.getSpawnsIronGolems());
sb.append('/');
serializeColor(boundingBox.getColor(), sb);
}
private static void serializeStructure(BoundingBoxStructure boundingBox, StringBuilder sb) {
sb.append("S/");
serializeCuboid(boundingBox, sb);
sb.append('/');
serializeColor(boundingBox.getColor(), sb);
}
private static void serializeSlimeChunk(BoundingBoxSlimeChunk boundingBox, StringBuilder sb) {
sb.append("SC/");
serializeCuboid(boundingBox, sb);
sb.append('/');
serializeColor(boundingBox.getColor(), sb);
}
private static void serializeColor(Color color, StringBuilder sb) {
sb.append(color.getRGB());
}
private static void serializeCuboid(BoundingBox boundingBox, StringBuilder sb) {
serializeBlockPos(boundingBox.getMinBlockPos(), sb);
sb.append('/');
serializeBlockPos(boundingBox.getMaxBlockPos(), sb);
}
private static void serializeBlockPos(BlockPos blockPos, StringBuilder sb) {
sb.append(blockPos.getX());
sb.append(',');
sb.append(blockPos.getY());
sb.append(',');
sb.append(blockPos.getZ());
}*/
}
Loading

0 comments on commit 0583514

Please sign in to comment.