Advertisement
stormtrooper28

IlluminatorBlockEntity Class

Oct 26th, 2020
4,126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.97 KB | None | 0 0
  1. package com.stormbot28.mod.magicmod.blocks.qol;
  2.  
  3. import com.stormbot28.mod.magicmod.Registering;
  4. import net.minecraft.block.Blocks;
  5. import net.minecraft.block.ShapeContext;
  6. import net.minecraft.block.entity.BlockEntity;
  7. import net.minecraft.item.ItemPlacementContext;
  8. import net.minecraft.nbt.CompoundTag;
  9. import net.minecraft.particle.ParticleTypes;
  10. import net.minecraft.util.Tickable;
  11. import net.minecraft.util.math.BlockPos;
  12. import net.minecraft.world.chunk.WorldChunk;
  13.  
  14.  
  15. /**
  16.  * Move contents to add/remove error
  17.  *In order of importance
  18.  * 1. torches placing and not updating player/client renderer
  19.  * 2. save/load data
  20.  *
  21.  *
  22.  *
  23.  *
  24.  */
  25.  
  26.  
  27. //###@Plans make it eat coal/!!glowstone!!/torches to run
  28. public class IlluminatorBlockEntity extends BlockEntity implements Tickable {
  29.     private static final int ILLUMINATOR_RADIUS = 3;
  30.     private static final int ILLUMINATOR_DIAMETER = (ILLUMINATOR_RADIUS * 2) + 1;
  31.  
  32.     private int waitTickCounter = 0;
  33.     private boolean initialized = false;
  34.  
  35.     private  WorldChunk startingWorldChunk;     // should be final :(, but world isnt set
  36.     private  int xChunkInit;                    // should be final :(, but world isnt set
  37.     private  int zChunkInit;                    // should be final :(, but world isnt set
  38.     private int xChunkIter;
  39.     private int zChunkIter;
  40.  
  41.  
  42.     private int xInit;
  43.     private int zInit;
  44.  
  45.     private int xIter;
  46.     private int zIter;
  47.  
  48.     public IlluminatorBlockEntity() {
  49.         super(Registering.ILLUMINATOR_BLOCK_ENTITY_TYPE);
  50.  
  51.     }
  52.  
  53.     private void initialize() {
  54.         if(initialized) return;
  55.  
  56.         this.startingWorldChunk = this.world.getWorldChunk(this.pos);
  57.  
  58.         this.xChunkInit = startingWorldChunk.getPos().x - ILLUMINATOR_RADIUS;
  59.         this.zChunkInit = startingWorldChunk.getPos().z - ILLUMINATOR_RADIUS;
  60.  
  61.  
  62.         System.out.println("X_CHUNK_INIT: "+xChunkInit);
  63.         System.out.println("X_STARTING_CHUNK_POS: "+startingWorldChunk.getPos().x);
  64.         System.out.println("ILLUMINATOR_RADIUS: "+ILLUMINATOR_RADIUS);
  65.         resetCounters();
  66.         System.out.println("X_INIT: "+xInit);
  67.  
  68.         initialized = true;
  69.     }
  70.  
  71.     private void resetCounters() {
  72.         BlockPos initialBlockPos = world.getChunk(xChunkInit, zChunkInit).getPos().getStartPos();
  73.         xInit = initialBlockPos.getX();
  74.         zInit = initialBlockPos.getZ();
  75.  
  76.         xIter = 0;
  77.         zIter = 0;
  78.  
  79.         xChunkIter = 0;
  80.         zChunkIter = 0;
  81.         System.out.println("counters reset");
  82.     }
  83.  
  84.  
  85.     @Override
  86.     public void tick() {
  87.         initialize();
  88.  
  89.         if(waitTickCounter > 0) { // test if cooldown ticking
  90.             System.out.println("waiting: " + waitTickCounter);
  91.             if(++waitTickCounter < 20*60*15)  //20 * 60 * 15 (15 minutes between updates)
  92.                 return;
  93.             else // once cooldown ends, relight
  94.                 waitTickCounter = 0;
  95.         }
  96.         // light up current column
  97.         lightUpWorldColumn(xInit + xIter, zInit + zIter);
  98.  
  99.         //increment iterators accordingly
  100.         System.out.println("==============================================");
  101.         System.out.println("xChunkIter: " + xChunkIter);
  102.         System.out.println("zChunkIter: " + zChunkIter);
  103.         System.out.println("xChunkInit: " + xChunkInit);
  104.         System.out.println("zChunkInit: " + zChunkInit);
  105.         if(++xIter > 15) { // move through Z row of chunk
  106.             xIter = 0;
  107.             if(++zIter > 15) { // move through next Z row of chunk
  108.                 System.out.println("---resetZ---OLD: " + zIter +" + " + zInit + " = " + (zIter+zInit));
  109.                 zIter = 0;
  110.                 if(++xChunkIter >= ILLUMINATOR_DIAMETER) { // move through Z row of selected chunks
  111.                     System.out.println("move z and x chunks");
  112.                     xChunkIter = 0;
  113.                     if(++zChunkIter >= ILLUMINATOR_DIAMETER) { // next Z row of selected chunks
  114.                         // We've reached the end, reset and await cooldown
  115.                         resetCounters();
  116.                         waitTickCounter = 1;
  117.                     }
  118.                 }
  119.                 // whether we've only reached the end of the x chunk, or the far end, we need to reset the init coords
  120.                 // remember, xChunkIter changes whether or not if statement is true
  121.                 realignInitOnChunk();
  122.                 System.out.println("---resetZ---NEW: " + zIter +" + " + zInit + " = " + (zIter+zInit));
  123.             }
  124.         }
  125.     }
  126.  
  127.     private void realignInitOnChunk() {
  128.         BlockPos startingPos = world.getChunk(xChunkInit + xChunkIter, zChunkInit + zChunkIter).getPos().getStartPos();
  129.  
  130.         xInit = startingPos.getX();
  131.         zInit = startingPos.getZ();
  132.         System.out.println("realign init on chunk");
  133.     }
  134.  
  135.     private void lightUpWorldColumn(int x, int z) {
  136.         BlockPos currentBlockPos = new BlockPos(x, 1, z);
  137.         WorldChunk currentWorldChunk = world.getWorldChunk(currentBlockPos);
  138.         currentWorldChunk.loadToWorld();
  139.         boolean isDirty = false;
  140.  
  141.         while(currentBlockPos.getY() < world.getHeight()) {
  142.             currentBlockPos = currentBlockPos.up();
  143.             //System.out.println("block: " + world.getBlockState(currentBlockPos).getBlock().getTranslationKey() + "|||isAir: "+world.getBlockState(currentBlockPos).isAir());
  144.             if(!world.getBlockState(currentBlockPos).isAir() ){
  145.                 //System.out.println("not air");
  146.                 if(world.getBlockState(currentBlockPos.up()).isAir() ) {
  147.                     currentBlockPos = currentBlockPos.up();
  148.                     world.addParticle(ParticleTypes.SOUL, true, currentBlockPos.getX(), currentBlockPos.getY() + 1, currentBlockPos.getZ(), 0, 0, 0);
  149.                     //System.out.println("under air with light: "+world.getLightLevel(currentBlockPos));
  150.                     if (world.getLightLevel(currentBlockPos) <= 7) {
  151.                         //System.out.println("bad light level at y="+currentBlockPos.getY());
  152.                         if (isDirty = world.canPlace(Blocks.TORCH.getDefaultState(), currentBlockPos, ShapeContext.absent())) {
  153.                             /*BlockState bs =*/
  154.                             world.getWorldChunk(currentBlockPos).setBlockState(currentBlockPos, Blocks.TORCH.getDefaultState(), false);
  155.  
  156.                             //currentWorldChunk.markBlockForPostProcessing(currentBlockPos);
  157.                             world.addParticle(ParticleTypes.CAMPFIRE_COSY_SMOKE, true, currentBlockPos.getX(), currentBlockPos.getY() + 1, currentBlockPos.getZ(), 0, 0, 0);
  158.                             world.getLightingProvider().checkBlock(currentBlockPos);
  159.                             System.out.println("torch placed @:" + x + ", " + z);
  160.                         }
  161.                     }
  162.                 }
  163.             }
  164.         }
  165.         //System.out.println("light column @:" +x+", "+z);
  166.         if(isDirty) {
  167.  
  168.             // real janky force re-render
  169.             //for(PlayerEntity p : world.getPlayers())
  170.  
  171.                 //world.getServer().getWorld(world.getRegistryKey()).getChunkManager().addTicket(ChunkTicketType.POST_TELEPORT, currentWorldChunk.getPos(), 0, p.getEntityId());
  172.  
  173.  
  174.             //for(PlayerEntity p : world.getPlayers())
  175.             //   if(p.getBlockPos())
  176.              //       currentWorldChunk.markBlockForPostProcessing();
  177.             currentWorldChunk.markDirty(); // tells world it needs to save, do this after rendering update
  178.         }
  179.     }
  180.  
  181.     /*
  182.     private int waitTickCounter = 0;
  183.     private boolean initialized = false;
  184.  
  185.     private  WorldChunk startingWorldChunk;     // should be final :(, but world isnt set
  186.     private  int xChunkInit;                    // should be final :(, but world isnt set
  187.     private  int zChunkInit;                    // should be final :(, but world isnt set
  188.     private int xChunkIter;
  189.     private int zChunkIter;
  190.  
  191.  
  192.     private int xInit;
  193.     private int zInit;
  194.  
  195.     private int xIter;
  196.     private int zIter;
  197.      */
  198.  
  199.     // Serialize the BlockEntity
  200.     public CompoundTag toTag(CompoundTag tag) {
  201.         super.toTag(tag);
  202.  
  203.         // Save the current value of the number to the tag
  204.         tag.putInt("waitTickCounter", waitTickCounter);
  205.         tag.putInt("xChunkIter", xChunkIter);
  206.         tag.putInt("zChunkIter", zChunkIter);
  207.         tag.putInt("xInit", xInit);
  208.         tag.putInt("zInit", zInit);
  209.         tag.putInt("xIter", xIter);
  210.         tag.putInt("zIter", zIter);
  211.  
  212.         return tag;
  213.     }
  214.  
  215.     // Deserialize the BlockEntity
  216.     public void fromTag(CompoundTag tag) {
  217.         this.pos = new BlockPos(tag.getInt("x"), tag.getInt("y"), tag.getInt("z"));
  218.  
  219.         initialize();
  220.  
  221.         waitTickCounter= tag.getInt("waitTickCounter");
  222.         xChunkIter = tag.getInt("xChunkIter");
  223.         zChunkIter = tag.getInt("zChunkIter");
  224.         xInit = tag.getInt("xInit");
  225.         zInit = tag.getInt("zInit");
  226.         xIter = tag.getInt("xIter");
  227.         zIter = tag.getInt("zIter");
  228.     }
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement