Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.59 KB | None | 0 0
  1. package lumien.randomthings.tileentity;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashSet;
  5. import java.util.List;
  6.  
  7. import lumien.randomthings.block.BlockPotionVaporizer;
  8. import lumien.randomthings.lib.ISlotFilter;
  9. import lumien.randomthings.network.PacketHandler;
  10. import lumien.randomthings.network.messages.MessagePotionVaporizerParticles;
  11. import lumien.randomthings.util.WorldUtil;
  12. import net.minecraft.entity.EntityLivingBase;
  13. import net.minecraft.init.Items;
  14. import net.minecraft.init.MobEffects;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.nbt.NBTTagCompound;
  17. import net.minecraft.nbt.NBTTagList;
  18. import net.minecraft.potion.Potion;
  19. import net.minecraft.potion.PotionEffect;
  20. import net.minecraft.potion.PotionUtils;
  21. import net.minecraft.tileentity.TileEntityFurnace;
  22. import net.minecraft.util.EnumFacing;
  23. import net.minecraft.util.ITickable;
  24. import net.minecraft.util.math.AxisAlignedBB;
  25. import net.minecraft.util.math.BlockPos;
  26. import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint;
  27.  
  28. public class TileEntityPotionVaporizer extends TileEntityBase implements ITickable
  29. {
  30. HashSet<BlockPos> affectedBlocks;
  31.  
  32. final int MAX_BLOCKS = 100;
  33.  
  34. PotionEffect currentPotionEffect;
  35.  
  36. int durationLeft;
  37.  
  38. int fuelBurn;
  39. int fuelBurnTime;
  40.  
  41. public TileEntityPotionVaporizer()
  42. {
  43. affectedBlocks = new HashSet<>();
  44.  
  45. validBlocks = new HashSet<>();
  46. checkedBlocks = new HashSet<>();
  47. toBeChecked = new ArrayList<>();
  48.  
  49. durationLeft = 1;
  50. fuelBurnTime = 0;
  51.  
  52. this.setItemHandler(3);
  53. this.setItemHandlerPublic(new int[] { 0, 1 }, new int[] { 2 });
  54.  
  55. this.addSlotFilter(0, new ISlotFilter()
  56. {
  57. @Override
  58. public boolean isItemStackValid(ItemStack is)
  59. {
  60. return TileEntityFurnace.isItemFuel(is);
  61. }
  62. });
  63.  
  64. this.addSlotFilter(1, new ISlotFilter()
  65. {
  66. @Override
  67. public boolean isItemStackValid(ItemStack is)
  68. {
  69. if (is.getItem() != Items.POTIONITEM)
  70. {
  71. return false;
  72. }
  73. List<PotionEffect> effects = PotionUtils.getEffectsFromStack(is);
  74. if (effects == null || effects.size() == 0)
  75. {
  76. return false;
  77. }
  78.  
  79. return !effects.get(0).getPotion().isInstant();
  80. }
  81. });
  82.  
  83. this.addSlotFilter(2, new ISlotFilter()
  84. {
  85. @Override
  86. public boolean isItemStackValid(ItemStack is)
  87. {
  88. return is.getItem() == Items.GLASS_BOTTLE;
  89. }
  90. });
  91. }
  92.  
  93. @Override
  94. public void writeDataToNBT(NBTTagCompound compound, boolean sync)
  95. {
  96. compound.setInteger("durationLeft", durationLeft);
  97. compound.setInteger("fuelBurn", fuelBurn);
  98. compound.setInteger("fuelBurnTime", fuelBurnTime);
  99.  
  100. NBTTagCompound potionEffectCompound = new NBTTagCompound();
  101.  
  102. if (currentPotionEffect != null)
  103. {
  104. currentPotionEffect.writeCustomPotionEffectToNBT(potionEffectCompound);
  105.  
  106. compound.setTag("currentPotionEffect", potionEffectCompound);
  107. }
  108.  
  109. NBTTagList affectedBlocksList = new NBTTagList();
  110.  
  111. for (BlockPos pos : affectedBlocks)
  112. {
  113. NBTTagCompound posCompound = new NBTTagCompound();
  114.  
  115. posCompound.setInteger("posX", pos.getX());
  116. posCompound.setInteger("posY", pos.getY());
  117. posCompound.setInteger("posZ", pos.getZ());
  118.  
  119. affectedBlocksList.appendTag(posCompound);
  120. }
  121.  
  122. compound.setTag("affectedBlocks", affectedBlocksList);
  123. }
  124.  
  125. @Override
  126. public void readDataFromNBT(NBTTagCompound compound, boolean sync)
  127. {
  128. durationLeft = compound.getInteger("durationLeft");
  129. fuelBurn = compound.getInteger("fuelBurn");
  130. fuelBurnTime = compound.getInteger("fuelBurnTime");
  131.  
  132. if (compound.hasKey("currentPotionEffect"))
  133. {
  134. NBTTagCompound potionEffectCompound = compound.getCompoundTag("currentPotionEffect");
  135.  
  136. this.currentPotionEffect = PotionEffect.readCustomPotionEffectFromNBT(potionEffectCompound);
  137. }
  138.  
  139. NBTTagList affectedBlocksList = compound.getTagList("affectedBlocks", 10);
  140.  
  141. for (int i = 0; i < affectedBlocksList.tagCount(); i++)
  142. {
  143. NBTTagCompound posCompound = affectedBlocksList.getCompoundTagAt(i);
  144.  
  145. this.affectedBlocks.add(new BlockPos(posCompound.getInteger("posX"), posCompound.getInteger("posY"), posCompound.getInteger("posZ")));
  146. }
  147. }
  148.  
  149. @Override
  150. public void update()
  151. {
  152. if (!world.isRemote)
  153. {
  154. int roomSteps = affectedBlocks.size() > 0 ? 2 : 5;
  155. for (int i = 0; i < roomSteps; i++)
  156. {
  157. stepRoomDetection();
  158. }
  159.  
  160. stepPotionTank();
  161. stepFuel();
  162.  
  163. if (fuelBurnTime > 0 && affectedBlocks.size() > 0)
  164. {
  165. stepPotionEffect();
  166. spawnParticles();
  167. }
  168. }
  169. }
  170.  
  171. public int getDurationLeft()
  172. {
  173. return durationLeft;
  174. }
  175.  
  176. public int getDuration()
  177. {
  178. if (this.currentPotionEffect != null)
  179. {
  180. return currentPotionEffect.getDuration();
  181. }
  182. else
  183. {
  184. return 0;
  185. }
  186. }
  187.  
  188. public int getColor()
  189. {
  190. if (this.currentPotionEffect != null)
  191. {
  192. return currentPotionEffect.getPotion().getLiquidColor();
  193. }
  194. else
  195. {
  196. return 0;
  197. }
  198. }
  199.  
  200. private void stepFuel()
  201. {
  202. if (fuelBurnTime > 0)
  203. {
  204. fuelBurnTime--;
  205. }
  206. else if (currentPotionEffect != null && affectedBlocks.size() > 0)
  207. {
  208. if (!getItemHandler().getStackInSlot(0).isEmpty() && durationLeft > 0)
  209. {
  210. fuelBurnTime = fuelBurn = TileEntityFurnace.getItemBurnTime(getItemHandler().getStackInSlot(0));
  211.  
  212. getItemHandler().extractItem(0, 1, false);
  213. }
  214. }
  215. }
  216.  
  217. private void stepPotionTank()
  218. {
  219. if (currentPotionEffect == null)
  220. {
  221. ItemStack newPotion = getItemHandler().getStackInSlot(1);
  222.  
  223. if (!newPotion.isEmpty())
  224. {
  225. ItemStack output = getItemHandler().getStackInSlot(2);
  226.  
  227. if (output.isEmpty() || output.getCount() < 64)
  228. {
  229. List<PotionEffect> effects = PotionUtils.getEffectsFromStack((newPotion));
  230.  
  231. if (effects != null && !effects.isEmpty() && !effects.get(0).getPotion().isInstant())
  232. {
  233. currentPotionEffect = new PotionEffect(effects.get(0));
  234. durationLeft = currentPotionEffect.getDuration();
  235.  
  236. getItemHandler().extractItem(1, 1, false);
  237.  
  238. if (!output.isEmpty())
  239. {
  240. output.grow(1);
  241. }
  242. else
  243. {
  244. getItemHandler().insertItem(2, new ItemStack(Items.GLASS_BOTTLE, 1, 0), false);
  245. }
  246. }
  247. }
  248. }
  249. }
  250. }
  251.  
  252. private void stepPotionEffect()
  253. {
  254. if (!world.isRemote)
  255. {
  256. if (currentPotionEffect != null)
  257. {
  258. durationLeft--;
  259. AxisAlignedBB[] bbs = new AxisAlignedBB[affectedBlocks.size()];
  260.  
  261. int counter = 0;
  262.  
  263. for (BlockPos pos : affectedBlocks)
  264. {
  265. bbs[counter] = new AxisAlignedBB(pos, pos.add(1, 1, 1));
  266.  
  267. counter++;
  268. }
  269.  
  270. for (EntityLivingBase entity : (List<EntityLivingBase>) WorldUtil.getEntitiesWithinAABBs(world, EntityLivingBase.class, bbs))
  271. {
  272. PotionEffect activeEffect = entity.getActivePotionEffect(currentPotionEffect.getPotion());
  273. boolean isNightVision = currentPotionEffect.getPotion() == MobEffects.NIGHT_VISION;
  274. if (activeEffect == null || activeEffect.getDuration() < (isNightVision ? 205 : 3))
  275. {
  276. PotionEffect applyEffect = new PotionEffect(new PotionEffect(currentPotionEffect.getPotion(), isNightVision ? 205 : 80, currentPotionEffect.getAmplifier(), currentPotionEffect.getIsAmbient(), currentPotionEffect.doesShowParticles()));
  277. entity.addPotionEffect(applyEffect);
  278. }
  279. }
  280. }
  281. }
  282.  
  283. if (durationLeft == 0)
  284. {
  285. currentPotionEffect = null;
  286. }
  287. }
  288.  
  289. private void spawnParticles()
  290. {
  291. if (!world.isRemote && currentPotionEffect != null && world.getTotalWorldTime() % 5 == 0)
  292. {
  293. MessagePotionVaporizerParticles message = new MessagePotionVaporizerParticles(new ArrayList<>(affectedBlocks), currentPotionEffect.getPotion().getLiquidColor());
  294. PacketHandler.INSTANCE.sendToAllAround(message, new TargetPoint(this.world.provider.getDimension(), this.pos.getX(), this.pos.getY(), this.pos.getZ(), 32));
  295. }
  296. }
  297.  
  298. HashSet<BlockPos> validBlocks;
  299. HashSet<BlockPos> checkedBlocks;
  300. ArrayList<BlockPos> toBeChecked;
  301.  
  302. int checkCounter;
  303. boolean firstCheck = true;
  304.  
  305. private void stepRoomDetection()
  306. {
  307. if (firstCheck)
  308. {
  309. EnumFacing facing = world.getBlockState(pos).getValue(BlockPotionVaporizer.FACING);
  310. toBeChecked.add(this.pos.offset(facing));
  311. firstCheck = false;
  312. }
  313. if (checkCounter > MAX_BLOCKS)
  314. {
  315. affectedBlocks.clear();
  316. validBlocks.clear();
  317. reset();
  318. }
  319. else
  320. {
  321. if (toBeChecked.size() > 0)
  322. {
  323. BlockPos toCheck = toBeChecked.remove(0);
  324. if (!checkedBlocks.contains(toCheck))
  325. {
  326. checkedBlocks.add(toCheck);
  327. if (world.isBlockLoaded(toCheck) && world.isAirBlock(toCheck))
  328. {
  329. validBlocks.add(toCheck);
  330. checkCounter++;
  331.  
  332. for (EnumFacing facing : EnumFacing.VALUES)
  333. {
  334. BlockPos nextPos = new BlockPos(toCheck.offset(facing));
  335.  
  336. if (!checkedBlocks.contains(nextPos))
  337. {
  338. toBeChecked.add(nextPos);
  339. }
  340. }
  341. }
  342. }
  343. }
  344. else
  345. {
  346. reset();
  347. }
  348. }
  349. }
  350.  
  351. private void reset()
  352. {
  353. affectedBlocks.clear();
  354. affectedBlocks.addAll(validBlocks);
  355.  
  356. checkCounter = 0;
  357. toBeChecked.clear();
  358. validBlocks.clear();
  359. checkedBlocks.clear();
  360.  
  361. EnumFacing facing = world.getBlockState(pos).getValue(BlockPotionVaporizer.FACING);
  362.  
  363. toBeChecked.add(this.pos.offset(facing));
  364. }
  365.  
  366. public int getPotionID()
  367. {
  368. if (currentPotionEffect == null)
  369. {
  370. return 0;
  371. }
  372. else
  373. {
  374. return Potion.getIdFromPotion(currentPotionEffect.getPotion());
  375. }
  376. }
  377.  
  378. public int getAmplifier()
  379. {
  380. if (currentPotionEffect == null)
  381. {
  382. return 0;
  383. }
  384. else
  385. {
  386. return currentPotionEffect.getAmplifier();
  387. }
  388. }
  389.  
  390. public int getFuelBurnTime()
  391. {
  392. return fuelBurnTime;
  393. }
  394.  
  395. public int getFuelBurn()
  396. {
  397. return fuelBurn;
  398. }
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement