Relocate Source to proper directory.
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import net.minecraft.entity.passive.EntityVillager;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.village.MerchantRecipe;
|
||||
import net.minecraft.village.MerchantRecipeList;
|
||||
import appeng.api.AEApi;
|
||||
import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;
|
||||
|
||||
public class AETrading implements IVillageTradeHandler
|
||||
{
|
||||
|
||||
private void addToList(MerchantRecipeList l, ItemStack a, ItemStack b)
|
||||
{
|
||||
if ( a.stackSize < 1 )
|
||||
a.stackSize = 1;
|
||||
if ( b.stackSize < 1 )
|
||||
b.stackSize = 1;
|
||||
|
||||
if ( a.stackSize > a.getMaxStackSize() )
|
||||
a.stackSize = a.getMaxStackSize();
|
||||
if ( b.stackSize > b.getMaxStackSize() )
|
||||
b.stackSize = b.getMaxStackSize();
|
||||
|
||||
l.add( new MerchantRecipe( a, b ) );
|
||||
}
|
||||
|
||||
private void addTrade(MerchantRecipeList list, ItemStack a, ItemStack b, Random rand, int conversion_Variance)
|
||||
{
|
||||
// Sell
|
||||
ItemStack From = a.copy();
|
||||
ItemStack To = b.copy();
|
||||
|
||||
From.stackSize = 1 + (Math.abs( rand.nextInt() ) % (1 + conversion_Variance));
|
||||
To.stackSize = 1;
|
||||
|
||||
addToList( list, From, To );
|
||||
}
|
||||
|
||||
private void addMerchant(MerchantRecipeList list, ItemStack item, int emera, Random rand, int greed)
|
||||
{
|
||||
if ( item == null )
|
||||
return;
|
||||
|
||||
// Sell
|
||||
ItemStack From = item.copy();
|
||||
ItemStack To = new ItemStack( Items.emerald );
|
||||
|
||||
int multiplier = (Math.abs( rand.nextInt() ) % 6);
|
||||
emera += (Math.abs( rand.nextInt() ) % greed) - multiplier;
|
||||
int mood = rand.nextInt() % 2;
|
||||
|
||||
From.stackSize = multiplier + mood;
|
||||
To.stackSize = multiplier * emera - mood;
|
||||
|
||||
if ( To.stackSize < 0 )
|
||||
{
|
||||
From.stackSize -= To.stackSize;
|
||||
To.stackSize -= To.stackSize;
|
||||
}
|
||||
|
||||
addToList( list, From, To );
|
||||
|
||||
// Buy
|
||||
ItemStack reverseTo = From.copy();
|
||||
ItemStack reverseFrom = To.copy();
|
||||
|
||||
reverseFrom.stackSize = (int) (reverseFrom.stackSize * (rand.nextFloat() * 3.0f + 1.0f));
|
||||
reverseTo.stackSize = reverseTo.stackSize;
|
||||
|
||||
addToList( list, reverseFrom, reverseTo );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void manipulateTradesForVillager(EntityVillager villager, MerchantRecipeList recipeList, Random random)
|
||||
{
|
||||
addMerchant( recipeList, AEApi.instance().materials().materialSilicon.stack( 1 ), 1, random, 2 );
|
||||
addMerchant( recipeList, AEApi.instance().materials().materialCertusQuartzCrystal.stack( 1 ), 2, random, 4 );
|
||||
addMerchant( recipeList, AEApi.instance().materials().materialCertusQuartzDust.stack( 1 ), 1, random, 3 );
|
||||
|
||||
addTrade( recipeList, AEApi.instance().materials().materialCertusQuartzDust.stack( 1 ),
|
||||
AEApi.instance().materials().materialCertusQuartzCrystal.stack( 1 ), random, 2 );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.sync.network.NetworkHandler;
|
||||
import appeng.core.sync.packets.PacketCompassRequest;
|
||||
|
||||
public class CompassManager
|
||||
{
|
||||
|
||||
public static CompassManager instance = new CompassManager();
|
||||
|
||||
class CompassReq
|
||||
{
|
||||
|
||||
final int hash;
|
||||
|
||||
final long attunement;
|
||||
final int cx, cdy, cz;
|
||||
|
||||
public CompassReq(long attunement, int x, int y, int z) {
|
||||
this.attunement = attunement;
|
||||
cx = x >> 4;
|
||||
cdy = y >> 5;
|
||||
cz = z >> 4;
|
||||
hash = ((Integer) cx).hashCode() ^ ((Integer) cdy).hashCode() ^ ((Integer) cz).hashCode() ^ ((Long) attunement).hashCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
CompassReq b = (CompassReq) obj;
|
||||
return attunement == b.attunement && cx == b.cx && cdy == b.cdy && cz == b.cz;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
HashMap<CompassReq, CompassResult> reqs = new HashMap();
|
||||
|
||||
public void postResult(long attunement, int x, int y, int z, CompassResult res)
|
||||
{
|
||||
CompassReq r = new CompassReq( attunement, x, y, z );
|
||||
reqs.put( r, res );
|
||||
}
|
||||
|
||||
public CompassResult getCompassDirection(long attunement, int x, int y, int z)
|
||||
{
|
||||
long now = System.currentTimeMillis();
|
||||
|
||||
Iterator<CompassResult> i = reqs.values().iterator();
|
||||
while (i.hasNext())
|
||||
{
|
||||
CompassResult res = i.next();
|
||||
long diff = now - res.time;
|
||||
if ( diff > 20000 )
|
||||
i.remove();
|
||||
}
|
||||
|
||||
CompassReq r = new CompassReq( attunement, x, y, z );
|
||||
CompassResult res = reqs.get( r );
|
||||
|
||||
if ( res == null )
|
||||
{
|
||||
res = new CompassResult( false, true, 0 );
|
||||
reqs.put( r, res );
|
||||
requestUpdate( r );
|
||||
}
|
||||
else if ( now - res.time > 1000 * 3 )
|
||||
{
|
||||
if ( !res.requested )
|
||||
{
|
||||
res.requested = true;
|
||||
requestUpdate( r );
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private void requestUpdate(CompassReq r)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
NetworkHandler.instance.sendToServer( new PacketCompassRequest( r.attunement, r.cx, r.cz, r.cdy ) );
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package appeng.hooks;
|
||||
|
||||
public class CompassResult
|
||||
{
|
||||
|
||||
public final boolean hasResult;
|
||||
public final boolean spin;
|
||||
public final double rad;
|
||||
public final long time;
|
||||
|
||||
public boolean requested = false;
|
||||
|
||||
public CompassResult(boolean hasResult, boolean spin, double rad) {
|
||||
this.hasResult = hasResult;
|
||||
this.spin = spin;
|
||||
this.rad = rad;
|
||||
this.time = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import net.minecraft.block.BlockDispenser;
|
||||
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
|
||||
import net.minecraft.dispenser.IBlockSource;
|
||||
import net.minecraft.entity.EntityLiving;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.world.World;
|
||||
import appeng.entity.EntityTinyTNTPrimed;
|
||||
|
||||
final public class DispenserBehaviorTinyTNT extends BehaviorDefaultDispenseItem
|
||||
{
|
||||
|
||||
@Override
|
||||
protected ItemStack dispenseStack(IBlockSource dispenser, ItemStack dispensedItem)
|
||||
{
|
||||
EnumFacing enumfacing = BlockDispenser.func_149937_b( dispenser.getBlockMetadata() );
|
||||
World world = dispenser.getWorld();
|
||||
int i = dispenser.getXInt() + enumfacing.getFrontOffsetX();
|
||||
int j = dispenser.getYInt() + enumfacing.getFrontOffsetY();
|
||||
int k = dispenser.getZInt() + enumfacing.getFrontOffsetZ();
|
||||
EntityTinyTNTPrimed entitytntprimed = new EntityTinyTNTPrimed( world, (double) ((float) i + 0.5F), (double) ((float) j + 0.5F),
|
||||
(double) ((float) k + 0.5F), (EntityLiving) null );
|
||||
world.spawnEntityInWorld( entitytntprimed );
|
||||
--dispensedItem.stackSize;
|
||||
return dispensedItem;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import net.minecraft.block.BlockDispenser;
|
||||
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
|
||||
import net.minecraft.dispenser.IBlockSource;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import appeng.util.Platform;
|
||||
|
||||
final public class DispenserBlockTool extends BehaviorDefaultDispenseItem
|
||||
{
|
||||
|
||||
@Override
|
||||
protected ItemStack dispenseStack(IBlockSource dispenser, ItemStack dispensedItem)
|
||||
{
|
||||
Item i = dispensedItem.getItem();
|
||||
if ( i instanceof IBlockTool )
|
||||
{
|
||||
EnumFacing enumfacing = BlockDispenser.func_149937_b( dispenser.getBlockMetadata() );
|
||||
IBlockTool tm = (IBlockTool) i;
|
||||
|
||||
World w = dispenser.getWorld();
|
||||
if ( w instanceof WorldServer )
|
||||
{
|
||||
int x = dispenser.getXInt() + enumfacing.getFrontOffsetX();
|
||||
int y = dispenser.getYInt() + enumfacing.getFrontOffsetY();
|
||||
int z = dispenser.getZInt() + enumfacing.getFrontOffsetZ();
|
||||
|
||||
tm.onItemUse( dispensedItem, Platform.getPlayer( (WorldServer) w ), w, x, y, z, enumfacing.ordinal(), 0.5f, 0.5f, 0.5f );
|
||||
}
|
||||
}
|
||||
return dispensedItem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import net.minecraft.block.BlockDispenser;
|
||||
import net.minecraft.dispenser.BehaviorDefaultDispenseItem;
|
||||
import net.minecraft.dispenser.IBlockSource;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraftforge.common.util.ForgeDirection;
|
||||
import appeng.items.tools.powered.ToolMassCannon;
|
||||
import appeng.util.Platform;
|
||||
|
||||
final public class DispenserMatterCannon extends BehaviorDefaultDispenseItem
|
||||
{
|
||||
|
||||
@Override
|
||||
protected ItemStack dispenseStack(IBlockSource dispenser, ItemStack dispensedItem)
|
||||
{
|
||||
Item i = dispensedItem.getItem();
|
||||
if ( i instanceof ToolMassCannon )
|
||||
{
|
||||
EnumFacing enumfacing = BlockDispenser.func_149937_b( dispenser.getBlockMetadata() );
|
||||
ForgeDirection dir = ForgeDirection.UNKNOWN;
|
||||
for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS)
|
||||
{
|
||||
if ( enumfacing.getFrontOffsetX() == d.offsetX && enumfacing.getFrontOffsetY() == d.offsetY && enumfacing.getFrontOffsetZ() == d.offsetZ )
|
||||
dir = d;
|
||||
}
|
||||
|
||||
ToolMassCannon tm = (ToolMassCannon) i;
|
||||
|
||||
World w = dispenser.getWorld();
|
||||
if ( w instanceof WorldServer )
|
||||
{
|
||||
EntityPlayer p = Platform.getPlayer( (WorldServer) w );
|
||||
Platform.configurePlayer( p, dir, dispenser.getBlockTileEntity() );
|
||||
|
||||
p.posX += dir.offsetX;
|
||||
p.posY += dir.offsetY;
|
||||
p.posZ += dir.offsetZ;
|
||||
|
||||
dispensedItem = tm.onItemRightClick( dispensedItem, w, p );
|
||||
}
|
||||
}
|
||||
return dispensedItem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
public interface IBlockTool
|
||||
{
|
||||
|
||||
boolean onItemUse(ItemStack dispensedItem, EntityPlayer player, World w, int x, int y, int z, int ordinal, float hitx, float hity, float hitz);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.IChunkProvider;
|
||||
import appeng.api.features.IWorldGen.WorldGenType;
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.core.WorldSettings;
|
||||
import appeng.core.features.registries.WorldGenRegistry;
|
||||
import appeng.helpers.MeteoritePlacer;
|
||||
import appeng.services.helpers.ICompassCallback;
|
||||
import appeng.util.Platform;
|
||||
import cpw.mods.fml.common.IWorldGenerator;
|
||||
|
||||
final public class MeteoriteWorldGen implements IWorldGenerator
|
||||
{
|
||||
|
||||
class myGen implements ICompassCallback
|
||||
{
|
||||
|
||||
double distance = 0;
|
||||
|
||||
@Override
|
||||
public void calculatedDirection(boolean hasResult, boolean spin, double radians, double dist)
|
||||
{
|
||||
if ( hasResult )
|
||||
distance = dist;
|
||||
else
|
||||
distance = Double.MAX_VALUE;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@Override
|
||||
public void generate(Random r, int chunkX, int chunkZ, World w, IChunkProvider chunkGenerator, IChunkProvider chunkProvider)
|
||||
{
|
||||
if ( WorldGenRegistry.instance.isWorldGenEnabled( WorldGenType.Meteorites, w ) )
|
||||
{
|
||||
// add new meteorites?
|
||||
if ( r.nextFloat() < AEConfig.instance.meteoriteSpawnChance )
|
||||
{
|
||||
int x = r.nextInt( 16 ) + (chunkX << 4);
|
||||
int z = r.nextInt( 16 ) + (chunkZ << 4);
|
||||
|
||||
int depth = 180 + r.nextInt( 20 );
|
||||
TickHandler.instance.addCallable( w, new MeteoriteSpawn( x, depth, z, w ) );
|
||||
}
|
||||
else
|
||||
TickHandler.instance.addCallable( w, new MeteoriteSpawn( chunkX << 4, 128, chunkZ << 4, w ) );
|
||||
}
|
||||
else
|
||||
WorldSettings.getInstance().getCompass().updateArea( w, chunkX, chunkZ );
|
||||
}
|
||||
|
||||
class MeteoriteSpawn implements Callable
|
||||
{
|
||||
|
||||
final int x;
|
||||
final int z;
|
||||
final World w;
|
||||
int depth;
|
||||
|
||||
public MeteoriteSpawn(int x, int depth, int z, World w) {
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
this.w = w;
|
||||
this.depth = depth;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object call() throws Exception
|
||||
{
|
||||
int chunkX = x >> 4;
|
||||
int chunkZ = z >> 4;
|
||||
|
||||
double minSqDist = Double.MAX_VALUE;
|
||||
|
||||
// near by meteorites!
|
||||
for (NBTTagCompound data : getNearByMeteorites( w, chunkX, chunkZ ))
|
||||
{
|
||||
MeteoritePlacer mp = new MeteoritePlacer();
|
||||
mp.spawnMeteorite( new MeteoritePlacer.ChunkOnly( w, chunkX, chunkZ ), data );
|
||||
|
||||
minSqDist = Math.min( minSqDist, mp.getSqDistance( x, z ) );
|
||||
}
|
||||
|
||||
boolean isCluster = (minSqDist < 30 * 30) && Platform.getRandomFloat() < AEConfig.instance.meteoriteClusterChance;
|
||||
|
||||
if ( minSqDist > AEConfig.instance.minMeteoriteDistanceSq || isCluster )
|
||||
tryMeteorite( w, depth, x, z );
|
||||
|
||||
WorldSettings.getInstance().setGenerated( w.provider.dimensionId, chunkX, chunkZ );
|
||||
WorldSettings.getInstance().getCompass().updateArea( w, chunkX, chunkZ );
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean tryMeteorite(World w, int depth, int x, int z)
|
||||
{
|
||||
for (int tries = 0; tries < 20; tries++)
|
||||
{
|
||||
MeteoritePlacer mp = new MeteoritePlacer();
|
||||
|
||||
if ( mp.spawnMeteorite( new MeteoritePlacer.ChunkOnly( w, x >> 4, z >> 4 ), x, depth, z ) )
|
||||
{
|
||||
int px = x >> 4;
|
||||
int pz = z >> 4;
|
||||
|
||||
for (int cx = px - 6; cx < px + 6; cx++)
|
||||
for (int cz = pz - 6; cz < pz + 6; cz++)
|
||||
{
|
||||
if ( w.getChunkProvider().chunkExists( cx, cz ) )
|
||||
{
|
||||
if ( px == cx && pz == cz )
|
||||
continue;
|
||||
|
||||
if ( WorldSettings.getInstance().hasGenerated( w.provider.dimensionId, cx, cz ) )
|
||||
{
|
||||
MeteoritePlacer mp2 = new MeteoritePlacer();
|
||||
mp2.spawnMeteorite( new MeteoritePlacer.ChunkOnly( w, cx, cz ), mp.getSettings() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
depth -= 15;
|
||||
if ( depth < 40 )
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private Collection<NBTTagCompound> getNearByMeteorites(World w, int chunkX, int chunkZ)
|
||||
{
|
||||
return WorldSettings.getInstance().getNearByMeteorites( w.provider.dimensionId, chunkX, chunkZ );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.init.Blocks;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.IChunkProvider;
|
||||
import net.minecraft.world.gen.feature.WorldGenMinable;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.features.IWorldGen.WorldGenType;
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.core.features.registries.WorldGenRegistry;
|
||||
import cpw.mods.fml.common.IWorldGenerator;
|
||||
|
||||
final public class QuartzWorldGen implements IWorldGenerator
|
||||
{
|
||||
|
||||
final WorldGenMinable oreNormal;
|
||||
final WorldGenMinable oreCharged;
|
||||
|
||||
public QuartzWorldGen() {
|
||||
Block normal = AEApi.instance().blocks().blockQuartzOre.block();
|
||||
Block charged = AEApi.instance().blocks().blockQuartzOreCharged.block();
|
||||
|
||||
if ( normal != null && charged != null )
|
||||
{
|
||||
oreNormal = new WorldGenMinable( normal, 0, AEConfig.instance.quartzOresPerCluster, Blocks.stone );
|
||||
oreCharged = new WorldGenMinable( charged, 0, AEConfig.instance.quartzOresPerCluster, Blocks.stone );
|
||||
}
|
||||
else
|
||||
oreNormal = oreCharged = null;
|
||||
};
|
||||
|
||||
@Override
|
||||
public void generate(Random r, int chunkX, int chunkZ, World w, IChunkProvider chunkGenerator, IChunkProvider chunkProvider)
|
||||
{
|
||||
int sealevel = w.provider.getAverageGroundLevel() + 1;
|
||||
|
||||
if ( sealevel < 20 )
|
||||
{
|
||||
int x = (chunkX << 4) + 8;
|
||||
int z = (chunkZ << 4) + 8;
|
||||
sealevel = w.getHeightValue( x, z );
|
||||
}
|
||||
|
||||
if ( oreNormal == null || oreCharged == null )
|
||||
return;
|
||||
|
||||
double oreDepthMultiplier = AEConfig.instance.quartzOresClusterAmount * sealevel / 64;
|
||||
int scale = (int) Math.round( r.nextGaussian() * Math.sqrt( oreDepthMultiplier ) + oreDepthMultiplier );
|
||||
|
||||
for (int x = 0; x < (r.nextBoolean() ? scale * 2 : scale) / 2; ++x)
|
||||
{
|
||||
boolean isCharged = r.nextFloat() > AEConfig.instance.spawnChargedChance;
|
||||
WorldGenMinable whichOre = isCharged ? oreCharged : oreNormal;
|
||||
|
||||
if ( WorldGenRegistry.instance.isWorldGenEnabled( isCharged ? WorldGenType.ChargedCertusQuartz : WorldGenType.CertusQuartz, w ) )
|
||||
{
|
||||
int cx = chunkX * 16 + r.nextInt( 22 );
|
||||
int cy = r.nextInt( 40 * sealevel / 64 ) + r.nextInt( 22 * sealevel / 64 ) + 12 * sealevel / 64;
|
||||
int cz = chunkZ * 16 + r.nextInt( 22 );
|
||||
whichOre.generate( w, r, cx, cy, cz );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
package appeng.hooks;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraftforge.event.world.ChunkEvent;
|
||||
import net.minecraftforge.event.world.WorldEvent;
|
||||
import appeng.api.AEApi;
|
||||
import appeng.api.networking.IGridNode;
|
||||
import appeng.api.parts.CableRenderMode;
|
||||
import appeng.api.util.AEColor;
|
||||
import appeng.core.AEConfig;
|
||||
import appeng.core.AELog;
|
||||
import appeng.core.CommonHelper;
|
||||
import appeng.core.sync.packets.PacketPaintedEntity;
|
||||
import appeng.crafting.CraftingJob;
|
||||
import appeng.entity.EntityFloatingItem;
|
||||
import appeng.me.Grid;
|
||||
import appeng.me.NetworkList;
|
||||
import appeng.tile.AEBaseTile;
|
||||
import appeng.util.Platform;
|
||||
|
||||
import com.google.common.base.Stopwatch;
|
||||
import com.google.common.collect.LinkedListMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
|
||||
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.Phase;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.Type;
|
||||
import cpw.mods.fml.common.gameevent.TickEvent.WorldTickEvent;
|
||||
|
||||
public class TickHandler
|
||||
{
|
||||
|
||||
class HandlerRep
|
||||
{
|
||||
|
||||
public Queue<AEBaseTile> tiles = new LinkedList();
|
||||
|
||||
public Collection<Grid> networks = new NetworkList();
|
||||
|
||||
public void clear()
|
||||
{
|
||||
tiles = new LinkedList();
|
||||
networks = new NetworkList();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
final public static TickHandler instance = new TickHandler();
|
||||
|
||||
final private WeakHashMap<World, Queue<Callable>> callQueue = new WeakHashMap<World, Queue<Callable>>();
|
||||
Queue<Callable> serverQueue = new LinkedList<Callable>();
|
||||
|
||||
final private HandlerRep server = new HandlerRep();
|
||||
final private HandlerRep client = new HandlerRep();
|
||||
|
||||
static public class PlayerColor
|
||||
{
|
||||
|
||||
public final AEColor myColor;
|
||||
protected final int myEntity;
|
||||
protected int ticksLeft;
|
||||
|
||||
public PacketPaintedEntity getPacket()
|
||||
{
|
||||
return new PacketPaintedEntity( myEntity, myColor, ticksLeft );
|
||||
}
|
||||
|
||||
public PlayerColor(int id, AEColor col, int ticks) {
|
||||
myEntity = id;
|
||||
myColor = col;
|
||||
ticksLeft = ticks;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
final private HashMap<Integer, PlayerColor> cliPlayerColors = new HashMap();
|
||||
final private HashMap<Integer, PlayerColor> srvPlayerColors = new HashMap();
|
||||
|
||||
public HashMap<Integer, PlayerColor> getPlayerColors()
|
||||
{
|
||||
if ( Platform.isServer() )
|
||||
return srvPlayerColors;
|
||||
return cliPlayerColors;
|
||||
}
|
||||
|
||||
private void tickColors(HashMap<Integer, PlayerColor> playerSet)
|
||||
{
|
||||
Iterator<PlayerColor> i = playerSet.values().iterator();
|
||||
while (i.hasNext())
|
||||
{
|
||||
PlayerColor pc = i.next();
|
||||
if ( pc.ticksLeft-- <= 0 )
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
|
||||
HandlerRep getRepo()
|
||||
{
|
||||
if ( Platform.isServer() )
|
||||
return server;
|
||||
return client;
|
||||
}
|
||||
|
||||
public void addCallable(World w, Callable c)
|
||||
{
|
||||
if ( w == null )
|
||||
serverQueue.add( c );
|
||||
else
|
||||
{
|
||||
Queue<Callable> queue = callQueue.get( w );
|
||||
|
||||
if ( queue == null )
|
||||
callQueue.put( w, queue = new LinkedList<Callable>() );
|
||||
|
||||
queue.add( c );
|
||||
}
|
||||
}
|
||||
|
||||
public void addInit(AEBaseTile tile)
|
||||
{
|
||||
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
||||
getRepo().tiles.add( tile );
|
||||
}
|
||||
|
||||
public void addNetwork(Grid grid)
|
||||
{
|
||||
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
||||
getRepo().networks.add( grid );
|
||||
}
|
||||
|
||||
public void removeNetwork(Grid grid)
|
||||
{
|
||||
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
||||
getRepo().networks.remove( grid );
|
||||
}
|
||||
|
||||
public Iterable<Grid> getGridList()
|
||||
{
|
||||
return getRepo().networks;
|
||||
}
|
||||
|
||||
public void shutdown()
|
||||
{
|
||||
getRepo().clear();
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void unloadWorld(WorldEvent.Unload ev)
|
||||
{
|
||||
if ( Platform.isServer() ) // for no there is no reason to care about this on the client...
|
||||
{
|
||||
LinkedList<IGridNode> toDestroy = new LinkedList();
|
||||
|
||||
for (Grid g : getRepo().networks)
|
||||
{
|
||||
for (IGridNode n : g.getNodes())
|
||||
{
|
||||
if ( n.getWorld() == ev.world )
|
||||
toDestroy.add( n );
|
||||
}
|
||||
}
|
||||
|
||||
for (IGridNode n : toDestroy)
|
||||
n.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onChunkLoad(ChunkEvent.Load load)
|
||||
{
|
||||
for (Object te : load.getChunk().chunkTileEntityMap.values())
|
||||
{
|
||||
if ( te instanceof AEBaseTile )
|
||||
{
|
||||
((AEBaseTile) te).onChunkLoad();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
CableRenderMode crm = CableRenderMode.Standard;
|
||||
|
||||
@SubscribeEvent
|
||||
public void onTick(TickEvent ev)
|
||||
{
|
||||
|
||||
if ( ev.type == Type.CLIENT && ev.phase == Phase.START )
|
||||
{
|
||||
tickColors( cliPlayerColors );
|
||||
EntityFloatingItem.ageStatic = (EntityFloatingItem.ageStatic + 1) % 60000;
|
||||
CableRenderMode currentMode = AEApi.instance().partHelper().getCableRenderMode();
|
||||
if ( currentMode != crm )
|
||||
{
|
||||
crm = currentMode;
|
||||
CommonHelper.proxy.triggerUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
// rwar!
|
||||
if ( ev.type == Type.WORLD && ev.phase == Phase.END )
|
||||
{
|
||||
WorldTickEvent wte = (WorldTickEvent) ev;
|
||||
synchronized (craftingJobs)
|
||||
{
|
||||
Collection<CraftingJob> jobSet = craftingJobs.get( wte.world );
|
||||
if ( !jobSet.isEmpty() )
|
||||
{
|
||||
int simTime = Math.max( 1, AEConfig.instance.craftingCalculationTimePerTick / jobSet.size() );
|
||||
Iterator<CraftingJob> i = jobSet.iterator();
|
||||
while (i.hasNext())
|
||||
{
|
||||
CraftingJob cj = i.next();
|
||||
if ( !cj.simulateFor( simTime ) )
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// for no there is no reason to care about this on the client...
|
||||
else if ( ev.type == Type.SERVER && ev.phase == Phase.END )
|
||||
{
|
||||
tickColors( srvPlayerColors );
|
||||
// ready tiles.
|
||||
HandlerRep repo = getRepo();
|
||||
while (!repo.tiles.isEmpty())
|
||||
{
|
||||
AEBaseTile bt = repo.tiles.poll();
|
||||
if ( !bt.isInvalid() )
|
||||
bt.onReady();
|
||||
}
|
||||
|
||||
// tick networks.
|
||||
for (Grid g : getRepo().networks)
|
||||
g.update();
|
||||
|
||||
// cross world queue.
|
||||
processQueue( serverQueue );
|
||||
}
|
||||
|
||||
// world synced queue(s)
|
||||
if ( ev.type == Type.WORLD && ev.phase == Phase.START )
|
||||
{
|
||||
processQueue( callQueue.get( ((WorldTickEvent) ev).world ) );
|
||||
}
|
||||
}
|
||||
|
||||
private void processQueue(Queue<Callable> queue)
|
||||
{
|
||||
if ( queue == null )
|
||||
return;
|
||||
|
||||
Stopwatch sw = Stopwatch.createStarted();
|
||||
|
||||
Callable c = null;
|
||||
while ((c = queue.poll()) != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
c.call();
|
||||
|
||||
if ( sw.elapsed( TimeUnit.MILLISECONDS ) > 50 )
|
||||
break;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
}
|
||||
|
||||
// long time = sw.elapsed( TimeUnit.MILLISECONDS );
|
||||
// if ( time > 0 )
|
||||
// AELog.info( "processQueue Time: " + time + "ms" );
|
||||
}
|
||||
|
||||
Multimap<World, CraftingJob> craftingJobs = LinkedListMultimap.create();
|
||||
|
||||
public void registerCraftingSimulation(World world, CraftingJob craftingJob)
|
||||
{
|
||||
synchronized (craftingJobs)
|
||||
{
|
||||
craftingJobs.put( world, craftingJob );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user