mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-18 19:29:31 +00:00
Basic power system logic.
This commit is contained in:
1 parent
c244a12635
commit
7af69602d3
3 files changed
+116
No files matched your search
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* This class was created by <Vazkii>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under a
|
||||
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
|
||||
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
|
||||
*
|
||||
* File Created @ [Jan 22, 2014, 5:35:10 PM (GMT)]
|
||||
*/
|
||||
package vazkii.botania.common.core.handler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.ChunkCoordinates;
|
||||
import net.minecraftforge.event.ForgeSubscribe;
|
||||
import vazkii.botania.api.internal.ManaNetworkEvent;
|
||||
import vazkii.botania.api.internal.ManaNetworkEvent.Action;
|
||||
import vazkii.botania.api.internal.ManaNetworkEvent.ManaBlockType;
|
||||
import vazkii.botania.common.core.helper.MathHelper;
|
||||
|
||||
public final class ManaNetworkHandler {
|
||||
|
||||
public static final ManaNetworkHandler instance = new ManaNetworkHandler();
|
||||
|
||||
public Map<Integer, List<TileEntity>> manaPools = new HashMap();
|
||||
public Map<Integer, List<TileEntity>> manaCollectors = new HashMap();
|
||||
|
||||
@ForgeSubscribe
|
||||
public void onNetworkEvent(ManaNetworkEvent event) {
|
||||
Map<Integer, List<TileEntity>> map = event.type == ManaBlockType.COLLECTOR ? manaCollectors : manaPools;
|
||||
if(event.action == Action.ADD)
|
||||
add(map, event.tile);
|
||||
else remove(map, event.tile);
|
||||
}
|
||||
|
||||
public TileEntity getClosestPool(ChunkCoordinates pos, int dimension, int limit) {
|
||||
if(manaPools.containsKey(dimension))
|
||||
return getClosest(manaPools.get(dimension), pos, limit);
|
||||
return null;
|
||||
}
|
||||
|
||||
public TileEntity getClosestCollector(ChunkCoordinates pos, int dimension, int limit) {
|
||||
if(manaCollectors.containsKey(dimension))
|
||||
return getClosest(manaCollectors.get(dimension), pos, limit);
|
||||
return null;
|
||||
}
|
||||
|
||||
private TileEntity getClosest(List<TileEntity> tiles, ChunkCoordinates pos, int limit) {
|
||||
float closest = Float.MAX_VALUE;
|
||||
TileEntity closestTile = null;
|
||||
|
||||
for(TileEntity tile : tiles) {
|
||||
float distance = MathHelper.pointDistanceSpace(tile.xCoord, tile.yCoord, tile.zCoord, pos.posX, pos.posY, pos.posZ);
|
||||
if(distance > limit)
|
||||
continue;
|
||||
|
||||
if(distance < closest) {
|
||||
closest = distance;
|
||||
closestTile = tile;
|
||||
}
|
||||
}
|
||||
|
||||
return closestTile;
|
||||
}
|
||||
|
||||
private void remove(Map<Integer, List<TileEntity>> map, TileEntity tile) {
|
||||
int dim = tile.worldObj.provider.dimensionId;
|
||||
|
||||
if(!map.containsKey(dim))
|
||||
return;
|
||||
|
||||
List<TileEntity> tiles = map.get(dim);
|
||||
tiles.remove(tile);
|
||||
}
|
||||
|
||||
private void add(Map<Integer, List<TileEntity>> map, TileEntity tile) {
|
||||
int dim = tile.worldObj.provider.dimensionId;
|
||||
|
||||
List<TileEntity> tiles;
|
||||
if(!map.containsKey(dim))
|
||||
map.put(dim, new ArrayList());
|
||||
|
||||
tiles = map.get(dim);
|
||||
|
||||
if(!tiles.contains(tile))
|
||||
tiles.add(tile);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* This class was created by <Vazkii>. It's distributed as
|
||||
* part of the Botania Mod. Get the Source Code in github:
|
||||
* https://github.com/Vazkii/Botania
|
||||
*
|
||||
* Botania is Open Source and distributed under a
|
||||
* Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License
|
||||
* (http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_GB)
|
||||
*
|
||||
* File Created @ [Jan 22, 2014, 5:49:50 PM (GMT)]
|
||||
*/
|
||||
package vazkii.botania.common.core.helper;
|
||||
|
||||
public final class MathHelper {
|
||||
|
||||
public static float pointDistanceSpace(double x1, double y1, double z1, double x2, double y2, double z2) {
|
||||
return (float) Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2) + Math.pow(z1 - z2, 2));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import vazkii.botania.common.block.ModBlocks;
|
||||
import vazkii.botania.common.core.handler.BiomeDecorationHandler;
|
||||
import vazkii.botania.common.core.handler.ConfigHandler;
|
||||
import vazkii.botania.common.core.handler.InternalMethodHandler;
|
||||
import vazkii.botania.common.core.handler.ManaNetworkHandler;
|
||||
import vazkii.botania.common.crafting.ModCrafingRecipes;
|
||||
import vazkii.botania.common.crafting.ModPetalRecipes;
|
||||
import vazkii.botania.common.item.ModItems;
|
||||
@@ -50,6 +51,7 @@ public class CommonProxy {
|
||||
NetworkRegistry.instance().registerGuiHandler(Botania.instance, new GuiHandler());
|
||||
|
||||
MinecraftForge.TERRAIN_GEN_BUS.register(new BiomeDecorationHandler());
|
||||
MinecraftForge.EVENT_BUS.register(new ManaNetworkHandler());
|
||||
}
|
||||
|
||||
public void postInit(FMLPostInitializationEvent event) {
|
||||
|
||||
Reference in new issue
Block a user