Advertisement
Corosus

forge chunk loading/persisting basics

Dec 5th, 2012
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.08 KB | None | 0 0
  1. package railcraft.common.blocks.machine.alpha;
  2.  
  3. import aaj;
  4. import amj;
  5. import anq;
  6. import bq;
  7. import java.io.DataInputStream;
  8. import java.io.DataOutputStream;
  9. import java.io.IOException;
  10. import java.util.Collections;
  11. import java.util.HashMap;
  12. import java.util.Map;
  13. import java.util.Random;
  14. import java.util.Set;
  15. import lq;
  16. import net.minecraftforge.common.ForgeChunkManager;
  17. import net.minecraftforge.common.ForgeChunkManager.Ticket;
  18. import net.minecraftforge.common.ForgeChunkManager.Type;
  19. import qx;
  20. import railcraft.client.core.KeyHandler;
  21. import railcraft.common.api.core.WorldCoordinate;
  22. import railcraft.common.api.core.items.ICrowbar;
  23. import railcraft.common.blocks.machine.IEnumMachine;
  24. import railcraft.common.blocks.machine.TileMachineBase;
  25. import railcraft.common.blocks.machine.beta.EnumMachineBeta;
  26. import railcraft.common.blocks.machine.beta.TileSentinel;
  27. import railcraft.common.core.Railcraft;
  28. import railcraft.common.core.RailcraftConfig;
  29. import railcraft.common.lang.RailcraftLanguage;
  30. import railcraft.common.util.effects.EffectManager;
  31. import railcraft.common.util.misc.ChunkManager;
  32. import railcraft.common.util.misc.Game;
  33. import railcraft.common.util.misc.MiscTools;
  34. import um;
  35. import xo;
  36. import xv;
  37.  
  38. public class TileWorldAnchor extends TileMachineBase
  39. {
  40.   public static final Map pairingMap = Collections.synchronizedMap(new HashMap());
  41.   private static final int SENTINEL_CHECK = 128;
  42.   private static final byte MAX_SENTINEL_CHUNKS = 9;
  43.   private static final byte ANCHOR_RADIUS = 1;
  44.   private int xSentinel = -1;
  45.   private int ySentinel = -1;
  46.   private int zSentinel = -1;
  47.   private ForgeChunkManager.Ticket ticket;
  48.   private int update = MiscTools.getRand().nextInt();
  49.   private Set chunks;
  50.  
  51.   public IEnumMachine getMachineType()
  52.   {
  53.     return EnumMachineAlpha.WORLD_ANCHOR;
  54.   }
  55.  
  56.   public int getBlockTexture(int side)
  57.   {
  58.     return getMachineType().getTexture(side);
  59.   }
  60.  
  61.   public boolean blockActivated(qx player, int side)
  62.   {
  63.     um current = player.bT();
  64.     if ((current != null) && ((current.b() instanceof ICrowbar))) {
  65.       WorldCoordinate sentinel = (WorldCoordinate)pairingMap.get(player);
  66.       if (sentinel != null) {
  67.         if (this.k.v.h == sentinel.dimension) {
  68.           setSentinel(player, sentinel.x, sentinel.y, sentinel.z);
  69.         }
  70.         return true;
  71.       }
  72.     }
  73.     return super.blockActivated(player, side);
  74.   }
  75.  
  76.   public int getMaxSentinelChunks() {
  77.     if (this.ticket == null) {
  78.       return 9;
  79.     }
  80.     return Math.min(this.ticket.getMaxChunkListDepth(), 9);
  81.   }
  82.  
  83.   public void setSentinel(qx player, int x, int y, int z) {
  84.     anq tile = this.k.q(x, y, z);
  85.     if ((tile instanceof TileSentinel)) {
  86.       int xChunk = this.l >> 4;
  87.       int zChunk = this.n >> 4;
  88.  
  89.       int xSentinelChunk = tile.l >> 4;
  90.       int zSentinelChunk = tile.n >> 4;
  91.  
  92.       if ((xChunk != xSentinelChunk) && (zChunk != zSentinelChunk)) {
  93.         if (Game.isNotHost(this.k)) {
  94.           player.b(b() + " " + RailcraftLanguage.translate("gui.anchor.pair.fail.alignment") + " " + RailcraftLanguage.translate(EnumMachineBeta.SENTINEL.getTag()));
  95.         }
  96.         return;
  97.       }
  98.  
  99.       int max = getMaxSentinelChunks();
  100.       if ((Math.abs(xChunk - xSentinelChunk) > max) || (Math.abs(zChunk - zSentinelChunk) > max)) {
  101.         if (Game.isNotHost(this.k)) {
  102.           player.b(b() + " " + RailcraftLanguage.translate("gui.anchor.pair.fail.distance") + " " + RailcraftLanguage.translate(EnumMachineBeta.SENTINEL.getTag()));
  103.         }
  104.         return;
  105.       }
  106.  
  107.       this.xSentinel = tile.l;
  108.       this.ySentinel = tile.m;
  109.       this.zSentinel = tile.n;
  110.  
  111.       if (Game.isHost(this.k)) {
  112.         releaseTicket();
  113.         requestTicket();
  114.         markBlockForUpdate();
  115.       } else {
  116.         player.b(b() + " " + RailcraftLanguage.translate("gui.anchor.pair.success"));
  117.       }
  118.     }
  119.   }
  120.  
  121.   public void clearSentinel() {
  122.     if (!hasSentinel()) {
  123.       return;
  124.     }
  125.  
  126.     this.xSentinel = -1;
  127.     this.ySentinel = -1;
  128.     this.zSentinel = -1;
  129.  
  130.     releaseTicket();
  131.     requestTicket();
  132.     markBlockForUpdate();
  133.   }
  134.  
  135.   public boolean hasSentinel() {
  136.     return this.ySentinel != -1;
  137.   }
  138.  
  139.   public void g()
  140.   {
  141.     super.g();
  142.     if (Game.isNotHost(this.k)) {
  143.       if ((this.chunks != null) && (KeyHandler.isChunkAuraEnabled())) {
  144.         EffectManager.chunkLoaderEffect(this.k, this, this.chunks);
  145.       }
  146.       return;
  147.     }
  148.  
  149.     if (RailcraftConfig.deleteAnchors()) {
  150.       releaseTicket();
  151.       this.k.e(this.l, this.m, this.n, amj.as.cm);
  152.       return;
  153.     }
  154.  
  155.     this.update += 1;
  156.     if ((this.update % 128 == 0) && (hasSentinel())) {
  157.       anq tile = this.k.q(this.xSentinel, this.ySentinel, this.zSentinel);
  158.       if (!(tile instanceof TileSentinel)) {
  159.         clearSentinel();
  160.       }
  161.     }
  162.  
  163.     if ((this.ticket == null) && (ForgeChunkManager.ticketCountAvailableFor(Railcraft.getMod(), this.k) > 0))
  164.       requestTicket();
  165.   }
  166.  
  167.   public void onBlockAdded()
  168.   {
  169.     requestTicket();
  170.   }
  171.  
  172.   public void onBlockRemoval()
  173.   {
  174.     releaseTicket();
  175.   }
  176.  
  177.   private void releaseTicket() {
  178.     ForgeChunkManager.releaseTicket(this.ticket);
  179.   }
  180.  
  181.   private void requestTicket() {
  182.     ForgeChunkManager.Ticket chunkTicket = ForgeChunkManager.requestTicket(Railcraft.getMod(), this.k, ForgeChunkManager.Type.NORMAL);
  183.     if (chunkTicket != null) {
  184.       chunkTicket.getModData().a("xCoord", this.l);
  185.       chunkTicket.getModData().a("yCoord", this.m);
  186.       chunkTicket.getModData().a("zCoord", this.n);
  187.       forceChunkLoading(chunkTicket);
  188.     }
  189.   }
  190.  
  191.   public void forceChunkLoading(ForgeChunkManager.Ticket ticket) {
  192.     if (this.ticket == null) {
  193.       this.ticket = ticket;
  194.     }
  195.  
  196.     setupChunks();
  197.  
  198.     for (xo chunk : this.chunks)
  199.       ForgeChunkManager.forceChunk(ticket, chunk);
  200.   }
  201.  
  202.   public void setupChunks()
  203.   {
  204.     if (hasSentinel())
  205.       this.chunks = ChunkManager.getInstance().getChunksBetween(this.l >> 4, this.n >> 4, this.xSentinel >> 4, this.zSentinel >> 4, getMaxSentinelChunks());
  206.     else
  207.       this.chunks = ChunkManager.getInstance().getChunksAround(this.l >> 4, this.n >> 4, 1);
  208.   }
  209.  
  210.   public void writePacketData(DataOutputStream data)
  211.     throws IOException
  212.   {
  213.     super.writePacketData(data);
  214.     data.writeInt(this.xSentinel);
  215.     data.writeInt(this.ySentinel);
  216.     data.writeInt(this.zSentinel);
  217.   }
  218.  
  219.   public void readPacketData(DataInputStream data) throws IOException
  220.   {
  221.     super.readPacketData(data);
  222.     this.xSentinel = data.readInt();
  223.     this.ySentinel = data.readInt();
  224.     this.zSentinel = data.readInt();
  225.  
  226.     setupChunks();
  227.   }
  228.  
  229.   public void b(bq data)
  230.   {
  231.     super.b(data);
  232.  
  233.     data.a("xSentinel", this.xSentinel);
  234.     data.a("ySentinel", this.ySentinel);
  235.     data.a("zSentinel", this.zSentinel);
  236.   }
  237.  
  238.   public void a(bq data)
  239.   {
  240.     super.a(data);
  241.  
  242.     this.xSentinel = data.e("xSentinel");
  243.     this.ySentinel = data.e("ySentinel");
  244.     this.zSentinel = data.e("zSentinel");
  245.   }
  246.  
  247.   public float getResistance(lq exploder)
  248.   {
  249.     return 60.0F;
  250.   }
  251.  
  252.   public float getHardness()
  253.   {
  254.     return 20.0F;
  255.   }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement