diff --git a/src/main/java/cr0s/warpdrive/Commons.java b/src/main/java/cr0s/warpdrive/Commons.java index 74ddfa26..0060cfbb 100644 --- a/src/main/java/cr0s/warpdrive/Commons.java +++ b/src/main/java/cr0s/warpdrive/Commons.java @@ -195,15 +195,15 @@ public class Commons { fieldToReturn = clazz.getDeclaredField(obfuscatedName); } catch (Exception exception2) { exception2.printStackTrace(); - String map = ""; + StringBuilder map = new StringBuilder(); for (Field fieldDeclared : clazz.getDeclaredFields()) { - if (!map.isEmpty()) { - map += ", "; + if (map.length() > 0) { + map.append(", "); } - map += fieldDeclared.getName(); + map.append(fieldDeclared.getName()); } WarpDrive.logger.error(String.format("Unable to find %1$s field in %2$s class. Available fields are: %3$s", - deobfuscatedName, clazz.toString(), map)); + deobfuscatedName, clazz.toString(), map.toString())); } } if (fieldToReturn != null) { @@ -285,8 +285,7 @@ public class Commons { toIgnore.addAll(Arrays.asList(ignore)); } - Set toIterate = new HashSet<>(); - toIterate.addAll(start); + Set toIterate = new HashSet<>(start); Set toIterateNext; @@ -295,7 +294,7 @@ public class Commons { int range = 0; while(!toIterate.isEmpty() && range < maxRange) { toIterateNext = new HashSet<>(); - for (BlockPos current : toIterate) { + for (final BlockPos current : toIterate) { if (whitelist.contains(new VectorI(current).getBlockState_noChunkLoading(world).getBlock())) { iterated.add(current); } diff --git a/src/main/java/cr0s/warpdrive/WarpDrive.java b/src/main/java/cr0s/warpdrive/WarpDrive.java index 93d9f617..ecb670e4 100644 --- a/src/main/java/cr0s/warpdrive/WarpDrive.java +++ b/src/main/java/cr0s/warpdrive/WarpDrive.java @@ -1,7 +1,13 @@ package cr0s.warpdrive; import cr0s.warpdrive.api.IBlockBase; -import cr0s.warpdrive.block.*; +import cr0s.warpdrive.block.BlockChunkLoader; +import cr0s.warpdrive.block.BlockLaser; +import cr0s.warpdrive.block.BlockLaserMedium; +import cr0s.warpdrive.block.ItemBlockAbstractBase; +import cr0s.warpdrive.block.TileEntityChunkLoader; +import cr0s.warpdrive.block.TileEntityLaser; +import cr0s.warpdrive.block.TileEntityLaserMedium; import cr0s.warpdrive.block.atomic.BlockAcceleratorControlPoint; import cr0s.warpdrive.block.atomic.BlockAcceleratorController; import cr0s.warpdrive.block.atomic.BlockChiller; @@ -74,6 +80,7 @@ import cr0s.warpdrive.damage.DamageTeleportation; import cr0s.warpdrive.damage.DamageWarm; import cr0s.warpdrive.data.*; import cr0s.warpdrive.event.ChunkHandler; +import cr0s.warpdrive.event.ChunkLoadingHandler; import cr0s.warpdrive.event.ClientHandler; import cr0s.warpdrive.event.CommonWorldGenerator; import cr0s.warpdrive.event.ItemHandler; @@ -118,9 +125,6 @@ import net.minecraft.world.biome.Biome; import net.minecraftforge.common.BiomeDictionary; import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.ForgeChunkManager; -import net.minecraftforge.common.ForgeChunkManager.LoadingCallback; -import net.minecraftforge.common.ForgeChunkManager.Ticket; -import net.minecraftforge.common.ForgeChunkManager.Type; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.EnumHelper; import net.minecraftforge.fml.common.FMLCommonHandler; @@ -148,7 +152,7 @@ import javax.annotation.Nullable; @Mod(modid = WarpDrive.MODID, name = "WarpDrive", version = WarpDrive.VERSION, dependencies = "after:IC2;" + " after:cofhcore;" + " after:ComputerCraft;" + " after:OpenComputer;" + " after:CCTurtle;" + " after:gregtech;" + " after:AppliedEnergistics;" + " after:EnderIO;" + " after:DefenseTech;" + " after:icbmclassic;") -public class WarpDrive implements LoadingCallback { +public class WarpDrive { public static final String MODID = "warpdrive"; public static final String VERSION = "@version@"; public static final String PREFIX = MODID + ":"; @@ -427,7 +431,7 @@ public class WarpDrive implements LoadingCallback { itemUpgrade = new ItemUpgrade("itemUpgrade"); } - // tool items + // tool items itemTuningFork = new ItemTuningFork("itemTuningFork"); itemTuningDriver = new ItemTuningDriver("itemTuningDriver"); @@ -450,7 +454,7 @@ public class WarpDrive implements LoadingCallback { proxy.registerRendering(); // chunk loading - ForgeChunkManager.setForcedChunkLoadingCallback(instance, instance); + ForgeChunkManager.setForcedChunkLoadingCallback(instance, ChunkLoadingHandler.INSTANCE); // world generation commonWorldGenerator = new CommonWorldGenerator(); @@ -567,51 +571,6 @@ public class WarpDrive implements LoadingCallback { event.registerServerCommand(new CommandSpace()); } - public Ticket registerChunkLoadTileEntity(TileEntityAbstractChunkLoading tileEntity) { - World worldObj = tileEntity.getWorld(); - if (ForgeChunkManager.ticketCountAvailableFor(this, worldObj) > 0) { - Ticket ticket = ForgeChunkManager.requestTicket(this, worldObj, Type.NORMAL); - if (ticket != null) { - tileEntity.giveTicket(ticket); // FIXME calling the caller is a bad idea - return ticket; - } else { - WarpDrive.logger.error("Ticket not granted"); - } - } else { - WarpDrive.logger.error("No tickets left!"); - } - return null; - } - - @Override - public void ticketsLoaded(List tickets, World world) { - for (Ticket ticket : tickets) { - NBTTagCompound data = ticket.getModData(); - if (data != null) { - int w = data.getInteger("ticketWorldObj"); - int x = data.getInteger("ticketX"); - int y = data.getInteger("ticketY"); - int z = data.getInteger("ticketZ"); - if (w != 0 || x != 0 || y != 0 || z != 0) { - WorldServer worldServer = DimensionManager.getWorld(w); - if (worldServer != null) {// skip non-loaded worlds - TileEntity tileEntity = worldServer.getTileEntity(new BlockPos(x, y, z)); - if (tileEntity != null && tileEntity instanceof TileEntityAbstractChunkLoading) { - if (((TileEntityAbstractChunkLoading) tileEntity).shouldChunkLoad()) { - WarpDrive.logger.info("ChunkLoadingTicket is loading " + tileEntity); - ((TileEntityAbstractChunkLoading) tileEntity).giveTicket(ticket); - ((TileEntityAbstractChunkLoading) tileEntity).refreshLoading(true); - return; - } - } - } - } - } - - ForgeChunkManager.releaseTicket(ticket); - } - } - @SuppressWarnings("ConstantConditions") @Mod.EventHandler public void onFMLMissingMappings(FMLMissingMappingsEvent event) { diff --git a/src/main/java/cr0s/warpdrive/block/BlockChunkLoader.java b/src/main/java/cr0s/warpdrive/block/BlockChunkLoader.java index a733b4a8..1d39d054 100644 --- a/src/main/java/cr0s/warpdrive/block/BlockChunkLoader.java +++ b/src/main/java/cr0s/warpdrive/block/BlockChunkLoader.java @@ -1,12 +1,25 @@ package cr0s.warpdrive.block; +import cr0s.warpdrive.Commons; import cr0s.warpdrive.WarpDrive; +import cr0s.warpdrive.data.EnumComponentType; +import cr0s.warpdrive.item.ItemComponent; + import net.minecraft.block.material.Material; +import net.minecraft.block.state.IBlockState; +import net.minecraft.entity.item.EntityItem; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.EnumFacing; +import net.minecraft.util.EnumHand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.TextComponentTranslation; import net.minecraft.world.World; import net.minecraftforge.fml.common.registry.GameRegistry; import javax.annotation.Nonnull; +import javax.annotation.Nullable; public class BlockChunkLoader extends BlockAbstractContainer { @@ -18,7 +31,101 @@ public class BlockChunkLoader extends BlockAbstractContainer { @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityChunkLoader(); } + + @Override + public boolean onBlockActivated(final World world, final BlockPos blockPos, final IBlockState state, final EntityPlayer entityPlayer, + final EnumHand hand, @Nullable final ItemStack itemStackHeld, + final EnumFacing side, final float hitX, final float hitY, final float hitZ) { + if (world.isRemote) { + return false; + } + + if (hand != EnumHand.MAIN_HAND) { + return true; + } + + final TileEntity tileEntity = world.getTileEntity(blockPos); + if (!(tileEntity instanceof TileEntityChunkLoader)) { + return false; + } + final TileEntityChunkLoader tileEntityChunkLoader = (TileEntityChunkLoader) tileEntity; + + EnumComponentType enumComponentType = null; + if ( itemStackHeld != null + && itemStackHeld.getItem() instanceof ItemComponent ) { + enumComponentType = EnumComponentType.get(itemStackHeld.getItemDamage()); + } + + // sneaking with an empty hand or an upgrade item in hand to dismount current upgrade + if (entityPlayer.isSneaking()) { + // using an upgrade item or an empty hand means dismount upgrade + if ( itemStackHeld == null + || enumComponentType != null ) { + // find a valid upgrade to dismount + if ( itemStackHeld == null + || !tileEntityChunkLoader.hasUpgrade(enumComponentType) ) { + enumComponentType = (EnumComponentType) tileEntityChunkLoader.getFirstUpgradeOfType(EnumComponentType.class, null); + } + + if (enumComponentType == null) { + // no more upgrades to dismount + Commons.addChatMessage(entityPlayer, new TextComponentTranslation("warpdrive.upgrade.result.noUpgradeToDismount")); + return true; + } + + if (!entityPlayer.capabilities.isCreativeMode) { + // dismount the current upgrade item + final ItemStack itemStackDrop = ItemComponent.getItemStackNoCache(enumComponentType, 1); + final EntityItem entityItem = new EntityItem(world, entityPlayer.posX, entityPlayer.posY + 0.5D, entityPlayer.posZ, itemStackDrop); + entityItem.setNoPickupDelay(); + world.spawnEntityInWorld(entityItem); + } + + tileEntityChunkLoader.dismountUpgrade(enumComponentType); + // upgrade dismounted + Commons.addChatMessage(entityPlayer, new TextComponentTranslation("warpdrive.upgrade.result.dismounted", enumComponentType.name())); + return false; + } + + } else if (itemStackHeld == null) {// no sneaking and no item in hand => show status + Commons.addChatMessage(entityPlayer, tileEntityChunkLoader.getStatus()); + return true; + + } else if (enumComponentType != null) {// no sneaking and an upgrade in hand => mounting an upgrade + // validate type + if (tileEntityChunkLoader.getUpgradeMaxCount(enumComponentType) <= 0) { + // invalid upgrade type + Commons.addChatMessage(entityPlayer, new TextComponentTranslation("warpdrive.upgrade.result.invalidUpgrade")); + return true; + } + if (!tileEntityChunkLoader.canUpgrade(enumComponentType)) { + // too many upgrades + Commons.addChatMessage(entityPlayer, new TextComponentTranslation("warpdrive.upgrade.result.tooManyUpgrades", + tileEntityChunkLoader.getUpgradeMaxCount(enumComponentType))); + return true; + } + + if (!entityPlayer.capabilities.isCreativeMode) { + // validate quantity + if (itemStackHeld.stackSize < 1) { + // not enough upgrade items + Commons.addChatMessage(entityPlayer, new TextComponentTranslation("warpdrive.upgrade.result.notEnoughUpgrades")); + return true; + } + + // update player inventory + itemStackHeld.stackSize -= 1; + } + + // mount the new upgrade item + tileEntityChunkLoader.mountUpgrade(enumComponentType); + // upgrade mounted + Commons.addChatMessage(entityPlayer, new TextComponentTranslation("warpdrive.upgrade.result.mounted", enumComponentType.name())); + } + + return false; + } } diff --git a/src/main/java/cr0s/warpdrive/block/BlockLaser.java b/src/main/java/cr0s/warpdrive/block/BlockLaser.java index 6d552316..db639e30 100644 --- a/src/main/java/cr0s/warpdrive/block/BlockLaser.java +++ b/src/main/java/cr0s/warpdrive/block/BlockLaser.java @@ -30,7 +30,7 @@ public class BlockLaser extends BlockAbstractContainer { @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityLaser(); } diff --git a/src/main/java/cr0s/warpdrive/block/BlockLaserMedium.java b/src/main/java/cr0s/warpdrive/block/BlockLaserMedium.java index 7acf2712..17d41724 100644 --- a/src/main/java/cr0s/warpdrive/block/BlockLaserMedium.java +++ b/src/main/java/cr0s/warpdrive/block/BlockLaserMedium.java @@ -23,6 +23,7 @@ import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; public class BlockLaserMedium extends BlockAbstractContainer { + public static final PropertyInteger LEVEL = PropertyInteger.create("level", 0, 7); public BlockLaserMedium(final String registryName) { @@ -72,7 +73,7 @@ public class BlockLaserMedium extends BlockAbstractContainer { } if (itemStackHeld == null) { - TileEntity tileEntity = world.getTileEntity(blockPos); + final TileEntity tileEntity = world.getTileEntity(blockPos); if (tileEntity instanceof TileEntityLaserMedium) { Commons.addChatMessage(entityPlayer, ((TileEntityLaserMedium) tileEntity).getStatus()); return true; diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractBase.java b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractBase.java index 221d3bd6..e3941e74 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractBase.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractBase.java @@ -347,6 +347,10 @@ public abstract class TileEntityAbstractBase extends TileEntity implements IBloc return mapResult; } + public int getValidUpgradeCount(final Object upgrade) { + return Math.min(getUpgradeMaxCount(upgrade), getUpgradeCount(upgrade)); + } + public int getUpgradeCount(final Object upgrade) { final Integer value = installedUpgrades.get(upgrade); return value == null ? 0 : value; @@ -358,10 +362,10 @@ public abstract class TileEntityAbstractBase extends TileEntity implements IBloc } protected String getUpgradesAsString() { - String message = ""; + final StringBuilder message = new StringBuilder(); for (final Entry entry : installedUpgrades.entrySet()) { - if (!message.isEmpty()) { - message += ", "; + if (message.length() > 0) { + message.append(", "); } final Object key = entry.getKey(); String keyName = key.toString(); @@ -371,12 +375,12 @@ public abstract class TileEntityAbstractBase extends TileEntity implements IBloc keyName = ((Block) key).getUnlocalizedName(); } if (entry.getValue() == 1) { - message += keyName; + message.append(keyName); } else { - message += entry.getValue() + " x " + keyName; + message.append(entry.getValue()).append(" x ").append(keyName); } } - return message; + return message.toString(); } protected void setUpgradeMaxCount(final Object upgrade, final int value) { @@ -397,7 +401,7 @@ public abstract class TileEntityAbstractBase extends TileEntity implements IBloc } public boolean dismountUpgrade(final Object upgrade) { - int count = getUpgradeCount(upgrade); + final int count = getUpgradeCount(upgrade); if (count > 1) { installedUpgrades.put(upgrade, count - 1); markDirty(); diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractChunkLoading.java b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractChunkLoading.java index 15305f00..62432fba 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityAbstractChunkLoading.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityAbstractChunkLoading.java @@ -1,223 +1,175 @@ package cr0s.warpdrive.block; -import com.google.common.collect.ImmutableSet; import cr0s.warpdrive.WarpDrive; +import cr0s.warpdrive.config.WarpDriveConfig; +import cr0s.warpdrive.event.ChunkLoadingHandler; import java.util.ArrayList; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.ChunkPos; -import net.minecraftforge.common.ForgeChunkManager; import net.minecraftforge.common.ForgeChunkManager.Ticket; -public abstract class TileEntityAbstractChunkLoading extends TileEntityAbstractEnergy -{ - private final ArrayList ticketList = new ArrayList<>(); +public abstract class TileEntityAbstractChunkLoading extends TileEntityAbstractEnergy { - public abstract boolean shouldChunkLoad(); - protected ChunkPos minChunk = null; - protected ChunkPos maxChunk = null; + // persistent properties + protected ChunkPos chunkMin = null; + protected ChunkPos chunkMax = null; + // computed properties + private Ticket ticket = null; + private boolean isRefreshNeeded = true; protected boolean areChunksLoaded = false; - - // OVERRIDES + + @Override + protected void onFirstUpdateTick() { + super.onFirstUpdateTick(); + + if (worldObj.isRemote) { + return; + } + + if ( chunkMin == null + || chunkMax == null ) { + WarpDrive.logger.warn(this + " No chunk coordinates defined, assuming current chunk"); + chunkMin = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); + chunkMax = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); + } + } + @Override public void update() { super.update(); - if (shouldChunkLoad() != areChunksLoaded) { - refreshLoading(); + if (worldObj.isRemote) { + return; } - if (shouldChunkLoad()) { - handleLoadedTick(); + if ( isRefreshNeeded + || shouldChunkLoad() != areChunksLoaded ) { + refreshLoading(isRefreshNeeded); + isRefreshNeeded = false; } } - public void handleLoadedTick() { - + public abstract boolean shouldChunkLoad(); + + public void refreshChunkLoading() { + isRefreshNeeded = true; } - public synchronized void refreshLoading(boolean force) { - boolean loadRequested = shouldChunkLoad(); - if (!ticketList.isEmpty()) { - if (loadRequested && (!areChunksLoaded || force)) { - int ticketSize = ticketList.get(0).getMaxChunkListDepth(); - ArrayList chunkList = getChunksToLoad(); - int numTicketsRequired = (int) Math.ceil((double) chunkList.size() / ticketSize); // FIXME there should be only one ticket per requesting TileEntity - if (ticketList.size() != numTicketsRequired) { - for(int i = ticketList.size(); i < numTicketsRequired; i++) { - WarpDrive.instance.registerChunkLoadTileEntity(this); - } + public synchronized void refreshLoading(final boolean force) { + final boolean shouldChunkLoad = shouldChunkLoad(); + if (shouldChunkLoad) { + if (ticket == null) { + chunkloading_giveTicket(ChunkLoadingHandler.forgeTicket_requestNormal(worldObj, this)); + } else if (force) { + ChunkLoadingHandler.forgeTicket_clearChunks(ticket); + } + + if (!areChunksLoaded || force) { + final int ticketSize = ticket.getMaxChunkListDepth(); + final ArrayList chunksToLoad = getChunksToLoad(); + if (chunksToLoad.size() > ticketSize) { + WarpDrive.logger.error(String.format("Too many chunk requested for loading @ %s (%d %d %d)", + worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ())); + return; } - int tickNum = 0; - int chunkInTicket = 0; - - Ticket t = ticketList.get(0); - for(ChunkPos chunk:chunkList) { - if (chunkInTicket >= ticketSize) { - chunkInTicket = 0; - tickNum++; - t = ticketList.get(tickNum); - } - - WarpDrive.logger.info("Attempting to force chunk" + chunk); - ForgeChunkManager.forceChunk(t, chunk); - chunkInTicket++; + for (final ChunkPos chunk : chunksToLoad) { + ChunkLoadingHandler.forgeTicket_addChunks(ticket, chunk); } areChunksLoaded = true; - } else if(!loadRequested) { - for(Ticket ticket:ticketList) { - ImmutableSet chunks = ticket.getChunkList(); - for(ChunkPos chunk : chunks) { - ForgeChunkManager.unforceChunk(ticket, chunk); - } - - ForgeChunkManager.releaseTicket(ticket); - } - ticketList.clear(); - areChunksLoaded = false; } - } else if (loadRequested) { - WarpDrive.instance.registerChunkLoadTileEntity(this); - refreshLoading(); + + } else if (ticket != null) { + ChunkLoadingHandler.forgeTicket_release(ticket); + ticket = null; + areChunksLoaded = false; } } - public void refreshLoading() { - refreshLoading(false); - } - - public void giveTicket(Ticket t) { - NBTTagCompound nbt = t.getModData(); - nbt.setInteger("ticketWorldObj", worldObj.provider.getDimension()); - nbt.setInteger("ticketX", pos.getX()); - nbt.setInteger("ticketY", pos.getY()); - nbt.setInteger("ticketZ", pos.getZ()); - ticketList.add(t); - } - - private static int dX(int dir) - { - if (dir == 1) - return 1; - else if (dir == 3) - return -1; - return 0; - } - - private static int dZ(int dir) - { - if (dir == 0) - return 1; - else if (dir == 2) - return -1; - return 0; - } - - public ArrayList getChunksFromCentre(ChunkPos chunkA, ChunkPos chunkB) - { - if(!shouldChunkLoad()) - return null; - int minX = Math.min(chunkA.chunkXPos, chunkB.chunkXPos); - int maxX = Math.max(chunkA.chunkXPos, chunkB.chunkXPos); - int minZ = Math.min(chunkA.chunkZPos, chunkB.chunkZPos); - int maxZ = Math.max(chunkA.chunkZPos, chunkB.chunkZPos); - WarpDrive.logger.info("ChunkLoading from " + minX + "," + minZ + " to " + maxX + "," + maxZ); - - //REMOVE ODD SIZES - int deltaX = (maxX - minX + 1); - int deltaZ = (maxZ - minZ + 1); - - maxX = minX + deltaX - 1; - maxZ = minZ + deltaZ - 1; - WarpDrive.logger.info("Allocating " + deltaX + " x " + deltaZ + " blocks from " + minX + "," + minZ + " to " + maxX + "," + maxZ); - - int maxEntries = (deltaX) * (deltaZ); - ArrayList chunkList = new ArrayList<>(maxEntries); - - int dir = 1; - int x = minX; - int z = maxZ; - for(int i = 0; i < maxEntries; i++) - { - chunkList.add(new ChunkPos(x, z)); - int dX = dX(dir); - int dZ = dZ(dir); - if(x + dX > maxX || x + dX < minX || z + dZ > maxZ || z + dZ < minZ) - { - dir++; - if(dir >= 4) - dir = 0; - dX = dX(dir); - dZ = dZ(dir); - - if(dX == 1) - minX++; - if(dX == -1) - maxX--; - if(dZ == 1) - minZ++; - if(dZ == -1) - maxZ--; - - } - x += dX; - z += dZ; + public void chunkloading_giveTicket(final Ticket ticket) { + if (this.ticket != null) { + ChunkLoadingHandler.forgeTicket_release(this.ticket); + this.ticket = null; } - - return chunkList; + this.ticket = ticket; + } + + public int chunkloading_getArea() { + return (chunkMax.chunkXPos - chunkMin.chunkXPos + 1) + * (chunkMax.chunkZPos - chunkMin.chunkZPos + 1); } @Override - public NBTTagCompound writeToNBT(NBTTagCompound tag) { - tag = super.writeToNBT(tag); - if (minChunk == null) { - minChunk = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); + public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) { + tagCompound = super.writeToNBT(tagCompound); + + if (chunkMin == null) { + chunkMin = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); } - if (maxChunk == null) { - maxChunk = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); + if (chunkMax == null) { + chunkMax = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); } - tag.setInteger("minChunkX", minChunk.chunkXPos); - tag.setInteger("minChunkZ", minChunk.chunkZPos); - tag.setInteger("maxChunkX", maxChunk.chunkXPos); - tag.setInteger("maxChunkZ", maxChunk.chunkZPos); - return tag; + + tagCompound.setInteger("minChunkX", chunkMin.chunkXPos); + tagCompound.setInteger("minChunkZ", chunkMin.chunkZPos); + tagCompound.setInteger("maxChunkX", chunkMax.chunkXPos); + tagCompound.setInteger("maxChunkZ", chunkMax.chunkZPos); + return tagCompound; } @Override - public void readFromNBT(NBTTagCompound tag) { - super.readFromNBT(tag); - if(tag.hasKey("minChunkX")) - { - int mx = tag.getInteger("minChunkX"); - int mz = tag.getInteger("minChunkZ"); - minChunk = new ChunkPos(mx,mz); - mx = tag.getInteger("maxChunkX"); - mz = tag.getInteger("maxChunkZ"); - maxChunk = new ChunkPos(mx,mz); + public void readFromNBT(final NBTTagCompound tagCompound) { + super.readFromNBT(tagCompound); + + if (tagCompound.hasKey("minChunkX")) { + final int xMin = tagCompound.getInteger("minChunkX"); + final int zMin = tagCompound.getInteger("minChunkZ"); + chunkMin = new ChunkPos(xMin, zMin); + + final int xMax = tagCompound.getInteger("maxChunkX"); + final int zMax = tagCompound.getInteger("maxChunkZ"); + chunkMax = new ChunkPos(xMax, zMax); } } @Override public void invalidate() { super.invalidate(); - for(Ticket t : ticketList) { - ForgeChunkManager.releaseTicket(t); + if (ticket != null) { + ChunkLoadingHandler.forgeTicket_release(ticket); + ticket = null; } } - public ArrayList getChunksToLoad() - { - if(minChunk == null || maxChunk == null) - { - ArrayList chunkList = new ArrayList<>(1); - chunkList.add(worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair()); - return chunkList; + public ArrayList getChunksToLoad() { + if (!shouldChunkLoad()) { + return null; } - return getChunksFromCentre(minChunk, maxChunk); + + assert(chunkMin.chunkXPos <= chunkMax.chunkXPos); + assert(chunkMin.chunkZPos <= chunkMax.chunkZPos); + + final int count = chunkloading_getArea(); + if (WarpDriveConfig.LOGGING_CHUNK_LOADING) { + WarpDrive.logger.info(String.format("Collecting %d chunks to be loaded @ %s from %s to %s", + count, + worldObj.provider.getSaveFolder(), + chunkMin, chunkMax)); + } + final ArrayList chunkCoords = new ArrayList<>(count); + + for (int x = chunkMin.chunkXPos; x <= chunkMax.chunkXPos; x++) { + for (int z = chunkMin.chunkZPos; z <= chunkMax.chunkZPos; z++) { + chunkCoords.add(new ChunkPos(x, z)); + } + } + + return chunkCoords; } } diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityChunkLoader.java b/src/main/java/cr0s/warpdrive/block/TileEntityChunkLoader.java index f8b9b59f..150803ac 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityChunkLoader.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityChunkLoader.java @@ -2,8 +2,7 @@ package cr0s.warpdrive.block; import cr0s.warpdrive.Commons; import cr0s.warpdrive.config.WarpDriveConfig; -import cr0s.warpdrive.data.UpgradeType; -import cr0s.warpdrive.item.ItemUpgrade; +import cr0s.warpdrive.data.EnumComponentType; import dan200.computercraft.api.lua.ILuaContext; import dan200.computercraft.api.peripheral.IComputerAccess; import li.cil.oc.api.machine.Arguments; @@ -13,29 +12,33 @@ import li.cil.oc.api.machine.Context; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; import net.minecraft.util.math.ChunkPos; -import net.minecraft.world.chunk.Chunk; +import net.minecraft.util.text.ITextComponent; +import net.minecraft.util.text.TextComponentString; import net.minecraftforge.fml.common.Optional; public class TileEntityChunkLoader extends TileEntityAbstractChunkLoading { - private boolean isActive = false; + // persistent properties private boolean isEnabled = false; - private boolean initialised = false; - private ChunkPos myChunk; + private int radiusXneg = 0; + private int radiusXpos = 0; + private int radiusZneg = 0; + private int radiusZpos = 0; - private int negDX, posDX, negDZ, posDZ; - private int area = 1; + // fuel status is needed before first tick + private boolean isPowered = false; + + // computed properties + // (none) public TileEntityChunkLoader() { super(); + IC2_sinkTier = 2; IC2_sourceTier = 2; - negDX = 0; - negDZ = 0; - posDX = 0; - posDZ = 0; - peripheralName = "warpdriveChunkloader"; + + peripheralName = "warpdriveChunkLoader"; addMethods(new String[] { "enable", "bounds", @@ -44,125 +47,177 @@ public class TileEntityChunkLoader extends TileEntityAbstractChunkLoading { "getEnergyRequired" }); - setUpgradeMaxCount(ItemUpgrade.getItemStack(UpgradeType.Energy), 2); - setUpgradeMaxCount(ItemUpgrade.getItemStack(UpgradeType.Power), 2); + setUpgradeMaxCount(EnumComponentType.SUPERCONDUCTOR, 5); + setUpgradeMaxCount(EnumComponentType.EMERALD_CRYSTAL, WarpDriveConfig.CHUNK_LOADER_MAX_RADIUS); } @Override public int energy_getMaxStorage() { - return WarpDriveConfig.CL_MAX_ENERGY; + return WarpDriveConfig.CHUNK_LOADER_MAX_ENERGY_STORED; } @Override - public boolean energy_canInput(EnumFacing from) { + public boolean energy_canInput(final EnumFacing from) { return true; } - public long energy_getEnergyRequired() { - return area * WarpDriveConfig.CL_RF_PER_CHUNKTICK; + @Override + public boolean dismountUpgrade(Object upgrade) { + final boolean isSuccess = super.dismountUpgrade(upgrade); + if (isSuccess) { + final int maxRange = getMaxRange(); + setBounds(maxRange, maxRange, maxRange, maxRange); + } + return isSuccess; } @Override - public boolean shouldChunkLoad() - { - return isEnabled && isActive; + public boolean mountUpgrade(Object upgrade) { + final boolean isSuccess = super.mountUpgrade(upgrade); + if (isSuccess) { + final int maxRange = getMaxRange(); + setBounds(maxRange, maxRange, maxRange, maxRange); + } + return isSuccess; + } + + private int getMaxRange() { + return getValidUpgradeCount(EnumComponentType.EMERALD_CRYSTAL); + } + + private double getEnergyFactor() { + final int upgradeCount = getValidUpgradeCount(EnumComponentType.SUPERCONDUCTOR); + return 1.0D - 0.1D * upgradeCount; + } + + public long chunkloading_getEnergyRequired() { + return (long) Math.ceil(getEnergyFactor() * chunkloading_getArea() * WarpDriveConfig.CHUNK_LOADER_ENERGY_PER_CHUNK); + } + + @Override + public boolean shouldChunkLoad() { + return isEnabled && isPowered; + } + + @Override + protected void onFirstUpdateTick() { + super.onFirstUpdateTick(); + + if (worldObj.isRemote) { + return; + } + + refreshChunkRange(); } @Override public void update() { super.update(); - if (!initialised) { - initialised = true; - myChunk = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); - changedDistance(); + if (worldObj.isRemote) { + return; } - if (isEnabled) { - isActive = energy_consume(energy_getEnergyRequired(), false); - } else { - isActive = energy_consume(energy_getEnergyRequired(), true); + isPowered = energy_consume(chunkloading_getEnergyRequired(), !isEnabled); + } + + private void setBounds(final int negX, final int posX, final int negZ, final int posZ) { + // compute new values + final int maxRange = getMaxRange(); + final int radiusXneg_new = - Commons.clamp(0, 1000, Math.abs(negX)); + final int radiusXpos_new = Commons.clamp(0, 1000, Math.abs(posX)); + final int radiusZneg_new = - Commons.clamp(0, 1000, Math.abs(negZ)); + final int radiusZpos_new = Commons.clamp(0, 1000, Math.abs(posZ)); + + // validate size constrains + final int maxArea = (1 + 2 * maxRange) * (1 + 2 * maxRange); + final int newArea = (-radiusXneg_new + 1 + radiusXpos_new) + * (-radiusZneg_new + 1 + radiusZpos_new); + if (newArea <= maxArea) { + radiusXneg = radiusXneg_new; + radiusXpos = radiusXpos_new; + radiusZneg = radiusZneg_new; + radiusZpos = radiusZpos_new; + refreshChunkRange(); } } - private void setBounds(int negX, int posX, int negZ, int posZ) { - negDX = - Commons.clamp(0, WarpDriveConfig.CL_MAX_DISTANCE, Math.abs(negX)); - posDX = Commons.clamp(0, WarpDriveConfig.CL_MAX_DISTANCE, Math.abs(posX)); - negDZ = - Commons.clamp(0, WarpDriveConfig.CL_MAX_DISTANCE, Math.abs(negZ)); - posDZ = Commons.clamp(0, WarpDriveConfig.CL_MAX_DISTANCE, Math.abs(posZ)); - } - - private void changedDistance() { + private void refreshChunkRange() { if (worldObj == null) { return; } - if (myChunk == null) { - Chunk aChunk = worldObj.getChunkFromBlockCoords(pos); - myChunk = aChunk.getChunkCoordIntPair(); - } + final ChunkPos chunkSelf = worldObj.getChunkFromBlockCoords(pos).getChunkCoordIntPair(); - minChunk = new ChunkPos(myChunk.chunkXPos + negDX,myChunk.chunkZPos + negDZ); - maxChunk = new ChunkPos(myChunk.chunkXPos + posDX,myChunk.chunkZPos + posDZ); - area = (posDX - negDX + 1) * (posDZ - negDZ + 1); - refreshLoading(true); - } - - @Override - public void readFromNBT(NBTTagCompound nbt) - { - super.readFromNBT(nbt); - setBounds(nbt.getInteger("negDX"), nbt.getInteger("posDX"), nbt.getInteger("negDZ"), nbt.getInteger("posDZ")); - changedDistance(); - } - - @Override - public NBTTagCompound writeToNBT(NBTTagCompound tag) { - tag = super.writeToNBT(tag); - tag.setInteger("negDX", negDX); - tag.setInteger("negDZ", negDZ); - tag.setInteger("posDX", posDX); - tag.setInteger("posDZ", posDZ); - return tag; + chunkMin = new ChunkPos(chunkSelf.chunkXPos + radiusXneg, chunkSelf.chunkZPos + radiusZneg); + chunkMax = new ChunkPos(chunkSelf.chunkXPos + radiusXpos, chunkSelf.chunkZPos + radiusZpos); + refreshChunkLoading(); } - //Common LUA functions + @Override + public ITextComponent getStatus() { + return super.getStatus() + .appendSibling(new TextComponentString("\n")).appendSibling(getUpgradeStatus()); + } + + // Forge overrides + + @Override + public void readFromNBT(NBTTagCompound tagCompound) { + super.readFromNBT(tagCompound); + tagCompound = super.writeToNBT(tagCompound); + isEnabled = !tagCompound.hasKey("isEnabled") || tagCompound.getBoolean("isEnabled"); + setBounds(tagCompound.getInteger("radiusXneg"), tagCompound.getInteger("radiusXpos"), tagCompound.getInteger("radiusZneg"), tagCompound.getInteger("radiusZpos")); + isPowered = tagCompound.getBoolean("isPowered"); + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound tagCompound) { + tagCompound = super.writeToNBT(tagCompound); + tagCompound.setBoolean("isEnabled", isEnabled); + tagCompound.setInteger("radiusXneg", radiusXneg); + tagCompound.setInteger("radiusZneg", radiusZneg); + tagCompound.setInteger("radiusXpos", radiusXpos); + tagCompound.setInteger("radiusZpos", radiusZpos); + tagCompound.setBoolean("isPowered", isPowered); + return tagCompound; + } + + // Common OC/CC methods public Object[] enable(Object[] arguments) { - if (arguments.length == 1) + if (arguments.length == 1) { isEnabled = Commons.toBool(arguments[0]); - return new Object[]{shouldChunkLoad()}; + } + return new Object[] { isEnabled }; } public Object[] bounds(Object[] arguments) { if (arguments.length == 4) { setBounds(Commons.toInt(arguments[0]), Commons.toInt(arguments[1]), Commons.toInt(arguments[2]), Commons.toInt(arguments[3])); - changedDistance(); } - return new Object[] { negDX, posDX, negDZ, posDZ }; + return new Object[] { radiusXneg, radiusXpos, radiusZneg, radiusZpos }; } public Object[] radius(Object[] arguments) { if (arguments.length == 1) { - int dist = Commons.toInt(arguments[0]); - setBounds(dist,dist,dist,dist); - changedDistance(); - return new Object[]{true}; + final int radius = Commons.toInt(arguments[0]); + setBounds(radius, radius, radius, radius); } - return new Object[]{false}; + return new Object[] { radiusXneg, radiusXpos, radiusZneg, radiusZpos }; } - public Object[] upgrades(Object[] arguments) { + public Object[] upgrades() { return new Object[] { getUpgradesAsString() }; } - public Object[] getEnergyRequired(Object[] arguments) { - return new Object[] { energy_getEnergyRequired() }; + public Object[] getEnergyRequired() { + return new Object[] { chunkloading_getEnergyRequired() }; } - + // OpenComputer callback methods @Callback @Optional.Method(modid = "OpenComputers") - public Object[] radius(Context context, Arguments arguments) { - return radius(argumentsOCtoCC(arguments)); + public Object[] enable(Context context, Arguments arguments) { + return enable(argumentsOCtoCC(arguments)); } @Callback @@ -173,42 +228,54 @@ public class TileEntityChunkLoader extends TileEntityAbstractChunkLoading { @Callback @Optional.Method(modid = "OpenComputers") - public Object[] enable(Context context, Arguments arguments) { - return enable(argumentsOCtoCC(arguments)); + public Object[] radius(Context context, Arguments arguments) { + return radius(argumentsOCtoCC(arguments)); } @Callback @Optional.Method(modid = "OpenComputers") public Object[] upgrades(Context context, Arguments arguments) { - return upgrades(argumentsOCtoCC(arguments)); + return upgrades(); } @Callback @Optional.Method(modid = "OpenComputers") public Object[] getEnergyRequired(Context context, Arguments arguments) { - return getEnergyRequired(argumentsOCtoCC(arguments)); - } + return getEnergyRequired(); + } - //CC method + // ComputerCraft IPeripheral methods @Override @Optional.Method(modid = "ComputerCraft") public Object[] callMethod(IComputerAccess computer, ILuaContext context, int method, Object[] arguments) { final String methodName = getMethodName(method); switch (methodName) { - case "radius": - return radius(arguments); - case "bounds": - return bounds(arguments); - case "enable": - return enable(arguments); - case "upgrades": - return upgrades(arguments); - case "getEnergyRequired": - return getEnergyRequired(arguments); + case "radius": + return radius(arguments); + + case "bounds": + return bounds(arguments); + + case "enable": + return enable(arguments); + + case "upgrades": + return upgrades(); + + case "getEnergyRequired": + return getEnergyRequired(); } return super.callMethod(computer, context, method, arguments); } + @Override + public String toString() { + return String.format( + "%s @ %s (%d %d %d)", + getClass().getSimpleName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); + } } diff --git a/src/main/java/cr0s/warpdrive/block/TileEntityLaser.java b/src/main/java/cr0s/warpdrive/block/TileEntityLaser.java index 6ae7ad2f..2778bbe7 100644 --- a/src/main/java/cr0s/warpdrive/block/TileEntityLaser.java +++ b/src/main/java/cr0s/warpdrive/block/TileEntityLaser.java @@ -677,7 +677,7 @@ public class TileEntityLaser extends TileEntityAbstractLaser implements IBeamFre return String.format("%s Beam \'%d\' @ %s (%d %d %d)", getClass().getSimpleName(), beamFrequency, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), pos.getX(), pos.getY(), pos.getZ()); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/block/atomic/BlockAcceleratorControlPoint.java b/src/main/java/cr0s/warpdrive/block/atomic/BlockAcceleratorControlPoint.java index be6a18ec..b632e8ec 100644 --- a/src/main/java/cr0s/warpdrive/block/atomic/BlockAcceleratorControlPoint.java +++ b/src/main/java/cr0s/warpdrive/block/atomic/BlockAcceleratorControlPoint.java @@ -55,7 +55,7 @@ public class BlockAcceleratorControlPoint extends BlockAbstractAccelerator imple @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityAcceleratorControlPoint(); } } diff --git a/src/main/java/cr0s/warpdrive/block/atomic/BlockChiller.java b/src/main/java/cr0s/warpdrive/block/atomic/BlockChiller.java index d0e75f61..5a055dec 100644 --- a/src/main/java/cr0s/warpdrive/block/atomic/BlockChiller.java +++ b/src/main/java/cr0s/warpdrive/block/atomic/BlockChiller.java @@ -31,7 +31,7 @@ public class BlockChiller extends BlockAbstractAccelerator { } @Override - public int damageDropped(IBlockState blockState) { + public int damageDropped(final IBlockState blockState) { return 0; } diff --git a/src/main/java/cr0s/warpdrive/block/atomic/BlockParticlesInjector.java b/src/main/java/cr0s/warpdrive/block/atomic/BlockParticlesInjector.java index c6249f76..d7db0089 100644 --- a/src/main/java/cr0s/warpdrive/block/atomic/BlockParticlesInjector.java +++ b/src/main/java/cr0s/warpdrive/block/atomic/BlockParticlesInjector.java @@ -20,7 +20,7 @@ public class BlockParticlesInjector extends BlockAcceleratorControlPoint { @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityParticlesInjector(); } } diff --git a/src/main/java/cr0s/warpdrive/block/atomic/TileEntityAcceleratorController.java b/src/main/java/cr0s/warpdrive/block/atomic/TileEntityAcceleratorController.java index a583a9e0..c6b79265 100644 --- a/src/main/java/cr0s/warpdrive/block/atomic/TileEntityAcceleratorController.java +++ b/src/main/java/cr0s/warpdrive/block/atomic/TileEntityAcceleratorController.java @@ -59,7 +59,9 @@ public class TileEntityAcceleratorController extends TileEntityAbstractEnergy im @Override public String toString() { - return String.format("%s @ \'%s\' (%d %d %d)", getClass().getSimpleName(), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), pos.getX(), pos.getY(), pos.getZ()); + return String.format("%s @ \'%s\' (%d %d %d)", + getClass().getSimpleName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); } } diff --git a/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGenerator.java b/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGenerator.java index 32f3106c..63506b46 100644 --- a/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGenerator.java +++ b/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGenerator.java @@ -131,7 +131,7 @@ public class TileEntityAirGenerator extends TileEntityAbstractEnergy { public String toString() { return String.format("%s @ %s (%d %d %d)", getClass().getSimpleName(), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), pos.getX(), pos.getY(), pos.getZ()); } diff --git a/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGeneratorTiered.java b/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGeneratorTiered.java index c1463d48..97628f16 100644 --- a/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGeneratorTiered.java +++ b/src/main/java/cr0s/warpdrive/block/breathing/TileEntityAirGeneratorTiered.java @@ -142,16 +142,16 @@ public class TileEntityAirGeneratorTiered extends TileEntityAbstractEnergy { } @Override - public boolean energy_canInput(EnumFacing from) { + public boolean energy_canInput(final EnumFacing from) { return true; } @Override public String toString() { return String.format("%s @ %s (%d %d %d)", - getClass().getSimpleName(), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - pos.getX(), pos.getY(), pos.getZ()); + getClass().getSimpleName(), + worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + pos.getX(), pos.getY(), pos.getZ()); } public Object[] enable(Object[] arguments) { diff --git a/src/main/java/cr0s/warpdrive/block/building/TileEntityShipScanner.java b/src/main/java/cr0s/warpdrive/block/building/TileEntityShipScanner.java index ef24b486..32274d90 100644 --- a/src/main/java/cr0s/warpdrive/block/building/TileEntityShipScanner.java +++ b/src/main/java/cr0s/warpdrive/block/building/TileEntityShipScanner.java @@ -786,7 +786,7 @@ public class TileEntityShipScanner extends TileEntityAbstractInterfaced implemen public String toString() { return String.format("%s @ %s (%d %d %d)", getClass().getSimpleName(), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - pos.getX(), pos.getY(), pos.getZ()); + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); } } diff --git a/src/main/java/cr0s/warpdrive/block/collection/TileEntityMiningLaser.java b/src/main/java/cr0s/warpdrive/block/collection/TileEntityMiningLaser.java index 9a9b0e02..af939287 100644 --- a/src/main/java/cr0s/warpdrive/block/collection/TileEntityMiningLaser.java +++ b/src/main/java/cr0s/warpdrive/block/collection/TileEntityMiningLaser.java @@ -570,6 +570,8 @@ public class TileEntityMiningLaser extends TileEntityAbstractMiner { @Override public String toString() { return String.format("%s @ \'%s\' %d, %d, %d", - getClass().getSimpleName(), worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), pos.getX(), pos.getY(), pos.getZ()); + getClass().getSimpleName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); } } diff --git a/src/main/java/cr0s/warpdrive/block/detection/BlockMonitor.java b/src/main/java/cr0s/warpdrive/block/detection/BlockMonitor.java index e137314d..c9d7e479 100644 --- a/src/main/java/cr0s/warpdrive/block/detection/BlockMonitor.java +++ b/src/main/java/cr0s/warpdrive/block/detection/BlockMonitor.java @@ -32,7 +32,7 @@ public class BlockMonitor extends BlockAbstractRotatingContainer { @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityMonitor(); } diff --git a/src/main/java/cr0s/warpdrive/block/detection/TileEntityCamera.java b/src/main/java/cr0s/warpdrive/block/detection/TileEntityCamera.java index 6dd29b8e..c625e131 100644 --- a/src/main/java/cr0s/warpdrive/block/detection/TileEntityCamera.java +++ b/src/main/java/cr0s/warpdrive/block/detection/TileEntityCamera.java @@ -193,9 +193,9 @@ public class TileEntityCamera extends TileEntityAbstractInterfaced implements IV @Override public String toString() { return String.format("%s %d @ %s (%d %d %d)", - getClass().getSimpleName(), - videoChannel, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - pos.getX(), pos.getY(), pos.getZ()); + getClass().getSimpleName(), + videoChannel, + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/block/detection/TileEntityMonitor.java b/src/main/java/cr0s/warpdrive/block/detection/TileEntityMonitor.java index c71d3177..c431b8b7 100644 --- a/src/main/java/cr0s/warpdrive/block/detection/TileEntityMonitor.java +++ b/src/main/java/cr0s/warpdrive/block/detection/TileEntityMonitor.java @@ -152,10 +152,10 @@ public class TileEntityMonitor extends TileEntityAbstractInterfaced implements I @Override public String toString() { - return String.format("%s %d @ %s (%d %d %d)", - getClass().getSimpleName(), - videoChannel, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - pos.getX(), pos.getY(), pos.getZ()); + return String.format("%s %d @ %s (%d %d %d)", + getClass().getSimpleName(), + videoChannel, + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java b/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java index 7faba257..2345ee20 100644 --- a/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java +++ b/src/main/java/cr0s/warpdrive/block/detection/TileEntityRadar.java @@ -111,7 +111,7 @@ public class TileEntityRadar extends TileEntityAbstractEnergy { Vector3 vec3Position = StarMapRegistry.getUniversalCoordinates(celestialObject, pos.getX(), pos.getY(), pos.getZ()); return new Object[] { pos.getX(), pos.getY(), pos.getZ(), celestialObject.getDisplayName(), vec3Position.x, vec3Position.y, vec3Position.z }; } else { - String name = worldObj.getWorldInfo().getWorldName(); + String name = worldObj.provider.getSaveFolder(); if (name == null || name.isEmpty()) { name = "DIM" + worldObj.provider.getDimension(); } diff --git a/src/main/java/cr0s/warpdrive/block/energy/BlockEnergyBank.java b/src/main/java/cr0s/warpdrive/block/energy/BlockEnergyBank.java index 571f87ad..1bd5f251 100644 --- a/src/main/java/cr0s/warpdrive/block/energy/BlockEnergyBank.java +++ b/src/main/java/cr0s/warpdrive/block/energy/BlockEnergyBank.java @@ -126,7 +126,7 @@ public class BlockEnergyBank extends BlockAbstractContainer implements IExplosio @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityEnergyBank((byte)(metadata % 4)); } @@ -137,7 +137,7 @@ public class BlockEnergyBank extends BlockAbstractContainer implements IExplosio } @Override - public int damageDropped(IBlockState blockState) { + public int damageDropped(final IBlockState blockState) { return getMetaFromState(blockState); } @@ -218,7 +218,8 @@ public class BlockEnergyBank extends BlockAbstractContainer implements IExplosio } final TileEntityEnergyBank tileEntityEnergyBank = (TileEntityEnergyBank) tileEntity; - if (itemStackHeld != null && itemStackHeld.getItem() instanceof IWarpTool) { + if ( itemStackHeld != null + && itemStackHeld.getItem() instanceof IWarpTool ) { if (entityPlayer.isSneaking()) { tileEntityEnergyBank.setMode(side, tileEntityEnergyBank.getMode(side).getPrevious()); } else { @@ -243,17 +244,20 @@ public class BlockEnergyBank extends BlockAbstractContainer implements IExplosio } EnumComponentType enumComponentType = null; - if (itemStackHeld != null && itemStackHeld.getItem() instanceof ItemComponent) { + if ( itemStackHeld != null + && itemStackHeld.getItem() instanceof ItemComponent ) { enumComponentType = EnumComponentType.get(itemStackHeld.getItemDamage()); } // sneaking with an empty hand or an upgrade item in hand to dismount current upgrade if (entityPlayer.isSneaking()) { - // using an upgrade item or an empty means dismount upgrade - if (itemStackHeld == null || enumComponentType != null) { + // using an upgrade item or an empty hand means dismount upgrade + if ( itemStackHeld == null + || enumComponentType != null ) { // find a valid upgrade to dismount - if (itemStackHeld == null || !tileEntityEnergyBank.hasUpgrade(enumComponentType)) { - enumComponentType = (EnumComponentType)tileEntityEnergyBank.getFirstUpgradeOfType(EnumComponentType.class, null); + if ( itemStackHeld == null + || !tileEntityEnergyBank.hasUpgrade(enumComponentType) ) { + enumComponentType = (EnumComponentType) tileEntityEnergyBank.getFirstUpgradeOfType(EnumComponentType.class, null); } if (enumComponentType == null) { @@ -264,8 +268,8 @@ public class BlockEnergyBank extends BlockAbstractContainer implements IExplosio if (!entityPlayer.capabilities.isCreativeMode) { // dismount the current upgrade item - ItemStack itemStackDrop = ItemComponent.getItemStackNoCache(enumComponentType, 1); - EntityItem entityItem = new EntityItem(world, entityPlayer.posX, entityPlayer.posY + 0.5D, entityPlayer.posZ, itemStackDrop); + final ItemStack itemStackDrop = ItemComponent.getItemStackNoCache(enumComponentType, 1); + final EntityItem entityItem = new EntityItem(world, entityPlayer.posX, entityPlayer.posY + 0.5D, entityPlayer.posZ, itemStackDrop); entityItem.setNoPickupDelay(); world.spawnEntityInWorld(entityItem); } diff --git a/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorCore.java b/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorCore.java index 10d38e78..3e45ebc0 100644 --- a/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorCore.java +++ b/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnanReactorCore.java @@ -728,10 +728,10 @@ public class TileEntityEnanReactorCore extends TileEntityAbstractEnergy implemen @Override public String toString() { - return String.format("%s %s @ %s (%d %d %d)", - getClass().getSimpleName(), - connectedComputers == null ? "~NULL~" : connectedComputers, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - pos.getX(), pos.getY(), pos.getZ()); + return String.format("%s %s @ %s (%d %d %d)", + getClass().getSimpleName(), + connectedComputers == null ? "~NULL~" : connectedComputers, + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnergyBank.java b/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnergyBank.java index 0164c45f..3dfd11db 100644 --- a/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnergyBank.java +++ b/src/main/java/cr0s/warpdrive/block/energy/TileEntityEnergyBank.java @@ -1,6 +1,5 @@ package cr0s.warpdrive.block.energy; -import cr0s.warpdrive.Commons; import cr0s.warpdrive.block.TileEntityAbstractEnergy; import cr0s.warpdrive.config.WarpDriveConfig; import cr0s.warpdrive.data.EnumComponentType; @@ -57,7 +56,7 @@ public class TileEntityEnergyBank extends TileEntityAbstractEnergy { } private double getEfficiency() { - int upgradeCount = Commons.clamp(0, getUpgradeMaxCount(EnumComponentType.SUPERCONDUCTOR), getUpgradeCount(EnumComponentType.SUPERCONDUCTOR)); + final int upgradeCount = getValidUpgradeCount(EnumComponentType.SUPERCONDUCTOR); return WarpDriveConfig.ENERGY_BANK_EFFICIENCY_PER_UPGRADE[upgradeCount]; } @@ -192,9 +191,9 @@ public class TileEntityEnergyBank extends TileEntityAbstractEnergy { @Override public String toString() { return String.format("%s @ %s (%d %d %d) %8d", - getClass().getSimpleName(), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - pos.getX(), pos.getY(), pos.getZ(), - energy_getEnergyStored()); + getClass().getSimpleName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ(), + energy_getEnergyStored()); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityAbstractForceField.java b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityAbstractForceField.java index f06e4dae..28cc786f 100644 --- a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityAbstractForceField.java +++ b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityAbstractForceField.java @@ -222,7 +222,7 @@ public class TileEntityAbstractForceField extends TileEntityAbstractEnergy imple return String.format("%s Beam \'%d\' @ %s (%d %d %d)", getClass().getSimpleName(), beamFrequency, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), pos.getX(), pos.getY(), pos.getZ()); } } diff --git a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java index a6435602..404fb1b0 100644 --- a/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java +++ b/src/main/java/cr0s/warpdrive/block/forcefield/TileEntityForceField.java @@ -121,8 +121,9 @@ public class TileEntityForceField extends TileEntityAbstractBase { // projector is disabled or out of power worldObj.setBlockToAir(pos); if (WarpDriveConfig.LOGGING_FORCEFIELD) { - WarpDrive.logger.info("Removed a force field from an offline projector at " - + (worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName()) + " " + pos.getX() + " " + pos.getY() + " " + pos.getZ()); + WarpDrive.logger.info(String.format("Removed a force field from an offline projector @ %s (%d %d %d)", + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ())); } } } @@ -134,8 +135,9 @@ public class TileEntityForceField extends TileEntityAbstractBase { if (gracePeriod_calls < 0) { worldObj.setBlockToAir(pos); if (WarpDriveConfig.LOGGING_FORCEFIELD) { - WarpDrive.logger.info("Removed a force field with no projector defined at " - + (worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName()) + " " + pos.getX() + " " + pos.getY() + " " + pos.getZ()); + WarpDrive.logger.info(String.format("Removed a force field with no projector defined @ %s (%d %d %d)", + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ())); } } } diff --git a/src/main/java/cr0s/warpdrive/block/movement/BlockShipController.java b/src/main/java/cr0s/warpdrive/block/movement/BlockShipController.java index 634d172a..30020dbc 100644 --- a/src/main/java/cr0s/warpdrive/block/movement/BlockShipController.java +++ b/src/main/java/cr0s/warpdrive/block/movement/BlockShipController.java @@ -29,7 +29,7 @@ public class BlockShipController extends BlockAbstractContainer { @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityShipController(); } diff --git a/src/main/java/cr0s/warpdrive/block/movement/BlockShipCore.java b/src/main/java/cr0s/warpdrive/block/movement/BlockShipCore.java index 333ad442..256f8046 100644 --- a/src/main/java/cr0s/warpdrive/block/movement/BlockShipCore.java +++ b/src/main/java/cr0s/warpdrive/block/movement/BlockShipCore.java @@ -41,7 +41,7 @@ public class BlockShipCore extends BlockAbstractContainer { @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityShipCore(); } diff --git a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java index 2bd355f1..b035dd56 100644 --- a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java +++ b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipController.java @@ -800,7 +800,7 @@ public class TileEntityShipController extends TileEntityAbstractInterfaced imple return String.format("%s \'%s\' @ %s (%d %d %d)", getClass().getSimpleName(), tileEntityShipCore == null ? "-NULL-" : tileEntityShipCore.shipName, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), pos.getX(), pos.getY(), pos.getZ()); } } diff --git a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java index 1ce4a2b7..1324c64d 100644 --- a/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java +++ b/src/main/java/cr0s/warpdrive/block/movement/TileEntityShipCore.java @@ -1242,11 +1242,10 @@ public class TileEntityShipCore extends TileEntityAbstractEnergy implements ISta @Override public String toString() { - return String.format( - "%s \'%s\' @ %s (%d %d %d)", - getClass().getSimpleName(), - shipName, - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), - pos.getX(), pos.getY(), pos.getZ()); + return String.format("%s \'%s\' @ %s (%d %d %d)", + getClass().getSimpleName(), + shipName, + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), + pos.getX(), pos.getY(), pos.getZ()); } } diff --git a/src/main/java/cr0s/warpdrive/block/movement/TileEntityTransporter.java b/src/main/java/cr0s/warpdrive/block/movement/TileEntityTransporter.java index f042d9c6..edc99d5b 100644 --- a/src/main/java/cr0s/warpdrive/block/movement/TileEntityTransporter.java +++ b/src/main/java/cr0s/warpdrive/block/movement/TileEntityTransporter.java @@ -510,8 +510,8 @@ public class TileEntityTransporter extends TileEntityAbstractEnergy { @Override public int energy_getMaxStorage() { - int energyUgrades = getUpgradeCount(ItemUpgrade.getItemStack(UpgradeType.Energy)); - int max = (int) Math.floor(WarpDriveConfig.TRANSPORTER_MAX_ENERGY_STORED * Math.pow(1.2, energyUgrades)); + int energyUpgrades = getUpgradeCount(ItemUpgrade.getItemStack(UpgradeType.Energy)); + int max = (int) Math.floor(WarpDriveConfig.TRANSPORTER_MAX_ENERGY_STORED * Math.pow(1.2, energyUpgrades)); return max; } diff --git a/src/main/java/cr0s/warpdrive/block/passive/BlockTransportBeacon.java b/src/main/java/cr0s/warpdrive/block/passive/BlockTransportBeacon.java index a82fa788..f3c3bf85 100644 --- a/src/main/java/cr0s/warpdrive/block/passive/BlockTransportBeacon.java +++ b/src/main/java/cr0s/warpdrive/block/passive/BlockTransportBeacon.java @@ -25,7 +25,7 @@ public class BlockTransportBeacon extends BlockTorch { } @SideOnly(Side.CLIENT) - public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos, Random rand) { + public void randomDisplayTick(final IBlockState stateIn, final World worldIn, final BlockPos pos, final Random rand) { EnumFacing enumfacing = stateIn.getValue(FACING); double d0 = (double)pos.getX() + 0.5D; double d1 = (double)pos.getY() + 0.7D; diff --git a/src/main/java/cr0s/warpdrive/block/weapon/BlockWeaponController.java b/src/main/java/cr0s/warpdrive/block/weapon/BlockWeaponController.java index 76e4984f..034b450f 100644 --- a/src/main/java/cr0s/warpdrive/block/weapon/BlockWeaponController.java +++ b/src/main/java/cr0s/warpdrive/block/weapon/BlockWeaponController.java @@ -32,7 +32,7 @@ public class BlockWeaponController extends BlockAbstractContainer { @Nonnull @Override - public TileEntity createNewTileEntity(@Nonnull World world, int metadata) { + public TileEntity createNewTileEntity(@Nonnull final World world, final int metadata) { return new TileEntityWeaponController(); } diff --git a/src/main/java/cr0s/warpdrive/block/weapon/TileEntityLaserCamera.java b/src/main/java/cr0s/warpdrive/block/weapon/TileEntityLaserCamera.java index 92d579a8..d612c983 100644 --- a/src/main/java/cr0s/warpdrive/block/weapon/TileEntityLaserCamera.java +++ b/src/main/java/cr0s/warpdrive/block/weapon/TileEntityLaserCamera.java @@ -194,6 +194,7 @@ public class TileEntityLaserCamera extends TileEntityLaser implements IVideoChan beamFrequency, videoChannel, worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), pos.getX(), pos.getY(), pos.getZ()); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/block/weapon/TileEntityWeaponController.java b/src/main/java/cr0s/warpdrive/block/weapon/TileEntityWeaponController.java index 299b82a3..0fcb4ab8 100644 --- a/src/main/java/cr0s/warpdrive/block/weapon/TileEntityWeaponController.java +++ b/src/main/java/cr0s/warpdrive/block/weapon/TileEntityWeaponController.java @@ -35,8 +35,8 @@ public class TileEntityWeaponController extends TileEntityAbstractInterfaced { @Override public String toString() { return String.format("%s @ %s (%d %d %d)", - getClass().getSimpleName(), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + getClass().getSimpleName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), pos.getX(), pos.getY(), pos.getZ()); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/command/CommandSpace.java b/src/main/java/cr0s/warpdrive/command/CommandSpace.java index 873f691b..053a25b4 100644 --- a/src/main/java/cr0s/warpdrive/command/CommandSpace.java +++ b/src/main/java/cr0s/warpdrive/command/CommandSpace.java @@ -175,7 +175,7 @@ public class CommandSpace extends CommandBase { } // inform player - String message = "§aTeleporting player " + entityPlayerMP.getName() + " to dimension " + dimensionIdTarget + "..."; // + ":" + worldTarget.getWorldInfo().getWorldName(); + String message = "§aTeleporting player " + entityPlayerMP.getName() + " to dimension " + dimensionIdTarget + "..."; // + ":" + worldTarget.provider.getSaveFolder(); Commons.addChatMessage(commandSender, new TextComponentString(message)); WarpDrive.logger.info(message); if (commandSender != entityPlayerMP) { diff --git a/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java b/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java index ac766de7..cd92273c 100644 --- a/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java +++ b/src/main/java/cr0s/warpdrive/config/WarpDriveConfig.java @@ -202,6 +202,7 @@ public class WarpDriveConfig { public static boolean LOGGING_XML_PREPROCESSOR = false; public static boolean LOGGING_RENDERING = false; public static boolean LOGGING_CHUNK_HANDLER = false; + public static boolean LOGGING_CHUNK_LOADING = true; public static boolean LOGGING_CLIENT_SYNCHRONIZATION = false; // Starmap @@ -378,9 +379,9 @@ public class WarpDriveConfig { public static int LIFT_ENTITY_COOLDOWN_TICKS = 40; // Chunk loader - public static int CL_MAX_ENERGY = 1000000; - public static int CL_MAX_DISTANCE = 2; - public static int CL_RF_PER_CHUNKTICK = 320; + public static int CHUNK_LOADER_MAX_ENERGY_STORED = 1000000; + public static int CHUNK_LOADER_MAX_RADIUS = 2; + public static int CHUNK_LOADER_ENERGY_PER_CHUNK = 8; // Hull public static float[] HULL_HARDNESS = { 25.0F, 50.0F, 80.0F }; @@ -673,6 +674,7 @@ public class WarpDriveConfig { LOGGING_XML_PREPROCESSOR = config.get("logging", "enable_XML_preprocessor_logs", LOGGING_XML_PREPROCESSOR, "Save XML preprocessor results as output*.xml file, enable it to debug your XML configuration files").getBoolean(false); LOGGING_RENDERING = config.get("logging", "enable_rendering_logs", LOGGING_RENDERING, "Detailed rendering logs to help debug the mod.").getBoolean(false); LOGGING_CHUNK_HANDLER = config.get("logging", "enable_chunk_handler_logs", LOGGING_CHUNK_HANDLER, "Detailed chunk data logs to help debug the mod.").getBoolean(false); + LOGGING_CHUNK_LOADING = config.get("logging", "enable_chunk_loading_logs", LOGGING_CHUNK_LOADING, "Chunk loading logs, enable it to report chunk loaders updates").getBoolean(false); // Starmap registry STARMAP_REGISTRY_UPDATE_INTERVAL_SECONDS = Commons.clamp(0, 300, @@ -1011,6 +1013,16 @@ public class WarpDriveConfig { LIFT_ENTITY_COOLDOWN_TICKS = Commons.clamp(1, 6000, config.get("lift", "entity_cooldown_ticks", LIFT_ENTITY_COOLDOWN_TICKS, "Cooldown after moving an entity").getInt()); + + // Chunk loader + CHUNK_LOADER_MAX_ENERGY_STORED = Commons.clamp(1, Integer.MAX_VALUE, + config.get("chunk_loader", "max_energy_stored", CHUNK_LOADER_MAX_ENERGY_STORED, "Maximum energy stored").getInt()); + CHUNK_LOADER_MAX_RADIUS = Commons.clamp(1, Integer.MAX_VALUE, + config.get("chunk_loader", "max_radius", CHUNK_LOADER_MAX_RADIUS, "Maximum radius when loading a square shape, measured in chunks. A linear shape can be up to 1 chunk wide by (radius + 1 + radius) ^ 2 chunks long.").getInt()); + CHUNK_LOADER_ENERGY_PER_CHUNK = Commons.clamp(1, 100, + config.get("chunk_loader", "energy_per_chunk", CHUNK_LOADER_ENERGY_PER_CHUNK, "Energy consumed per chunk loaded").getInt()); + + // Particles accelerator ACCELERATOR_ENABLE = config.get("accelerator", "enable", ACCELERATOR_ENABLE, "Enable accelerator blocks. Requires a compatible server, as it won't work in single player").getBoolean(false); diff --git a/src/main/java/cr0s/warpdrive/data/JumpShip.java b/src/main/java/cr0s/warpdrive/data/JumpShip.java index 7107742b..4f33a040 100644 --- a/src/main/java/cr0s/warpdrive/data/JumpShip.java +++ b/src/main/java/cr0s/warpdrive/data/JumpShip.java @@ -229,7 +229,7 @@ public class JumpShip { return String.format("%s/%d \'%s\' @ %s (%d %d %d)", getClass().getSimpleName(), hashCode(), shipCore == null ? "~NULL~" : (shipCore.uuid + ":" + shipCore.shipName), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), core.getX(), core.getY(), core.getZ()); } diff --git a/src/main/java/cr0s/warpdrive/entity/EntityParticleBunch.java b/src/main/java/cr0s/warpdrive/entity/EntityParticleBunch.java index a56c0e9e..8f178643 100644 --- a/src/main/java/cr0s/warpdrive/entity/EntityParticleBunch.java +++ b/src/main/java/cr0s/warpdrive/entity/EntityParticleBunch.java @@ -46,7 +46,7 @@ public class EntityParticleBunch extends Entity { public EntityParticleBunch(World world) { super(world); if (WarpDriveConfig.LOGGING_ACCELERATOR) { - WarpDrive.logger.info(this + " created in dimension '" + worldObj.getWorldInfo().getWorldName() + "'"); + WarpDrive.logger.info(this + " created in dimension " + worldObj.provider.getSaveFolder()); } } @@ -197,7 +197,7 @@ public class EntityParticleBunch extends Entity { return String.format("%s/%d @ \'%s\' %.2f %.2f %.2f", getClass().getSimpleName(), getEntityId(), - worldObj == null ? "~NULL~" : worldObj.getWorldInfo().getWorldName(), + worldObj == null ? "~NULL~" : worldObj.provider.getSaveFolder(), posX, posY, posZ); } } \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/event/ChunkHandler.java b/src/main/java/cr0s/warpdrive/event/ChunkHandler.java index 7324f518..c29746a9 100644 --- a/src/main/java/cr0s/warpdrive/event/ChunkHandler.java +++ b/src/main/java/cr0s/warpdrive/event/ChunkHandler.java @@ -215,7 +215,7 @@ public class ChunkHandler { if (WarpDriveConfig.LOGGING_CHUNK_HANDLER) { WarpDrive.logger.info(String.format("%s world %s chunk %s unwatch by %s", event.player.worldObj.isRemote ? "Client" : "Server", - event.player.worldObj.provider.getDimensionName(), + event.player.worldObj.provider.getSaveFolder(), event.chunk, event.player)); } diff --git a/src/main/java/cr0s/warpdrive/event/ChunkLoadingHandler.java b/src/main/java/cr0s/warpdrive/event/ChunkLoadingHandler.java new file mode 100644 index 00000000..f48791ff --- /dev/null +++ b/src/main/java/cr0s/warpdrive/event/ChunkLoadingHandler.java @@ -0,0 +1,143 @@ +package cr0s.warpdrive.event; + +import com.google.common.collect.ImmutableSet; +import cr0s.warpdrive.WarpDrive; +import cr0s.warpdrive.block.TileEntityAbstractChunkLoading; +import cr0s.warpdrive.config.WarpDriveConfig; + +import java.util.List; + +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.ChunkPos; +import net.minecraft.world.World; + +import net.minecraftforge.common.ForgeChunkManager; +import net.minecraftforge.common.ForgeChunkManager.LoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.Ticket; +import net.minecraftforge.common.ForgeChunkManager.Type; + +public class ChunkLoadingHandler implements LoadingCallback { + + public final static ChunkLoadingHandler INSTANCE = new ChunkLoadingHandler(); + + /* event catchers */ + @Override + public void ticketsLoaded(List tickets, World world) { + for (final Ticket ticket : tickets) { + final NBTTagCompound tagCompound = ticket.getModData(); + if ( !tagCompound.hasKey("posX") + || !tagCompound.hasKey("posY") + || !tagCompound.hasKey("posZ") ) { + WarpDrive.logger.error(String.format("Unable to resume chunkloading: incomplete or corrupted NBT data %s", + tagCompound)); + ForgeChunkManager.releaseTicket(ticket); + continue; + } + + final int x = tagCompound.getInteger("posX"); + final int y = tagCompound.getInteger("posY"); + final int z = tagCompound.getInteger("posZ"); + final TileEntity tileEntity = world.getTileEntity(new BlockPos(x, y, z)); + if (!(tileEntity instanceof TileEntityAbstractChunkLoading)) { + WarpDrive.logger.error(String.format("Unable to resume chunkloading @ %s (%d %d %d): invalid tile entity %s", + world.provider.getSaveFolder(), + x, y, z, + tileEntity == null ? "-null-" : tileEntity)); + ForgeChunkManager.releaseTicket(ticket); + continue; + } + + final TileEntityAbstractChunkLoading tileEntityAbstractChunkLoading = (TileEntityAbstractChunkLoading) tileEntity; + final boolean shouldChunkLoad = tileEntityAbstractChunkLoading.shouldChunkLoad(); + if (!shouldChunkLoad) { + WarpDrive.logger.warn(String.format("Unable to resume chunkloading @ %s (%d %d %d): chunk loader is disabled or out of power %s", + world.provider.getSaveFolder(), + x, y, z, + tileEntity)); + ForgeChunkManager.releaseTicket(ticket); + continue; + } + + WarpDrive.logger.info(String.format("Resuming chunkloading of %s", tileEntity)); + tileEntityAbstractChunkLoading.chunkloading_giveTicket(ticket); + tileEntityAbstractChunkLoading.refreshChunkLoading(); + } + } + + /* Forge wrappers */ + public static Ticket forgeTicket_requestNormal(final World world, final TileEntity tileEntity) { + if (ForgeChunkManager.ticketCountAvailableFor(WarpDrive.instance, world) <= 0) { + WarpDrive.logger.error(String.format("No ChunkLoader tickets available for %s", + world.provider.getSaveFolder())); + return null; + } + + final Ticket ticket = ForgeChunkManager.requestTicket(WarpDrive.instance, world, Type.NORMAL); + if (ticket == null) { + WarpDrive.logger.error(String.format("Failed to register ChunkLoader Ticket for %s", + world.provider.getSaveFolder())); + return null; + } + + if (WarpDriveConfig.LOGGING_CHUNK_LOADING) { + WarpDrive.logger.info(String.format("Forcing chunk loading @ %s (%d %d %d)", + ticket.world.provider.getSaveFolder(), + tileEntity.getPos().getX(), tileEntity.getPos().getY(), tileEntity.getPos().getZ())); + } + + final NBTTagCompound tagCompound = ticket.getModData(); + tagCompound.setString("id", tileEntity.getClass().getSimpleName()); + tagCompound.setInteger("posX", tileEntity.getPos().getX()); + tagCompound.setInteger("posY", tileEntity.getPos().getY()); + tagCompound.setInteger("posZ", tileEntity.getPos().getZ()); + + return ticket; + } + + public static void forgeTicket_release(final Ticket ticket) { + if (ticket == null) { + return; + } + + forgeTicket_clearChunks(ticket); + + ForgeChunkManager.releaseTicket(ticket); + } + + public static void forgeTicket_addChunks(final Ticket ticket, final ChunkPos chunk) { + if (WarpDrive.isDev) { + WarpDrive.logger.info(String.format("Forcing chunk loading @ %s %s", + ticket.world.provider.getSaveFolder(), + chunk)); + } + ForgeChunkManager.forceChunk(ticket, chunk); + } + + public static void forgeTicket_clearChunks(final Ticket ticket) { + if (ticket == null) { + return; + } + + if (WarpDriveConfig.LOGGING_CHUNK_LOADING) { + final NBTTagCompound tagCompound = ticket.getModData(); + final int x = tagCompound.getInteger("posX"); + final int y = tagCompound.getInteger("posY"); + final int z = tagCompound.getInteger("posZ"); + WarpDrive.logger.info(String.format("Releasing chunk loading @ %s (%d %d %d)", + ticket.world.provider.getSaveFolder(), + x, y, z)); + } + + final ImmutableSet chunks = ticket.getChunkList(); + for (final ChunkPos chunk : chunks) { + if (WarpDrive.isDev) { + WarpDrive.logger.info(String.format("Releasing chunk loading @ %s %s", + ticket.world.provider.getSaveFolder(), + chunk)); + } + ForgeChunkManager.unforceChunk(ticket, chunk); + } + } +} \ No newline at end of file diff --git a/src/main/java/cr0s/warpdrive/event/JumpSequencer.java b/src/main/java/cr0s/warpdrive/event/JumpSequencer.java index f17dc243..2e663285 100644 --- a/src/main/java/cr0s/warpdrive/event/JumpSequencer.java +++ b/src/main/java/cr0s/warpdrive/event/JumpSequencer.java @@ -322,7 +322,7 @@ public class JumpSequencer extends AbstractSequencer { sourceWorldTicket = ForgeChunkManager.requestTicket(WarpDrive.instance, sourceWorld, Type.NORMAL); if (sourceWorldTicket == null) { reason.append(String.format("Chunkloading rejected in source world %s. Aborting.", - sourceWorld.getWorldInfo().getWorldName())); + sourceWorld.provider.getSaveFolder())); return false; } @@ -354,7 +354,7 @@ public class JumpSequencer extends AbstractSequencer { targetWorldTicket = ForgeChunkManager.requestTicket(WarpDrive.instance, targetWorld, Type.NORMAL); if (targetWorldTicket == null) { reason.append(String.format("Chunkloading rejected in target world %s. Aborting.", - targetWorld.getWorldInfo().getWorldName())); + targetWorld.provider.getSaveFolder())); return false; } @@ -1588,7 +1588,7 @@ public class JumpSequencer extends AbstractSequencer { return String.format("%s/%d \'%s\' @ %s (%d %d %d) #%d", getClass().getSimpleName(), hashCode(), (ship == null || ship.shipCore == null) ? "~NULL~" : (ship.shipCore.uuid + ":" + ship.shipCore.shipName), - sourceWorld == null ? "~NULL~" : sourceWorld.getWorldInfo().getWorldName(), + sourceWorld == null ? "~NULL~" : sourceWorld.provider.getSaveFolder(), ship == null ? -1 : ship.core.getX(), ship == null ? -1 : ship.core.getY(), ship == null ? -1 : ship.core.getZ(), ticks); } diff --git a/src/main/java/cr0s/warpdrive/item/ItemComponent.java b/src/main/java/cr0s/warpdrive/item/ItemComponent.java index b7b15da1..fcdea9eb 100644 --- a/src/main/java/cr0s/warpdrive/item/ItemComponent.java +++ b/src/main/java/cr0s/warpdrive/item/ItemComponent.java @@ -51,7 +51,7 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem @Nonnull @Override - public String getUnlocalizedName(ItemStack itemStack) { + public String getUnlocalizedName(final ItemStack itemStack) { int damage = itemStack.getItemDamage(); if (damage >= 0 && damage < EnumComponentType.length) { return "item.warpdrive.component." + EnumComponentType.get(damage).getUnlocalizedName(); @@ -60,7 +60,7 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem } @Override - public void getSubItems(@Nonnull Item item, @Nonnull CreativeTabs creativeTabs, @Nonnull List subItems) { + public void getSubItems(@Nonnull final Item item, @Nonnull final CreativeTabs creativeTabs, @Nonnull final List subItems) { for(EnumComponentType enumComponentType : EnumComponentType.values()) { subItems.add(new ItemStack(item, 1, enumComponentType.ordinal())); } @@ -69,7 +69,7 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem @Nonnull @Override @SideOnly(Side.CLIENT) - public ModelResourceLocation getModelResourceLocation(ItemStack itemStack) { + public ModelResourceLocation getModelResourceLocation(final ItemStack itemStack) { int damage = itemStack.getItemDamage(); ResourceLocation resourceLocation = getRegistryName(); if (damage >= 0 && damage < EnumComponentType.length) { @@ -80,12 +80,12 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem // IAirContainerItem overrides for empty air canister @Override - public boolean canContainAir(ItemStack itemStack) { + public boolean canContainAir(final ItemStack itemStack) { return (itemStack.getItem() instanceof ItemComponent && itemStack.getItemDamage() == EnumComponentType.AIR_CANISTER.ordinal()); } @Override - public int getMaxAirStorage(ItemStack itemStack) { + public int getMaxAirStorage(final ItemStack itemStack) { if (canContainAir(itemStack)) { return WarpDrive.itemAirTanks[0].getMaxAirStorage(itemStack); } else { @@ -94,18 +94,18 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem } @Override - public int getCurrentAirStorage(ItemStack itemStack) { + public int getCurrentAirStorage(final ItemStack itemStack) { return 0; } @Override - public ItemStack consumeAir(ItemStack itemStack) { + public ItemStack consumeAir(final ItemStack itemStack) { WarpDrive.logger.error(this + " consumeAir() with itemStack " + itemStack); throw new RuntimeException("Invalid call to consumeAir() on non or empty container"); } @Override - public int getAirTicksPerConsumption(ItemStack itemStack) { + public int getAirTicksPerConsumption(final ItemStack itemStack) { if (canContainAir(itemStack)) { return WarpDrive.itemAirTanks[0].getAirTicksPerConsumption(itemStack); } else { @@ -114,7 +114,7 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem } @Override - public ItemStack getFullAirContainer(ItemStack itemStack) { + public ItemStack getFullAirContainer(final ItemStack itemStack) { if (canContainAir(itemStack)) { return WarpDrive.itemAirTanks[0].getFullAirContainer(itemStack); } @@ -122,7 +122,7 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem } @Override - public ItemStack getEmptyAirContainer(ItemStack itemStack) { + public ItemStack getEmptyAirContainer(final ItemStack itemStack) { if (canContainAir(itemStack)) { return WarpDrive.itemAirTanks[0].getEmptyAirContainer(itemStack); } @@ -132,13 +132,15 @@ public class ItemComponent extends ItemAbstractBase implements IAirContainerItem @Override - public boolean doesSneakBypassUse(ItemStack itemStack, IBlockAccess world, BlockPos blockPos, EntityPlayer player) { - Block block = world.getBlockState(blockPos).getBlock(); - return block instanceof BlockEnergyBank || super.doesSneakBypassUse(itemStack, world, blockPos, player); + public boolean doesSneakBypassUse(final ItemStack itemStack, final IBlockAccess world, final BlockPos blockPos, final EntityPlayer player) { + final Block block = world.getBlockState(blockPos).getBlock(); + + return block instanceof BlockEnergyBank + || super.doesSneakBypassUse(itemStack, world, blockPos, player); } @Override - public void addInformation(ItemStack itemStack, EntityPlayer entityPlayer, List list, boolean advancedItemTooltips) { + public void addInformation(final ItemStack itemStack, final EntityPlayer entityPlayer, final List list, final boolean advancedItemTooltips) { super.addInformation(itemStack, entityPlayer, list, advancedItemTooltips); String tooltip = ""; diff --git a/src/main/resources/assets/warpdrive/textures/blocks/chunkLoader.psd b/src/main/resources/assets/warpdrive/textures/blocks/chunkLoader.psd deleted file mode 100644 index c652bc53..00000000 Binary files a/src/main/resources/assets/warpdrive/textures/blocks/chunkLoader.psd and /dev/null differ diff --git a/src/main/resources/assets/warpdrive/textures/blocks/chunkLoader.png b/src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-active.png similarity index 100% rename from src/main/resources/assets/warpdrive/textures/blocks/chunkLoader.png rename to src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-active.png diff --git a/src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-offline.png b/src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-offline.png new file mode 100644 index 00000000..4de2e952 Binary files /dev/null and b/src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-offline.png differ diff --git a/src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-out_of_power.png b/src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-out_of_power.png new file mode 100644 index 00000000..4de2e952 Binary files /dev/null and b/src/main/resources/assets/warpdrive/textures/blocks/chunk_loader-out_of_power.png differ