diff options
Diffstat (limited to 'src/Java')
13 files changed, 941 insertions, 390 deletions
diff --git a/src/Java/gtPlusPlus/GTplusplus.java b/src/Java/gtPlusPlus/GTplusplus.java index 219117fd8f..b6a14acb62 100644 --- a/src/Java/gtPlusPlus/GTplusplus.java +++ b/src/Java/gtPlusPlus/GTplusplus.java @@ -21,12 +21,12 @@ import gregtech.api.util.GT_Recipe.GT_Recipe_Map; import gtPlusPlus.api.objects.Logger; import gtPlusPlus.api.objects.data.AutoMap; import gtPlusPlus.api.objects.data.Pair; -import gtPlusPlus.api.objects.minecraft.ChunkManager; import gtPlusPlus.core.commands.CommandMath; import gtPlusPlus.core.common.CommonProxy; import gtPlusPlus.core.config.ConfigHandler; import gtPlusPlus.core.handler.BookHandler; import gtPlusPlus.core.handler.Recipes.RegistrationHandler; +import gtPlusPlus.core.handler.chunkloading.ChunkLoading; import gtPlusPlus.core.handler.events.*; import gtPlusPlus.core.lib.CORE; import gtPlusPlus.core.lib.LoadedMods; @@ -67,6 +67,9 @@ public class GTplusplus implements ActionListener { //GT++ Proxy Instances @SidedProxy(clientSide = "gtPlusPlus.core.proxy.ClientProxy", serverSide = "gtPlusPlus.core.proxy.ServerProxy") public static CommonProxy proxy; + + //Chunk handler + public static ChunkLoading mChunkLoading; // Loads Textures @SideOnly(value = Side.CLIENT) @@ -78,6 +81,11 @@ public class GTplusplus implements ActionListener { // Blocks Logger.WARNING("Processing texture: " + TexturesGtBlock.Casing_Machine_Dimensional.getTextureFile().getResourcePath()); } + + public GTplusplus() { + super(); + mChunkLoading = new ChunkLoading(); + } // Pre-Init @Mod.EventHandler @@ -114,7 +122,7 @@ public class GTplusplus implements ActionListener { Logger.INFO("Login Handler Initialized"); - + mChunkLoading.preInit(event); proxy.preInit(event); Core_Manager.preInit(); } @@ -122,6 +130,7 @@ public class GTplusplus implements ActionListener { // Init @Mod.EventHandler public void init(final FMLInitializationEvent event) { + mChunkLoading.init(event); proxy.init(event); proxy.registerNetworkStuff(); @@ -142,6 +151,7 @@ public class GTplusplus implements ActionListener { // Post-Init @Mod.EventHandler public void postInit(final FMLPostInitializationEvent event) { + mChunkLoading.postInit(event); proxy.postInit(event); BookHandler.runLater(); Core_Manager.postInit(); @@ -160,6 +170,7 @@ public class GTplusplus implements ActionListener { @EventHandler public synchronized void serverStarting(final FMLServerStartingEvent event) { + mChunkLoading.serverStarting(event); event.registerServerCommand(new CommandMath()); if (LoadedMods.Thaumcraft) { event.registerServerCommand(new CommandDumpAspects()); @@ -168,12 +179,7 @@ public class GTplusplus implements ActionListener { @Mod.EventHandler public synchronized void serverStopping(final FMLServerStoppingEvent event) { - //Chunkload Handler - if (ChunkManager.mChunkLoaderManagerMap.size() > 0) { - Logger.INFO("Clearing Chunk Loaders."); - ChunkManager.clearInternalMaps(); - } - + mChunkLoading.serverStopping(event); if (GregtechBufferThread.mBufferThreadAllocation.size() > 0) { for (GregtechBufferThread i : GregtechBufferThread.mBufferThreadAllocation.values()) { i.destroy(); diff --git a/src/Java/gtPlusPlus/RoadMap.java b/src/Java/gtPlusPlus/RoadMap.java index 11714ef2cb..47a0116192 100644 --- a/src/Java/gtPlusPlus/RoadMap.java +++ b/src/Java/gtPlusPlus/RoadMap.java @@ -1,12 +1,5 @@ package gtPlusPlus; -import java.util.Iterator; -import java.util.LinkedHashMap; - -import gregtech.api.util.GT_Utility; -import gtPlusPlus.api.objects.data.ObjMap; -import net.minecraft.item.ItemStack; - /** * This Class purely exists to note down ideas and or plans to (re)implement things. * @@ -70,6 +63,32 @@ public class RoadMap { return false; }*/ - +/* private static final LinkedHashMap<String, ObjMap<Integer, Boolean>>mCachedResults = new LinkedHashMap<String, ObjMap<Integer, Boolean>>(); + + public boolean contains(ItemStack aStack) { + if (aStack == null){ + return false; + } + ObjMap<Integer, Boolean> aCurrentSet = mCachedResults.get(this.toString().toUpperCase()); + if (aCurrentSet == null){ + aCurrentSet = new ObjMap<Integer, Boolean>((mPrefixedItems != null && mPrefixedItems.size() > 0 ? mPrefixedItems.size() : 1000), 0.5f); + mCachedResults.put(this.toString().toUpperCase(), aCurrentSet); + } + int mainHash = Objects.hashCode(aStack.getItem(), aStack.getItemDamage()); + Boolean result = aCurrentSet.get(mainHash); + if (result != null){ + return result; + } + else { + for (ItemStack tStack : mPrefixedItems){ + if (GT_Utility.areStacksEqual(aStack, tStack, !tStack.hasTagCompound())){ + aCurrentSet.put(Objects.hashCode(tStack.getItem(), tStack.getItemDamage()), true); + return true; + } + } + } + aCurrentSet.put(mainHash, false); + return false; + }*/ } diff --git a/src/Java/gtPlusPlus/api/interfaces/IGregtechPacketEntity.java b/src/Java/gtPlusPlus/api/interfaces/IGregtechPacketEntity.java new file mode 100644 index 0000000000..fa59c58d9f --- /dev/null +++ b/src/Java/gtPlusPlus/api/interfaces/IGregtechPacketEntity.java @@ -0,0 +1,13 @@ +package gtPlusPlus.api.interfaces; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +public interface IGregtechPacketEntity { + + public void writePacketData(DataOutputStream data) throws IOException; + + public void readPacketData(DataInputStream data) throws IOException; + +} diff --git a/src/Java/gtPlusPlus/api/objects/minecraft/ChunkManager.java b/src/Java/gtPlusPlus/api/objects/minecraft/ChunkManager.java index b0975dcca7..826f0c5106 100644 --- a/src/Java/gtPlusPlus/api/objects/minecraft/ChunkManager.java +++ b/src/Java/gtPlusPlus/api/objects/minecraft/ChunkManager.java @@ -344,8 +344,8 @@ public class ChunkManager implements LoadingCallback, OrderedLoadingCallback, Fo if (H != null) { ForgeChunkManager.releaseTicket(H); } - f.forceChunkLoading(ticket); - printAnchor("Force Chunk Loading. Chunk Loader has ID of "+f.getLoaderID()+". ",x,y,z); + f.forceChunkLoading(f.getBaseMetaTileEntity(), ticket); + printAnchor("Force Chunk Loading. Chunk Loader has ID of "+f.getUUID().toString()+". ",x,y,z); } else { Logger.INFO("Tile Entity is null."); @@ -445,7 +445,7 @@ public class ChunkManager implements LoadingCallback, OrderedLoadingCallback, Fo for (Triplet<Integer, GregtechMetaTileEntityChunkLoader, DimChunkPos> j : mChunkLoaderManagerMap.values()) { Ticket T; Chunk C; - T = j.getValue_2().getTicketFromForge(); + T = j.getValue_2().getTicketFromForge(j.getValue_2().getBaseMetaTileEntity()); C = j.getValue_3().getChunk(); ForgeChunkManager.forceChunk(T, C.getChunkCoordIntPair()); Logger.INFO("[Chunk Loader] Trying to force load a chunk that holds a chunkloader. [Timer]"); diff --git a/src/Java/gtPlusPlus/core/gui/machine/GUI_ScrollTest.java b/src/Java/gtPlusPlus/core/gui/machine/GUI_ScrollTest.java new file mode 100644 index 0000000000..7a3a01b27c --- /dev/null +++ b/src/Java/gtPlusPlus/core/gui/machine/GUI_ScrollTest.java @@ -0,0 +1,228 @@ +package gtPlusPlus.core.gui.machine; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.lwjgl.Sys; + +import com.google.common.collect.Lists; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import net.minecraft.client.gui.GuiButton; +import net.minecraft.client.gui.GuiOptionButton; +import net.minecraft.client.gui.GuiResourcePackAvailable; +import net.minecraft.client.gui.GuiResourcePackSelected; +import net.minecraft.client.gui.GuiScreen; +import net.minecraft.client.resources.I18n; +import net.minecraft.client.resources.ResourcePackListEntry; +import net.minecraft.client.resources.ResourcePackListEntryFound; +import net.minecraft.client.resources.ResourcePackRepository; +import net.minecraft.client.resources.ResourcePackRepository.Entry; +import net.minecraft.util.Util; + +@SideOnly(Side.CLIENT) +public class GUI_ScrollTest extends GuiScreen +{ + private static final Logger logger = LogManager.getLogger(); + private GuiScreen aThisGUIScreen; + private List<?> field_146966_g; + private List<?> field_146969_h; + private GuiResourcePackAvailable MapOfFreeResourcePacks; + private GuiResourcePackSelected MapOfActiveResourcePacks; + private static final String __OBFID = "CL_00000820"; + + public GUI_ScrollTest(GuiScreen p_i45050_1_) + { + this.aThisGUIScreen = p_i45050_1_; + } + + /** + * Adds the buttons (and other controls) to the screen in question. + */ + @SuppressWarnings("unchecked") + public void initGui() + { + this.buttonList.add(new GuiOptionButton(2, this.width / 2 - 154, this.height - 48, I18n.format("resourcePack.openFolder", new Object[0]))); + this.buttonList.add(new GuiOptionButton(1, this.width / 2 + 4, this.height - 48, I18n.format("gui.done", new Object[0]))); + this.field_146966_g = new ArrayList<Object>(); + this.field_146969_h = new ArrayList<Entry>(); + ResourcePackRepository resourcepackrepository = this.mc.getResourcePackRepository(); + resourcepackrepository.updateRepositoryEntriesAll(); + ArrayList<?> arraylist = Lists.newArrayList(resourcepackrepository.getRepositoryEntriesAll()); + arraylist.removeAll(resourcepackrepository.getRepositoryEntries()); + Iterator<?> iterator = arraylist.iterator(); + ResourcePackRepository.Entry entry; + + while (iterator.hasNext()) + { + entry = (ResourcePackRepository.Entry)iterator.next(); + //this.field_146966_g.add(new ResourcePackListEntryFound(this, entry)); + } + + iterator = Lists.reverse(resourcepackrepository.getRepositoryEntries()).iterator(); + + while (iterator.hasNext()) + { + entry = (ResourcePackRepository.Entry)iterator.next(); + //this.field_146969_h.add(new ResourcePackListEntryFound(this, entry)); + } + + //this.field_146969_h.add(new ResourcePackListEntryDefault(this)); + this.MapOfFreeResourcePacks = new GuiResourcePackAvailable(this.mc, 200, this.height, this.field_146966_g); + this.MapOfFreeResourcePacks.setSlotXBoundsFromLeft(this.width / 2 - 4 - 200); + this.MapOfFreeResourcePacks.registerScrollButtons(7, 8); + this.MapOfActiveResourcePacks = new GuiResourcePackSelected(this.mc, 200, this.height, this.field_146969_h); + this.MapOfActiveResourcePacks.setSlotXBoundsFromLeft(this.width / 2 + 4); + this.MapOfActiveResourcePacks.registerScrollButtons(7, 8); + } + + public boolean func_146961_a(ResourcePackListEntry p_146961_1_) + { + return this.field_146969_h.contains(p_146961_1_); + } + + public List<?> func_146962_b(ResourcePackListEntry p_146962_1_) + { + return this.func_146961_a(p_146962_1_) ? this.field_146969_h : this.field_146966_g; + } + + public List<?> func_146964_g() + { + return this.field_146966_g; + } + + public List<?> func_146963_h() + { + return this.field_146969_h; + } + + protected void actionPerformed(GuiButton p_146284_1_) + { + if (p_146284_1_.enabled) + { + if (p_146284_1_.id == 2) + { + File file1 = this.mc.getResourcePackRepository().getDirResourcepacks(); + String s = file1.getAbsolutePath(); + + if (Util.getOSType() == Util.EnumOS.OSX) + { + try + { + logger.info(s); + Runtime.getRuntime().exec(new String[] {"/usr/bin/open", s}); + return; + } + catch (IOException ioexception1) + { + logger.error("Couldn\'t open file", ioexception1); + } + } + else if (Util.getOSType() == Util.EnumOS.WINDOWS) + { + String s1 = String.format("cmd.exe /C start \"Open file\" \"%s\"", new Object[] {s}); + + try + { + Runtime.getRuntime().exec(s1); + return; + } + catch (IOException ioexception) + { + logger.error("Couldn\'t open file", ioexception); + } + } + + boolean flag = false; + + try + { + Class<?> oclass = Class.forName("java.awt.Desktop"); + Object object = oclass.getMethod("getDesktop", new Class[0]).invoke((Object)null, new Object[0]); + oclass.getMethod("browse", new Class[] {URI.class}).invoke(object, new Object[] {file1.toURI()}); + } + catch (Throwable throwable) + { + logger.error("Couldn\'t open link", throwable); + flag = true; + } + + if (flag) + { + logger.info("Opening via system class!"); + Sys.openURL("file://" + s); + } + } + else if (p_146284_1_.id == 1) + { + ArrayList<Entry> arraylist = Lists.newArrayList(); + Iterator<?> iterator = this.field_146969_h.iterator(); + + while (iterator.hasNext()) + { + ResourcePackListEntry resourcepacklistentry = (ResourcePackListEntry)iterator.next(); + + if (resourcepacklistentry instanceof ResourcePackListEntryFound) + { + arraylist.add(((ResourcePackListEntryFound)resourcepacklistentry).func_148318_i()); + } + } + + Collections.reverse(arraylist); + this.mc.getResourcePackRepository().func_148527_a(arraylist); + this.mc.gameSettings.resourcePacks.clear(); + iterator = arraylist.iterator(); + + while (iterator.hasNext()) + { + ResourcePackRepository.Entry entry = (ResourcePackRepository.Entry)iterator.next(); + this.mc.gameSettings.resourcePacks.add(entry.getResourcePackName()); + } + + this.mc.gameSettings.saveOptions(); + this.mc.refreshResources(); + this.mc.displayGuiScreen(this.aThisGUIScreen); + } + } + } + + /** + * Called when the mouse is clicked. + */ + protected void mouseClicked(int p_73864_1_, int p_73864_2_, int p_73864_3_) + { + super.mouseClicked(p_73864_1_, p_73864_2_, p_73864_3_); + this.MapOfFreeResourcePacks.func_148179_a(p_73864_1_, p_73864_2_, p_73864_3_); + this.MapOfActiveResourcePacks.func_148179_a(p_73864_1_, p_73864_2_, p_73864_3_); + } + + /** + * Called when the mouse is moved or a mouse button is released. Signature: (mouseX, mouseY, which) which==-1 is + * mouseMove, which==0 or which==1 is mouseUp + */ + protected void mouseMovedOrUp(int p_146286_1_, int p_146286_2_, int p_146286_3_) + { + super.mouseMovedOrUp(p_146286_1_, p_146286_2_, p_146286_3_); + } + + /** + * Draws the screen and all the components in it. + */ + public void drawScreen(int p_73863_1_, int p_73863_2_, float p_73863_3_) + { + this.drawBackground(0); + this.MapOfFreeResourcePacks.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + this.MapOfActiveResourcePacks.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + this.drawCenteredString(this.fontRendererObj, I18n.format("resourcePack.title", new Object[0]), this.width / 2, 16, 16777215); + this.drawCenteredString(this.fontRendererObj, I18n.format("resourcePack.folderInfo", new Object[0]), this.width / 2 - 77, this.height - 26, 8421504); + super.drawScreen(p_73863_1_, p_73863_2_, p_73863_3_); + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkLoading.java b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkLoading.java new file mode 100644 index 0000000000..bc690f250c --- /dev/null +++ b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkLoading.java @@ -0,0 +1,57 @@ +package gtPlusPlus.core.handler.chunkloading; + +import cpw.mods.fml.common.event.FMLInitializationEvent; +import cpw.mods.fml.common.event.FMLPostInitializationEvent; +import cpw.mods.fml.common.event.FMLPreInitializationEvent; +import cpw.mods.fml.common.event.FMLServerStartingEvent; +import cpw.mods.fml.common.event.FMLServerStoppingEvent; +import gtPlusPlus.GTplusplus; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.api.objects.minecraft.ChunkManager; +import gtPlusPlus.core.util.Utils; +import gtPlusPlus.core.util.minecraft.network.PacketHandler; +import net.minecraftforge.common.ForgeChunkManager; + +public class ChunkLoading { + + private final ChunkLoading instance; + + public ChunkLoading() { + instance = this; + } + + public ChunkLoading getInstance() { + return this.instance; + } + + + public void preInit(final FMLPreInitializationEvent event) { + PacketHandler.init(); + ForgeChunkManager.setForcedChunkLoadingCallback(GTplusplus.instance, ChunkManager.getInstance()); + Utils.registerEvent(ChunkManager.getInstance()); + } + + + public void init(final FMLInitializationEvent event) { + + } + + + public void postInit(final FMLPostInitializationEvent event) { + + } + + + public synchronized void serverStarting(final FMLServerStartingEvent event) { + + } + + public void serverStopping(final FMLServerStoppingEvent event){ + //Chunkload Handler + if (ChunkManager.mChunkLoaderManagerMap.size() > 0) { + Logger.INFO("Clearing Chunk Loaders."); + ChunkManager.clearInternalMaps(); + } + } + +} diff --git a/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkManager.java b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkManager.java new file mode 100644 index 0000000000..f95c4e7a78 --- /dev/null +++ b/src/Java/gtPlusPlus/core/handler/chunkloading/ChunkManager.java @@ -0,0 +1,153 @@ +package gtPlusPlus.core.handler.chunkloading; + +import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.ListMultimap; +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic.GregtechMetaTileEntityChunkLoader; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import net.minecraft.entity.Entity; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.ChunkCoordIntPair; +import net.minecraft.world.World; +import net.minecraftforge.common.ForgeChunkManager.LoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.OrderedLoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.PlayerOrderedLoadingCallback; +import net.minecraftforge.common.ForgeChunkManager.Ticket; +import net.minecraftforge.event.entity.EntityEvent.EnteringChunk; + +public class ChunkManager implements LoadingCallback, OrderedLoadingCallback, PlayerOrderedLoadingCallback { + private static ChunkManager instance; + + public static ChunkManager getInstance() { + if (instance == null) { + instance = new ChunkManager(); + } + + return instance; + } + + @SubscribeEvent + public void entityEnteredChunk(EnteringChunk event) { + + } + + public Set<ChunkCoordIntPair> getChunksBetween(int xChunkA, int zChunkA, int xChunkB, int zChunkB, int max) { + Set<ChunkCoordIntPair> chunkList = new HashSet<ChunkCoordIntPair>(); + if (xChunkA != xChunkB && zChunkA != zChunkB) { + return chunkList; + } else { + int xStart = Math.min(xChunkA, xChunkB); + int xEnd = Math.max(xChunkA, xChunkB); + int zStart = Math.min(zChunkA, zChunkB); + int zEnd = Math.max(zChunkA, zChunkB); + + for (int xx = xStart; xx <= xEnd; ++xx) { + for (int zz = zStart; zz <= zEnd; ++zz) { + chunkList.add(new ChunkCoordIntPair(xx, zz)); + if (chunkList.size() >= max) { + return chunkList; + } + } + } + + return chunkList; + } + } + + public Set<ChunkCoordIntPair> getChunksAround(int xChunk, int zChunk, int radius) { + Set<ChunkCoordIntPair> chunkList = new HashSet<ChunkCoordIntPair>(); + + for (int xx = xChunk - radius; xx <= xChunk + radius; ++xx) { + for (int zz = zChunk - radius; zz <= zChunk + radius; ++zz) { + chunkList.add(new ChunkCoordIntPair(xx, zz)); + } + } + + return chunkList; + } + + public Set<ChunkCoordIntPair> getBufferAround(int xWorld, int zWorld, int radius) { + int minX = xWorld - radius >> 4; + int maxX = xWorld + radius >> 4; + int minZ = zWorld - radius >> 4; + int maxZ = zWorld + radius >> 4; + Set<ChunkCoordIntPair> chunkList = new HashSet<ChunkCoordIntPair>(); + + for (int xx = minX; xx <= maxX; ++xx) { + for (int zz = minZ; zz <= maxZ; ++zz) { + chunkList.add(new ChunkCoordIntPair(xx, zz)); + } + } + + return chunkList; + } + + public void ticketsLoaded(List<Ticket> tickets, World world) { + Iterator<Ticket> var3 = tickets.iterator(); + while (var3.hasNext()) { + Ticket ticket = (Ticket) var3.next(); + if (!ticket.isPlayerTicket()) { + Entity entity = ticket.getEntity(); + if (entity == null) { + int x = ticket.getModData().getInteger("xCoord"); + int y = ticket.getModData().getInteger("yCoord"); + int z = ticket.getModData().getInteger("zCoord"); + if (y >= 0) { + TileEntity tile = world.getTileEntity(x, y, z); + if (tile instanceof IGregTechTileEntity) { + IGregTechTileEntity g = (IGregTechTileEntity) tile; + if (g instanceof GregtechMetaTileEntityChunkLoader) { + GregtechMetaTileEntityChunkLoader t = (GregtechMetaTileEntityChunkLoader) g; + t.forceChunkLoading(t.getBaseMetaTileEntity(), ticket); + // this.printChunkLoader(t.getName(), x, y, z); + } + } + } + } + } + } + } + + public List<Ticket> ticketsLoaded(List<Ticket> tickets, World world, int maxTicketCount) { + Set<Ticket> adminTickets = new HashSet<Ticket>(); + Set<Ticket> worldTickets = new HashSet<Ticket>(); + Set<Ticket> cartTickets = new HashSet<Ticket>(); + Iterator<Ticket> var7 = tickets.iterator(); + + while (var7.hasNext()) { + Ticket ticket = (Ticket) var7.next(); + Entity entity = ticket.getEntity(); + if (entity == null) { + int x = ticket.getModData().getInteger("xCoord"); + int y = ticket.getModData().getInteger("yCoord"); + int z = ticket.getModData().getInteger("zCoord"); + String type = ticket.getModData().getString("type"); + if (y >= 0) { + if (type.equals("AdminChunkLoader")) { + adminTickets.add(ticket); + } else if (type.equals("StandardChunkLoader")) { + worldTickets.add(ticket); + } else if (type.isEmpty()) { + worldTickets.add(ticket); + } + } + } + } + + List<Ticket> claimedTickets = new LinkedList<Ticket>(); + claimedTickets.addAll(cartTickets); + claimedTickets.addAll(adminTickets); + claimedTickets.addAll(worldTickets); + return claimedTickets; + } + + public ListMultimap<String, Ticket> playerTicketsLoaded(ListMultimap<String, Ticket> tickets, World world) { + return LinkedListMultimap.create(); + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/CustomPacket.java b/src/Java/gtPlusPlus/core/util/minecraft/network/CustomPacket.java new file mode 100644 index 0000000000..def836eac9 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/CustomPacket.java @@ -0,0 +1,39 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.common.network.internal.FMLProxyPacket; +import io.netty.buffer.Unpooled; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import mods.railcraft.common.util.misc.Game; + +public abstract class CustomPacket { + public static final String CHANNEL_NAME = "GTPP"; + + public enum PacketType { + TILE_ENTITY, + } + + public FMLProxyPacket getPacket() { + ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + DataOutputStream data = new DataOutputStream(bytes); + try { + data.writeByte(this.getID()); + this.writeData(data); + } catch (IOException var4) { + Game.logThrowable("Error constructing packet: {0}", var4, new Object[]{this.getClass()}); + } + return new FMLProxyPacket(Unpooled.wrappedBuffer(bytes.toByteArray()), "GTPP"); + } + + public abstract void writeData(DataOutputStream var1) throws IOException; + + public abstract void readData(DataInputStream var1) throws IOException; + + public abstract int getID(); + + public String toString() { + return this.getClass().getSimpleName(); + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketBuilder.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketBuilder.java new file mode 100644 index 0000000000..edbc6aaf83 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketBuilder.java @@ -0,0 +1,25 @@ +package gtPlusPlus.core.util.minecraft.network; + +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import net.minecraft.world.WorldServer; + +public class PacketBuilder { + + private static PacketBuilder instance; + + public static PacketBuilder instance() { + if (instance == null) { + instance = new PacketBuilder(); + } + return instance; + } + + public void sendTileEntityPacket(IGregTechTileEntity tile) { + if (tile.getWorld() instanceof WorldServer) { + WorldServer world = (WorldServer) tile.getWorld(); + PacketTileEntity pkt = new PacketTileEntity(tile); + PacketDispatcher.sendToWatchers(pkt, world, tile.getXCoord(), tile.getZCoord()); + } + } + +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketDispatcher.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketDispatcher.java new file mode 100644 index 0000000000..3e3801bff3 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketDispatcher.java @@ -0,0 +1,88 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.common.network.NetworkRegistry.TargetPoint; +import cpw.mods.fml.relauncher.ReflectionHelper; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.preloader.DevHelper; + +import java.lang.reflect.Method; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.Packet; +import net.minecraft.server.management.PlayerManager; +import net.minecraft.world.WorldServer; + +@SuppressWarnings("unchecked") +public class PacketDispatcher { + private static final Class playerInstanceClass; + private static final Method getOrCreateChunkWatcher; + private static final Method sendToAllPlayersWatchingChunk; + + public static void sendToServer(CustomPacket packet) { + PacketHandler.INSTANCE.channel.sendToServer(packet.getPacket()); + } + + public static void sendToPlayer(CustomPacket packet, EntityPlayerMP player) { + PacketHandler.INSTANCE.channel.sendTo(packet.getPacket(), player); + } + + public static void sendToAll(CustomPacket packet) { + PacketHandler.INSTANCE.channel.sendToAll(packet.getPacket()); + } + + public static void sendToAllAround(CustomPacket packet, TargetPoint zone) { + PacketHandler.INSTANCE.channel.sendToAllAround(packet.getPacket(), zone); + } + + public static void sendToDimension(CustomPacket packet, int dimensionId) { + PacketHandler.INSTANCE.channel.sendToDimension(packet.getPacket(), dimensionId); + } + + public static void sendToWatchers(CustomPacket packet, WorldServer world, int worldX, int worldZ) { + try { + Object playerInstance = getOrCreateChunkWatcher.invoke(world.getPlayerManager(), worldX >> 4, worldZ >> 4, + false); + if (playerInstance != null) { + sendToAllPlayersWatchingChunk.invoke(playerInstance, packet.getPacket()); + } + + } catch (Exception var5) { + Logger.ERROR("Reflection Failure in PacketDispatcher.sendToWatchers() {0} {1}" + 20 + var5 + + new Object[]{getOrCreateChunkWatcher.getName() + sendToAllPlayersWatchingChunk.getName()}); + throw new RuntimeException(var5); + } + } + + static { + try { + playerInstanceClass = PlayerManager.class.getDeclaredClasses()[0]; + + Method a, b; + + try { + a = DevHelper.getInstance().getForgeMethod(PlayerManager.class, "getOrCreateChunkWatcher", int.class, int.class, boolean.class); + } + catch (Throwable t) { + a = ReflectionHelper.findMethod(playerInstanceClass, (Object) null, + new String[]{"func_72690_a", "getOrCreateChunkWatcher"}, + new Class[]{Integer.TYPE, Integer.TYPE, Boolean.TYPE}); + } + try { + b = DevHelper.getInstance().getForgeMethod(PlayerManager.class, "sendToAllPlayersWatchingChunk", Packet.class); + } + catch (Throwable t) { + b = ReflectionHelper.findMethod(playerInstanceClass, (Object) null, + new String[]{"func_151251_a", "sendToAllPlayersWatchingChunk"}, + new Class[]{Packet.class}); + } + + + getOrCreateChunkWatcher = a; + sendToAllPlayersWatchingChunk = b; + getOrCreateChunkWatcher.setAccessible(true); + sendToAllPlayersWatchingChunk.setAccessible(true); + } catch (Exception var1) { + Logger.ERROR("Reflection Failure in PacketDispatcher initalization {0} {1}" + var1); + throw new RuntimeException(var1); + } + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketHandler.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketHandler.java new file mode 100644 index 0000000000..158f9f9483 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketHandler.java @@ -0,0 +1,71 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.network.FMLEventChannel; +import cpw.mods.fml.common.network.NetworkRegistry; +import gtPlusPlus.api.objects.Logger; +import gtPlusPlus.core.util.minecraft.network.CustomPacket.PacketType; +import cpw.mods.fml.common.network.FMLNetworkEvent.ClientCustomPacketEvent; +import cpw.mods.fml.common.network.FMLNetworkEvent.ServerCustomPacketEvent; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.IOException; +import java.util.Arrays; +import net.minecraft.entity.player.EntityPlayerMP; +import net.minecraft.network.NetHandlerPlayServer; + +public class PacketHandler { + public static final PacketHandler INSTANCE = new PacketHandler(); + private static final PacketType[] packetTypes = PacketType.values(); + final FMLEventChannel channel; + + private PacketHandler() { + this.channel = NetworkRegistry.INSTANCE.newEventDrivenChannel("GTPP"); + this.channel.register(this); + } + + public static void init() { + } + + @SubscribeEvent + public void onPacket(ServerCustomPacketEvent event) { + byte[] data = new byte[event.packet.payload().readableBytes()]; + event.packet.payload().readBytes(data); + this.onPacketData(data, ((NetHandlerPlayServer) event.handler).playerEntity); + } + + @SubscribeEvent + public void onPacket(ClientCustomPacketEvent event) { + byte[] data = new byte[event.packet.payload().readableBytes()]; + event.packet.payload().readBytes(data); + this.onPacketData(data, (EntityPlayerMP) null); + } + + public void onPacketData(byte[] bData, EntityPlayerMP player) { + DataInputStream data = new DataInputStream(new ByteArrayInputStream(bData)); + + try { + byte packetID = data.readByte(); + if (packetID < 0) { + return; + } + PacketType type = packetTypes[packetID]; + Object pkt; + + switch(type.ordinal()) { + case 0: + pkt = new PacketTileEntity(); + break; + default: + return; + } + + if (pkt != null) { + ((CustomPacket)pkt).readData(data); + } + } catch (IOException var7) { + Logger.ERROR("Exception in PacketHandler.onPacketData: {0}"+ var7 + new Object[]{Arrays.toString(bData)}); + } + + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/core/util/minecraft/network/PacketTileEntity.java b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketTileEntity.java new file mode 100644 index 0000000000..d59bee3c27 --- /dev/null +++ b/src/Java/gtPlusPlus/core/util/minecraft/network/PacketTileEntity.java @@ -0,0 +1,79 @@ +package gtPlusPlus.core.util.minecraft.network; + +import cpw.mods.fml.client.FMLClientHandler; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import gregtech.api.interfaces.tileentity.IGregTechTileEntity; +import gtPlusPlus.api.interfaces.IGregtechPacketEntity; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import mods.railcraft.common.util.misc.Game; +import net.minecraft.client.Minecraft; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.world.World; + +public class PacketTileEntity extends CustomPacket { + private IGregTechTileEntity tile; + private IGregtechPacketEntity ptile; + + public PacketTileEntity() { + } + + public PacketTileEntity(IGregTechTileEntity tile) { + this.tile = tile; + if (tile instanceof IGregtechPacketEntity) { + ptile = (IGregtechPacketEntity) tile; + } + } + + public void writeData(DataOutputStream data) throws IOException { + if (ptile != null) { + data.writeInt(this.tile.getXCoord()); + data.writeInt(this.tile.getYCoord()); + data.writeInt(this.tile.getZCoord()); + data.writeShort(this.tile.getMetaTileID()); + this.ptile.writePacketData(data); + } + } + + @SideOnly(Side.CLIENT) + public void readData(DataInputStream data) throws IOException { + Minecraft mc = FMLClientHandler.instance().getClient(); + World world = mc != null ? mc.theWorld : null; + if (world != null) { + int x = data.readInt(); + int y = data.readInt(); + int z = data.readInt(); + short id = data.readShort(); + if (id >= 0 && y >= 0 && world.blockExists(x, y, z)) { + TileEntity te = world.getTileEntity(x, y, z); + if (te instanceof IGregTechTileEntity) { + this.tile = (IGregTechTileEntity) te; + if (this.tile.getMetaTileID() != id) { + this.tile = null; + } + } else { + this.tile = null; + } + if (this.tile != null) { + if (tile instanceof IGregtechPacketEntity) { + ptile = (IGregtechPacketEntity) tile; + try { + this.ptile.readPacketData(data); + } catch (IOException var10) { + throw var10; + } catch (RuntimeException var11) { + Game.logThrowable("Exception in PacketTileEntity.readData:", var11, new Object[0]); + } + } + } + } + } + } + + public int getID() { + return 0; + } +}
\ No newline at end of file diff --git a/src/Java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaTileEntityChunkLoader.java b/src/Java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaTileEntityChunkLoader.java index 8decbd6a91..10023722c0 100644 --- a/src/Java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaTileEntityChunkLoader.java +++ b/src/Java/gtPlusPlus/xmod/gregtech/common/tileentities/machines/basic/GregtechMetaTileEntityChunkLoader.java @@ -1,63 +1,42 @@ package gtPlusPlus.xmod.gregtech.common.tileentities.machines.basic; import static gregtech.api.enums.GT_Values.V; -import static gtPlusPlus.api.objects.minecraft.ChunkManager.mChunkLoaderManagerMap; -import java.io.File; +import java.io.DataInputStream; +import java.io.DataOutputStream; import java.io.IOException; +import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.UUID; import com.google.common.collect.MapMaker; -import com.google.common.collect.Sets; +import com.google.common.collect.UnmodifiableIterator; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.ChunkCoordIntPair; -import net.minecraft.world.MinecraftException; -import net.minecraft.world.World; -import net.minecraft.world.chunk.Chunk; - +import net.minecraftforge.common.ForgeChunkManager; +import net.minecraftforge.common.ForgeChunkManager.Ticket; +import net.minecraftforge.common.ForgeChunkManager.Type; import gregtech.api.enums.Textures; import gregtech.api.interfaces.ITexture; import gregtech.api.interfaces.metatileentity.IMetaTileEntity; import gregtech.api.interfaces.tileentity.IGregTechTileEntity; import gregtech.api.metatileentity.implementations.GT_MetaTileEntity_TieredMachineBlock; import gregtech.api.objects.GT_RenderedTexture; - import gtPlusPlus.GTplusplus; -import gtPlusPlus.api.interfaces.IChunkLoader; -import gtPlusPlus.api.objects.Logger; -import gtPlusPlus.api.objects.data.Triplet; -import gtPlusPlus.api.objects.minecraft.BlockPos; -import gtPlusPlus.api.objects.minecraft.ChunkManager; -import gtPlusPlus.api.objects.minecraft.DimChunkPos; -import gtPlusPlus.core.util.reflect.ReflectionUtils; -import net.minecraftforge.common.ForgeChunkManager; -import net.minecraftforge.common.ForgeChunkManager.Ticket; -import net.minecraftforge.common.ForgeChunkManager.Type; +import gtPlusPlus.api.interfaces.IGregtechPacketEntity; +import gtPlusPlus.core.handler.chunkloading.ChunkManager; +import gtPlusPlus.core.util.minecraft.network.PacketBuilder; -public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredMachineBlock implements IChunkLoader { - - private long mTicksLeft = 0; - - private boolean hasTicket; - private boolean refreshTicket; +public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredMachineBlock implements IGregtechPacketEntity { @SuppressWarnings("unused") private final static int yMin = 0; private final static int yMax = 254; private final int xMin, xMax; private final int zMin, zMax; - private int mChunkLoaderMapID = -1; - private boolean mHasID = false; - private final short mMaxTicks = 100; - private final UUID mUUID; - private static final byte ANCHOR_RADIUS = 1; - private static final Map<UUID, Ticket> tickets = new MapMaker().makeMap(); - private Set<ChunkCoordIntPair> chunks; public GregtechMetaTileEntityChunkLoader(final int aID, final String aName, final String aNameRegional, final int aTier) { super(aID, aName, aNameRegional, aTier, 0, "Loads chunks: " + (16 + (48 * aTier)) + " powered"); @@ -65,10 +44,6 @@ public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredM xMax = this.xCoord+47; zMin = this.zCoord-47; zMax = this.zCoord+47; - mUUID = UUID.randomUUID(); - this.prevX = this.xCoord; - this.prevY = this.yCoord; - this.prevZ = this.zCoord; } public GregtechMetaTileEntityChunkLoader(final String aName, final int aTier, final int aInvSlotCount, final String aDescription, final ITexture[][][] aTextures) { @@ -77,10 +52,6 @@ public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredM xMax = this.xCoord+47; zMin = this.zCoord-47; zMax = this.zCoord+47; - mUUID = UUID.randomUUID(); - this.prevX = this.xCoord; - this.prevY = this.yCoord; - this.prevZ = this.zCoord; } @Override @@ -93,73 +64,6 @@ public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredM return new ITexture[]{Textures.BlockIcons.MACHINE_CASINGS[this.mTier][aColorIndex + 1], (aSide != 1) ? null : aActive ? new GT_RenderedTexture(Textures.BlockIcons.OVERLAY_TELEPORTER_ACTIVE) : new GT_RenderedTexture(Textures.BlockIcons.OVERLAY_TELEPORTER)}; } - private int xCoord, yCoord, zCoord; - private int prevX, prevY, prevZ; - @Override - public void onPostTick(final IGregTechTileEntity aBaseMetaTileEntity, final long aTimer) { - - xCoord = aBaseMetaTileEntity.getXCoord(); - yCoord = aBaseMetaTileEntity.getYCoord(); - zCoord = aBaseMetaTileEntity.getZCoord(); - - - if (!aBaseMetaTileEntity.isServerSide()) { - return; - } - - if (aTimer % 20 == 0) { - if (!isRegistered() && this.getEUVar() > 0) { - registerLoader(); - } - else if (isRegistered() && this.getEUVar() <= 0) { - releaseTicket(); - } - } - - if (!areChunksLoaded()) { - return; - } - - if (aBaseMetaTileEntity.isAllowedToWork()) { - if (this.mTicksLeft > 0) { - this.mTicksLeft--; - } - else if (this.mTicksLeft < 0) { - this.mTicksLeft = 0; - } - if (mTicksLeft < mMaxTicks) { - long h = this.getEUVar(); - if (h > 0) { - if (h >(this.maxEUInput()+this.getMinimumStoredEU())) { - this.setEUVar((this.getEUVar()-this.maxEUInput())); - this.mTicksLeft += 2; - } - } - } - if (mTicksLeft > mMaxTicks) { - mTicksLeft = mMaxTicks; - } - - //Somehow the Tile has moved, so lets release the ticket and update the previous xyz. - if (xCoord != prevX || yCoord != prevY || zCoord != prevZ) { - releaseTicket(); - prevX = xCoord; - prevY = yCoord; - prevZ = zCoord; - } - - //Tile has an active ticket, but either (1) the ticket is for a different dimension. (2) A ticket refresh has been queued or (3) the Tile is not allowed to work (Disabled via mallet) - if (hasActiveTicket() && (getTicket().world != aBaseMetaTileEntity.getWorld() || refreshTicket || !aBaseMetaTileEntity.isAllowedToWork())) { - releaseTicket(); - } - - //Tile does not have a ticket but has mTicksLeft, we had best create a ticket, - if (!hasActiveTicket() && this.mTicksLeft > 0) { - requestTicket(); - } - } - } - @Override public void onFirstTick(final IGregTechTileEntity aBaseMetaTileEntity) { @@ -167,7 +71,7 @@ public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredM @Override public void onRemoval() { - releaseTicket(); + } @Override @@ -232,283 +136,189 @@ public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredM @Override public void saveNBTData(final NBTTagCompound aNBT) { - aNBT.setLong("mTicksLeft", getTicksRemaining()); - aNBT.setBoolean("mHasID", mHasID); - aNBT.setInteger("mChunkLoaderMapID", mChunkLoaderMapID); + } @Override public void loadNBTData(final NBTTagCompound aNBT) { - this.mTicksLeft = aNBT.getLong("mTicksLeft"); - this.mHasID = aNBT.getBoolean("mHasID"); - this.mChunkLoaderMapID = aNBT.getInteger("mChunkLoaderMapID"); + } - - - - - + + + + /** - * Chunk Handling + * Chunkloading Code from Railcraft */ - public void registerLoader() { - if (this.getBaseMetaTileEntity().getWorld().isRemote) { - return; - } - //Logger.INFO("=======================[Chunkloader Registration]========================"); - //Logger.INFO("Method Call [1]:"+ReflectionUtils.getMethodName(0)); - //Logger.INFO("Method Call [2]:"+ReflectionUtils.getMethodName(1)); - //Logger.INFO("Method Call [3]:"+ReflectionUtils.getMethodName(2)); - if (this.getEUVar() <= 0) { - return; - } - - try { - if (this.getBaseMetaTileEntity() != null && this.getBaseMetaTileEntity().getWorld() != null && this.getBaseMetaTileEntity().getWorld().getWorldTime() >= 100) { - if (mChunkLoaderMapID == -1) { - this.mChunkLoaderMapID = ChunkManager.getIdFromUniqueString(getUniqueID()); - Logger.INFO("["+getUniqueID()+"] Adjusted Chunk Loaders ID to "+mChunkLoaderMapID+". This chunk loader is not registered to the global list yet."); - } - else { - Logger.INFO("["+getUniqueID()+"] Found cached ID - "+mChunkLoaderMapID); - ChunkManager.setIdAndUniqueString(this.mChunkLoaderMapID, getUniqueID()); - // Just Incase the ID gets shifted. - if (this.mChunkLoaderMapID != ChunkManager.getIdFromUniqueString(getUniqueID())) - this.mChunkLoaderMapID = ChunkManager.getIdFromUniqueString(getUniqueID()); - } - if (this != null && this.getBaseMetaTileEntity() != null) { - if (!isRegistered() && !getUniqueID().equalsIgnoreCase("0@0@0@0")) { - Triplet<Integer, GregtechMetaTileEntityChunkLoader, DimChunkPos> loaderData = new Triplet<Integer, GregtechMetaTileEntityChunkLoader, DimChunkPos>((int) this.mChunkLoaderMapID, this, new DimChunkPos(this.getBaseMetaTileEntity().getWorld(), getPos())); - mChunkLoaderManagerMap.put(getUniqueID(), loaderData); - Logger.INFO("[Chunk Loader] Registered Chunk loader ["+this.mChunkLoaderMapID+"]"+getPos().getLocationString()); - } - else { - Logger.INFO("Tried to re-register a Chunk-Loader. Loader located @ "+getPos().getLocationString()); - } - } - } - } - catch (Throwable t) { - t.printStackTrace(); - } - - } - + private static final Map<UUID, Ticket> tickets = new MapMaker().makeMap(); + private static final byte MAX_CHUNKS = 25; + private static final byte ANCHOR_RADIUS = 1; + private int prevX; + private int prevY; + private int prevZ; + private Set<ChunkCoordIntPair> chunks; + private boolean hasTicket; + private boolean refreshTicket; + private boolean powered; + private UUID uuid; + private int xCoord, yCoord, zCoord; - public void unregisterLoader() { - if (this != null && this.getBaseMetaTileEntity() != null) { - if (isRegistered()) { - mChunkLoaderManagerMap.remove(getUniqueID()); - Logger.INFO("[Chunk Loader] Removed Chunk loader ["+this.mChunkLoaderMapID+"]"+getPos().getLocationString()); - } + public UUID getUUID() { + if (this.uuid == null) { + this.uuid = UUID.randomUUID(); } - } - public boolean isRegistered() { - return mChunkLoaderManagerMap.containsKey(getUniqueID()); + return this.uuid; } - private BlockPos mCachedLocation; - public BlockPos getPos() { - if (mCachedLocation == null) { - mCachedLocation = new BlockPos(this.xCoord, this.yCoord, this.zCoord, this.getBaseMetaTileEntity().getWorld().provider.dimensionId); - } - else if (mCachedLocation.xPos == 0 && mCachedLocation.yPos == 0 && mCachedLocation.zPos == 0 && mCachedLocation.dim == 0) { - mCachedLocation = null; - return getPos(); + private boolean sendClientUpdate = false; + + public void sendUpdateToClient(IGregTechTileEntity aBaseMetaTileEntity) { + if (aBaseMetaTileEntity.hasWorkJustBeenEnabled()) { + this.sendClientUpdate = true; + } else { + PacketBuilder.instance().sendTileEntityPacket(aBaseMetaTileEntity); } - return mCachedLocation; - } - - public String getUniqueID() { - if (getPos() == null) { - return "InvalidChunkLoader"; - } - return getPos().getUniqueIdentifier(); - } - public int getLoaderID() { - return this.mChunkLoaderMapID; } - public void onBlockRemoval() { - releaseTicket(); - } - - public void invalidate() { - releaseTicket(); - super.inValidate(); - refreshTicket = true; + + @Override + public void onPostTick(final IGregTechTileEntity aBaseMetaTileEntity, final long aTimer) { + super.onPostTick(aBaseMetaTileEntity, aTimer); + if (!aBaseMetaTileEntity.isServerSide()) { + return; + } + else { + if (this.xCoord != this.prevX || this.yCoord != this.prevY || this.zCoord != this.prevZ) { + this.releaseTicket(aBaseMetaTileEntity); + this.prevX = this.xCoord; + this.prevY = this.yCoord; + this.prevZ = this.zCoord; + } + + this.powered = meetsTicketRequirements(); + if (this.hasActiveTicket() && (this.getTicket().world != aBaseMetaTileEntity.getWorld() || this.refreshTicket || !this.powered)) { + this.releaseTicket(aBaseMetaTileEntity); + } + if (!this.hasActiveTicket()) { + this.requestTicket(aBaseMetaTileEntity); + } + } + if (this.sendClientUpdate) { + this.sendClientUpdate = false; + PacketBuilder.instance().sendTileEntityPacket(aBaseMetaTileEntity); + } } public void validate() { - if (!isRegistered()) { - registerLoader(); - } + this.refreshTicket = true; } - protected void releaseTicket() { - refreshTicket = false; - if (getTicket() != null) { - ForgeChunkManager.releaseTicket(getTicket()); - } - setTicket(null); - unregisterLoader(); + protected void releaseTicket(IGregTechTileEntity aBaseMetaTileEntity) { + this.refreshTicket = false; + this.setTicket(aBaseMetaTileEntity, (Ticket) null); } - protected void requestTicket() { - Ticket chunkTicket = getTicketFromForge(); - if (chunkTicket != null) { - setTicketData(chunkTicket); - forceChunkLoading(chunkTicket); + protected void requestTicket(IGregTechTileEntity aBaseMetaTileEntity) { + if (this.meetsTicketRequirements()) { + Ticket chunkTicket = this.getTicketFromForge(aBaseMetaTileEntity); + if (chunkTicket != null) { + this.setTicketData(aBaseMetaTileEntity, chunkTicket); + this.forceChunkLoading(aBaseMetaTileEntity, chunkTicket); + } } } - public Ticket getTicketFromForge() { - if (this.getBaseMetaTileEntity().getWorld() != null) - return ForgeChunkManager.requestTicket(GTplusplus.instance, this.getBaseMetaTileEntity().getWorld(), Type.NORMAL); - return null; + protected boolean meetsTicketRequirements() { + return this.getEUVar() > (V[this.mTier]*2); + } + + public Ticket getTicketFromForge(IGregTechTileEntity aBaseMetaTileEntity) { + return ForgeChunkManager.requestTicket(GTplusplus.instance, aBaseMetaTileEntity.getWorld(), Type.NORMAL); } - protected void setTicketData(Ticket chunkTicket) { - chunkTicket.getModData().setInteger("xCoord", this.getBaseMetaTileEntity().getXCoord()); - chunkTicket.getModData().setInteger("yCoord", this.getBaseMetaTileEntity().getYCoord()); - chunkTicket.getModData().setInteger("zCoord", this.getBaseMetaTileEntity().getZCoord()); - //chunkTicket.getModData().setString("type", getMachineType().getTag()); + protected void setTicketData(IGregTechTileEntity aBaseMetaTileEntity, Ticket chunkTicket) { + chunkTicket.getModData().setInteger("xCoord", this.xCoord); + chunkTicket.getModData().setInteger("yCoord", this.yCoord); + chunkTicket.getModData().setInteger("zCoord", this.zCoord); + chunkTicket.getModData().setString("type", "StandardChunkLoader"); } public boolean hasActiveTicket() { - return getTicket() != null; + return this.getTicket() != null; } public Ticket getTicket() { - //Logger.INFO("[Chunk Loader] "+"Getting Ticking. ["+this.getLoaderID()+"] @ [x: "+this.xCoord+"][y: "+this.yCoord+"][z: "+this.zCoord+"]"); - return tickets.get(this.mUUID); + return (Ticket) tickets.get(this.getUUID()); } - public void setTicket(Ticket t) { + public void setTicket(IGregTechTileEntity aBaseMetaTileEntity, Ticket t) { boolean changed = false; - Ticket ticket = getTicket(); + Ticket ticket = this.getTicket(); if (ticket != t) { if (ticket != null) { - if (ticket.world == this.getBaseMetaTileEntity().getWorld()) { - for (ChunkCoordIntPair chunk : ticket.getChunkList()) { - if (ForgeChunkManager.getPersistentChunksFor(this.getBaseMetaTileEntity().getWorld()).keys().contains(chunk)) + if (ticket.world == aBaseMetaTileEntity.getWorld()) { + UnmodifiableIterator<ChunkCoordIntPair> var4 = ticket.getChunkList().iterator(); + + while (var4.hasNext()) { + ChunkCoordIntPair chunk = (ChunkCoordIntPair) var4.next(); + if (ForgeChunkManager.getPersistentChunksFor(aBaseMetaTileEntity.getWorld()).keys().contains(chunk)) { ForgeChunkManager.unforceChunk(ticket, chunk); + } } + ForgeChunkManager.releaseTicket(ticket); } - Logger.INFO("[Chunk Loader] "+"Removing Ticket. ["+this.getLoaderID()+"] @ [x: "+this.xCoord+"][y: "+this.yCoord+"][z: "+this.zCoord+"]"); - tickets.remove(mUUID); + + tickets.remove(this.getUUID()); } + changed = true; } - hasTicket = (t != null); - if (hasTicket) { - Logger.INFO("[Chunk Loader] "+"Putting Ticket. ["+this.getLoaderID()+"] @ [x: "+this.xCoord+"][y: "+this.yCoord+"][z: "+this.zCoord+"]"); - tickets.put(mUUID, t); - } - //if (changed) - //sendUpdateToClient(); - } - - public void forceChunkLoading(Ticket ticket) { - try { - setTicket(ticket); - forceChunkLoading2(ticket); - //setupChunks(); - /*if (chunks != null) { - for (ChunkCoordIntPair chunk : chunks) { - ForgeChunkManager.forceChunk(ticket, chunk); - } - }*/ + + this.hasTicket = t != null; + if (this.hasTicket) { + tickets.put(this.getUUID(), t); } - catch (Throwable t){ - t.printStackTrace(); + + if (changed) { + this.sendUpdateToClient(aBaseMetaTileEntity); } - } - public void forceChunkLoading2(Ticket ticket) { - chunks = Sets.newHashSet(); - ChunkCoordIntPair quarryChunk = new ChunkCoordIntPair(xCoord >> 4, zCoord >> 4); - chunks.add(quarryChunk); - ForgeChunkManager.forceChunk(ticket, quarryChunk); - for (int chunkX = xMin >> 4; chunkX <= xMax >> 4; chunkX++) { - for (int chunkZ = zMin >> 4; chunkZ <= zMax >> 4; chunkZ++) { - ChunkCoordIntPair chunk = new ChunkCoordIntPair(chunkX, chunkZ); + } + + public void forceChunkLoading(IGregTechTileEntity aBaseMetaTileEntity, Ticket ticket) { + this.setTicket(aBaseMetaTileEntity, ticket); + this.setupChunks(); + if (this.chunks != null) { + Iterator<ChunkCoordIntPair> var2 = this.chunks.iterator(); + while (var2.hasNext()) { + ChunkCoordIntPair chunk = (ChunkCoordIntPair) var2.next(); ForgeChunkManager.forceChunk(ticket, chunk); - chunks.add(chunk); } } - } - + public void setupChunks() { - if (!hasTicket) - chunks = null; + if (!this.hasTicket) { + this.chunks = null; + } else { - /*if (this.mTier == 3) { - chunks = ChunkManager.getInstance().getChunksAround(this.getBaseMetaTileEntity().getXCoord() >> 4, this.getBaseMetaTileEntity().getZCoord() >> 4, ANCHOR_RADIUS); - } - else if (this.mTier == 4) { - chunks = ChunkManager.getInstance().getChunksAround(this.getBaseMetaTileEntity().getXCoord() >> 4 >> 4, (this.getBaseMetaTileEntity().getZCoord() >> 4) >> 4, ANCHOR_RADIUS); - } - else if (this.mTier == 5) { - chunks = ChunkManager.getInstance().getChunksAround(this.getBaseMetaTileEntity().getXCoord() >> 4 >> 4 >> 4, ((this.getBaseMetaTileEntity().getZCoord() >> 4) >> 4) >> 4, ANCHOR_RADIUS); - } */ - chunks = ChunkManager.getInstance().getChunksAround(this.getBaseMetaTileEntity().getXCoord() >> 4, this.getBaseMetaTileEntity().getZCoord() >> 4, ANCHOR_RADIUS); + this.chunks = ChunkManager.getInstance().getChunksAround(this.xCoord >> 4, this.zCoord >> 4, 1); } } - - @Override - public Chunk loadChunk(World p_75815_1_, int p_75815_2_, int p_75815_3_) throws IOException { - Logger.INFO("1"); - return null; - } - - @Override - public void saveChunk(World p_75816_1_, Chunk p_75816_2_) throws MinecraftException, IOException { - Logger.INFO("2"); - } - - @Override - public void saveExtraChunkData(World p_75819_1_, Chunk p_75819_2_) { - Logger.INFO("3"); - } - - @Override - public void chunkTick() { - Logger.INFO("4"); - } - - @Override - public void saveExtraData() { - Logger.INFO("5"); - } - - @Override - public long getTicksRemaining() { - return this.mTicksLeft; - } - - @Override - public String[] getDescription() { - // TODO Auto-generated method stub - return super.getDescription(); - } - @Override public void onExplosion() { - this.releaseTicket(); + this.releaseTicket(this.getBaseMetaTileEntity()); super.onExplosion(); } @Override - public void inValidate() { - invalidate(); - super.inValidate(); + public void onValueUpdate(byte aValue) { + super.onValueUpdate(aValue); } @Override @@ -518,68 +328,31 @@ public class GregtechMetaTileEntityChunkLoader extends GT_MetaTileEntity_TieredM @Override public void markDirty() { - validate(); + this.refreshTicket = true; super.markDirty(); } @Override - public void doExplosion(long aExplosionPower) { - releaseTicket(); - super.doExplosion(aExplosionPower); - } - - @Override - public boolean isGivingInformation() { - return true; - } - - @Override - public String[] getInfoData() { - return new String[] { - this.getLocalName(), - "Ticks Left: "+this.mTicksLeft, - "mRange: "+(16 + (48 * mTier)), - "chunks: "+this.chunks.size()}; - } - - @Override - public void onCreated(ItemStack aStack, World aWorld, EntityPlayer aPlayer) { - super.onCreated(aStack, aWorld, aPlayer); - } - - @Override - public void onServerStart() { - validate(); - super.onServerStart(); + public boolean connectsToItemPipe(byte aSide) { + return false; } @Override - public void onWorldLoad(File aSaveDirectory) { - validate(); - super.onWorldLoad(aSaveDirectory); + public void doExplosion(long aExplosionPower) { + this.releaseTicket(this.getBaseMetaTileEntity()); + super.doExplosion(aExplosionPower); } - @Override - public void onWorldSave(File aSaveDirectory) { - validate(); - super.onWorldSave(aSaveDirectory); + public void writePacketData(DataOutputStream data) throws IOException { + data.writeBoolean(this.hasTicket); } - //Based on BC 7 - - - private boolean hasCheckedChunks = false; - public boolean areChunksLoaded() { - if (hasCheckedChunks) { - return true; + public void readPacketData(DataInputStream data) throws IOException { + boolean tick = data.readBoolean(); + if (this.hasTicket != tick) { + this.hasTicket = tick; + this.markDirty(); } - World worldObj = this.getBaseMetaTileEntity().getWorld(); - hasCheckedChunks = worldObj.blockExists(xMin, yMax, zMin) - && worldObj.blockExists(xMax, yMax, zMin) - && worldObj.blockExists(xMin, yMax, zMax) - && worldObj.blockExists(xMax, yMax, zMax); - // Each chunk covers the full height, so we only check one of them per height. - return hasCheckedChunks; + this.setupChunks(); } - } |