Guest User

Untitled

a guest
Mar 8th, 2025
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 35.17 KB | None | 0 0
  1. Here is my entire code
  2.  
  3. Tile rendere;
  4.  
  5.  
  6. package com.Triandon.harderHarvest.tileentity;
  7.  
  8. import com.Triandon.harderHarvest.HarderHarvest;
  9. import com.mojang.blaze3d.matrix.MatrixStack;
  10. import net.minecraft.client.Minecraft;
  11. import net.minecraft.client.renderer.IRenderTypeBuffer;
  12. import net.minecraft.client.renderer.model.ItemCameraTransforms;
  13. import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
  14. import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
  15. import net.minecraft.item.ItemStack;
  16. import net.minecraft.item.Items;
  17. import net.minecraft.util.NonNullList;
  18. import net.minecraft.util.math.vector.Vector3f;
  19.  
  20. public class Campfire_Tile_Renderer extends TileEntityRenderer<Campfire_Tile> {
  21. private final Minecraft mc = Minecraft.getInstance();
  22. public Campfire_Tile_Renderer(TileEntityRendererDispatcher rendererDispatcherIn) {
  23. super(rendererDispatcherIn);
  24. }
  25.  
  26. @Override
  27. public void render(Campfire_Tile tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
  28. NonNullList<ItemStack> nonnulllist = tileEntityIn.getInventory();
  29. ItemStack itemStack = tileEntityIn.getItem();
  30. if (!itemStack.isEmpty()) {
  31. System.out.println("ItemStack in slot 0: " + itemStack.getItem().getRegistryName() + ", Count: " + itemStack.getCount());
  32. } else {
  33. System.out.println("ItemStack is empty!");
  34. HarderHarvest.LOGGER.info(nonnulllist);
  35. HarderHarvest.LOGGER.info(tileEntityIn.getInventory());
  36. HarderHarvest.LOGGER.info(itemStack);
  37. }
  38.  
  39. if (itemStack != ItemStack.EMPTY || itemStack.getItem() != Items.AIR || !itemStack.isEmpty()) {
  40. matrixStackIn.push();
  41. matrixStackIn.translate(0.5D, 0.5D, 0.5D);
  42. matrixStackIn.rotate(Vector3f.XP.rotationDegrees(90.0F));
  43. //matrixStackIn.translate(-0.3125D, -0.3125D, 0.0D);
  44. matrixStackIn.scale(0.75F, 0.75F, 0.75F);
  45. Minecraft.getInstance().getItemRenderer().renderItem(itemStack, ItemCameraTransforms.TransformType.GROUND, combinedLightIn, combinedOverlayIn, matrixStackIn, bufferIn);
  46. matrixStackIn.pop();
  47. }
  48. }
  49.  
  50. }
  51.  
  52. The tile entity (campfrie tile)
  53.  
  54. package com.Triandon.harderHarvest.tileentity;
  55.  
  56. import com.Triandon.harderHarvest.block.custom.Campfire;
  57. import net.minecraft.block.BlockState;
  58. import net.minecraft.entity.item.ItemEntity;
  59. import net.minecraft.entity.player.PlayerEntity;
  60. import net.minecraft.inventory.IClearable;
  61. import net.minecraft.inventory.IInventory;
  62. import net.minecraft.inventory.Inventory;
  63. import net.minecraft.inventory.ItemStackHelper;
  64. import net.minecraft.item.Item;
  65. import net.minecraft.item.ItemStack;
  66. import net.minecraft.item.Items;
  67. import net.minecraft.item.crafting.CampfireCookingRecipe;
  68. import net.minecraft.item.crafting.IRecipeType;
  69. import net.minecraft.item.crafting.RecipeManager;
  70. import net.minecraft.nbt.CompoundNBT;
  71. import net.minecraft.network.play.server.SUpdateTileEntityPacket;
  72. import net.minecraft.tileentity.FurnaceTileEntity;
  73. import net.minecraft.tileentity.ITickableTileEntity;
  74. import net.minecraft.tileentity.TileEntity;
  75. import net.minecraft.tileentity.TileEntityType;
  76. import net.minecraft.util.NonNullList;
  77. import net.minecraft.util.math.BlockPos;
  78. import net.minecraft.world.server.ServerWorld;
  79. import net.minecraftforge.common.ForgeHooks;
  80.  
  81. import javax.annotation.Nullable;
  82. import java.util.Map;
  83. import java.util.Optional;
  84. import java.util.Random;
  85.  
  86. public class Campfire_Tile extends TileEntity implements IInventory, ITickableTileEntity, IClearable {
  87. public static int slots = 1;
  88. private int burningTimeTicks;
  89.  
  90. protected NonNullList<ItemStack> inventory = NonNullList.withSize(slots,ItemStack.EMPTY);
  91. protected NonNullList<ItemStack> fuelInventory = NonNullList.withSize(16,ItemStack.EMPTY);
  92. private int cookingTime;
  93. private int totalCookingTime;
  94.  
  95. public Campfire_Tile(TileEntityType<?> tileEntityTypeIn) {
  96. super(tileEntityTypeIn);
  97. this.burningTimeTicks = 1600;
  98. }
  99.  
  100. public Campfire_Tile(){
  101. this(ModTileEntities.CAMPFIRE_TILE.get());
  102. }
  103.  
  104. public void tick(){
  105. boolean lit = this.getBlockState().get(Campfire.LIT);
  106. boolean flag = this.world.isRemote;
  107.  
  108. if(!flag){
  109. if(!inventory.get(0).isEmpty()){
  110. this.inventoryChanged();
  111. }
  112. }
  113.  
  114. if(!world.isRemote){
  115. //HarderHarvest.LOGGER.info(getItem());
  116. ItemStack stack = inventory.get(0);
  117. if(!stack.isEmpty()){
  118. Optional<CampfireCookingRecipe> recipe = findMatchingRecipe(stack);
  119. BlockState state = getBlockState();
  120. if(recipe.isPresent() && state.get(Campfire.FIRE_LEVEL) < 4 && !state.get(Campfire.WATERLOGGED) && state.get(Campfire.LIT)){
  121. cookingTime++;
  122.  
  123. if(cookingTime >= totalCookingTime){
  124. ItemStack cookedItem = recipe.get().getCraftingResult(new Inventory(stack));
  125. BlockPos pos1 = getPos();
  126. ItemEntity itemEntity = new ItemEntity(world,pos1.getX(),pos1.getY(),pos1.getZ(),
  127. cookedItem);
  128. world.addEntity(itemEntity);
  129. inventory.set(0,ItemStack.EMPTY);
  130. markDirty();
  131. }
  132. }
  133. }
  134.  
  135.  
  136. if(burningTimeTicks > 0){
  137. if(!fuelInventory.isEmpty()){
  138. removeRandomFuel();
  139. }
  140.  
  141. burningTimeTicks--;
  142.  
  143. updateModel(burningTimeTicks);
  144. }
  145. else {
  146. world.setBlockState(getPos(),getBlockState().with(Campfire.LIT,true).with(Campfire.FIRE_LEVEL,5),11);
  147. markDirty();
  148. }
  149. }
  150. }
  151.  
  152. @Nullable
  153. public Optional<CampfireCookingRecipe> findMatchingRecipe(ItemStack stack){
  154. if(world instanceof ServerWorld){
  155. RecipeManager recipeManager = world.getRecipeManager();
  156. return recipeManager.getRecipe(IRecipeType.CAMPFIRE_COOKING, new Inventory(stack),world);
  157. }
  158. return Optional.empty();
  159. }
  160.  
  161. public void setInventory(NonNullList<ItemStack> inventory) {
  162. this.inventory = inventory;
  163. }
  164.  
  165. @Override
  166. public int getSizeInventory() {
  167. return inventory.size();
  168. }
  169.  
  170.  
  171. @Override
  172. public boolean isEmpty() {
  173. return inventory.stream().allMatch(ItemStack::isEmpty);
  174. }
  175.  
  176. @Override
  177. public ItemStack getStackInSlot(int index) {
  178. return this.inventory.get(index);
  179. }
  180.  
  181. public ItemStack getItem(){
  182. return this.inventory.get(0);
  183. }
  184.  
  185. public NonNullList<ItemStack> getInventory() {
  186. return this.inventory;
  187. }
  188.  
  189. @Override
  190. public ItemStack decrStackSize(int index, int count) {
  191. return ItemStackHelper.getAndSplit(inventory,index,count);
  192. }
  193.  
  194. @Override
  195. public ItemStack removeStackFromSlot(int index) {
  196. this.inventoryChanged();
  197. return ItemStackHelper.getAndRemove(inventory,index);
  198. }
  199.  
  200. @Override
  201. public void setInventorySlotContents(int index, ItemStack stack) {
  202. inventory.set(index,stack);
  203. markDirty();
  204. }
  205.  
  206. @Override
  207. public boolean isUsableByPlayer(PlayerEntity player) {
  208. return true;
  209. }
  210.  
  211. public void read(BlockState state, CompoundNBT nbt) {
  212. super.read(state, nbt);
  213. this.inventory.clear();
  214. ItemStackHelper.loadAllItems(nbt, this.inventory);
  215. if (nbt.contains("CookingTimes", 99)) {
  216. this.cookingTime = nbt.getInt("CookingTimes");
  217. }
  218.  
  219. if (nbt.contains("TotalCookingTime", 99)) {
  220. this.totalCookingTime = nbt.getInt("TotalCookingTime");
  221. }
  222.  
  223. if(nbt.contains("burningTimeTicks", 99)){
  224. this.burningTimeTicks = nbt.getInt("burningTimeTicks");
  225. }
  226.  
  227. }
  228.  
  229. public CompoundNBT write(CompoundNBT compound) {
  230. this.writeItems(compound);
  231. compound.putInt("CookingTimes", this.cookingTime);
  232. compound.putInt("TotalCookingTime", this.totalCookingTime);
  233. compound.putInt("burningTimeTicks", this.burningTimeTicks);
  234. return compound;
  235. }
  236.  
  237. private CompoundNBT writeItems(CompoundNBT compound) {
  238. super.write(compound);
  239. ItemStackHelper.saveAllItems(compound, this.inventory, true);
  240. return compound;
  241. }
  242.  
  243.  
  244. @Nullable
  245. @Override
  246. public SUpdateTileEntityPacket getUpdatePacket() {
  247. return new SUpdateTileEntityPacket(this.pos,0,this.getUpdateTag());
  248. }
  249.  
  250. @Override
  251. public CompoundNBT getUpdateTag() {
  252. CompoundNBT compoundNBT = new CompoundNBT();
  253. this.write(compoundNBT);
  254. return compoundNBT;
  255. }
  256.  
  257. @Override
  258. public void clear() {
  259. inventory.clear();
  260. }
  261.  
  262. public boolean addItem(ItemStack stack, int cookingTime){
  263. if(inventory.get(0).isEmpty() || inventory.get(0).equals(Items.AIR)){
  264. //inventory.add(0,stack.split(1));
  265. this.totalCookingTime = cookingTime; //Set the tot cooking time
  266. this.cookingTime = 0; //Reset cooking time
  267. this.inventory.set(0,stack.split(1));
  268. this.inventoryChanged();
  269. markDirty();
  270. return true;
  271. }
  272. return false;
  273. }
  274.  
  275. private void inventoryChanged(){
  276. this.markDirty();
  277. this.getWorld().notifyBlockUpdate(this.getPos(),this.getBlockState(),this.getBlockState(),11);
  278. }
  279.  
  280. public NonNullList<ItemStack> getFuelInventory() {
  281. return fuelInventory;
  282. }
  283.  
  284. public void setFuelInventory(NonNullList<ItemStack> fuelInventory) {
  285. this.fuelInventory = fuelInventory;
  286. }
  287.  
  288. public int getBurningTimeTicks() {
  289. return burningTimeTicks;
  290. }
  291.  
  292. public void setBurningTimeTicks(int burningTimeTicks) {
  293. this.burningTimeTicks = burningTimeTicks;
  294. markDirty();
  295. }
  296.  
  297. public boolean isCampfireLit(){
  298. BlockState state = this.getBlockState();
  299. return state.get(Campfire.LIT);
  300. }
  301.  
  302.  
  303. public boolean addFuel(ItemStack stack){
  304. Integer burnTime1 = ForgeHooks.getBurnTime(stack);
  305. int burnTime3 = 0;
  306.  
  307. if(burnTime1 > 0 && burnTime1 != null){
  308. burnTime3 = burnTime1;
  309. }
  310. else {
  311. Map<Item, Integer> burnTimes = FurnaceTileEntity.getBurnTimes();
  312. Integer burnTime2 = burnTimes.get(stack.getItem());
  313. if(burnTime2 != null){
  314. burnTime3 = burnTime2;
  315. }
  316. }
  317.  
  318. if(burnTime3 <= 0){
  319. return false; // Item cannot be used as fue
  320. }
  321.  
  322. for (int i = 0; i < fuelInventory.size(); i++) {
  323. if(fuelInventory.get(i).isEmpty()){
  324. fuelInventory.set(i,stack.split(1));
  325. burningTimeTicks += burnTime3;
  326. markDirty();
  327. return true;
  328. }
  329. }
  330. return false; // no space
  331. }
  332.  
  333. public void removeRandomFuel(){
  334. Random random = new Random();
  335. int randomIndex = random.nextInt(fuelInventory.size());
  336.  
  337. //Check if fuel inventory is not empty
  338. if(!fuelInventory.get(randomIndex).isEmpty()){
  339. ItemStack removedItem = fuelInventory.get(randomIndex);
  340. fuelInventory.set(randomIndex,ItemStack.EMPTY);
  341.  
  342. markDirty();
  343. }
  344. }
  345.  
  346. public void updateModel(int burningTimeTicks){
  347. boolean litState = isCampfireLit();
  348. BlockState currentState = getBlockState();
  349. BlockState newState = currentState;
  350.  
  351. if(litState){
  352. if(burningTimeTicks <= 0){
  353. newState = currentState.with(Campfire.FIRE_LEVEL,5);
  354. }else if(burningTimeTicks < 600){
  355. newState = currentState.with(Campfire.FIRE_LEVEL,4);
  356. }else if (burningTimeTicks < 1600){
  357. newState = currentState.with(Campfire.FIRE_LEVEL,1);
  358. }else if (burningTimeTicks < 3200){
  359. newState = currentState.with(Campfire.FIRE_LEVEL,2);
  360. }else if (burningTimeTicks < 6400){
  361. newState = currentState.with(Campfire.FIRE_LEVEL,3);
  362. }else {
  363. newState = currentState.with(Campfire.FIRE_LEVEL,3);
  364. }
  365. }
  366. else {
  367. newState = currentState.with(Campfire.FIRE_LEVEL,0);
  368. }
  369.  
  370. //Only update if states dont match
  371. if(currentState.get(Campfire.FIRE_LEVEL) != newState.get(Campfire.FIRE_LEVEL)){
  372. world.setBlockState(pos,newState,11);
  373. }
  374. markDirty();
  375. }
  376. }
  377.  
  378. The block (campfireblock)
  379.  
  380.  
  381. package com.Triandon.harderHarvest.block.custom;
  382.  
  383. import com.Triandon.harderHarvest.HarderHarvest;
  384. import com.Triandon.harderHarvest.tileentity.Campfire_Tile;
  385. import com.Triandon.harderHarvest.tileentity.ModTileEntities;
  386. import net.minecraft.block.*;
  387. import net.minecraft.entity.Entity;
  388. import net.minecraft.entity.LivingEntity;
  389. import net.minecraft.entity.player.PlayerEntity;
  390. import net.minecraft.entity.player.ServerPlayerEntity;
  391. import net.minecraft.entity.projectile.ProjectileEntity;
  392. import net.minecraft.fluid.FluidState;
  393. import net.minecraft.fluid.Fluids;
  394. import net.minecraft.item.BlockItemUseContext;
  395. import net.minecraft.item.FlintAndSteelItem;
  396. import net.minecraft.item.Item;
  397. import net.minecraft.item.ItemStack;
  398. import net.minecraft.item.crafting.CampfireCookingRecipe;
  399. import net.minecraft.network.play.server.SPlaySoundPacket;
  400. import net.minecraft.particles.ParticleTypes;
  401. import net.minecraft.state.BooleanProperty;
  402. import net.minecraft.state.DirectionProperty;
  403. import net.minecraft.state.IntegerProperty;
  404. import net.minecraft.state.StateContainer;
  405. import net.minecraft.state.properties.BlockStateProperties;
  406. import net.minecraft.stats.Stats;
  407. import net.minecraft.tags.BlockTags;
  408. import net.minecraft.tileentity.FurnaceTileEntity;
  409. import net.minecraft.tileentity.TileEntity;
  410. import net.minecraft.util.*;
  411. import net.minecraft.util.math.BlockPos;
  412. import net.minecraft.util.math.BlockRayTraceResult;
  413. import net.minecraft.util.math.shapes.ISelectionContext;
  414. import net.minecraft.util.math.shapes.VoxelShape;
  415. import net.minecraft.util.math.vector.Vector3d;
  416. import net.minecraft.world.IBlockReader;
  417. import net.minecraft.world.IWorld;
  418. import net.minecraft.world.World;
  419. import net.minecraft.world.server.ServerWorld;
  420. import net.minecraftforge.api.distmarker.Dist;
  421. import net.minecraftforge.api.distmarker.OnlyIn;
  422. import net.minecraftforge.common.ForgeHooks;
  423.  
  424. import javax.annotation.Nullable;
  425. import java.util.Map;
  426. import java.util.Optional;
  427. import java.util.Random;
  428.  
  429. public class Campfire extends ContainerBlock implements IWaterLoggable {
  430. private static final int LIGHT_LEVEL = 15; //Max light level
  431. protected static final VoxelShape SHAPE = Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 7.0D, 16.0D);
  432. public static final BooleanProperty LIT = BlockStateProperties.LIT;
  433. public static final BooleanProperty STICK = BooleanProperty.create("stick");
  434. public static final IntegerProperty FIRE_LEVEL = IntegerProperty.create("fire_level",0,5);
  435. public static final DirectionProperty HORIZONTAL_FACING = BlockStateProperties.HORIZONTAL_FACING;
  436. public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
  437.  
  438. private final boolean smokey;
  439. private final int fireDamage;
  440.  
  441. public Campfire(boolean smokey,int firedDamage,Properties builder) {
  442. super(builder);
  443. this.smokey = smokey;
  444. this.fireDamage = firedDamage;
  445. this.setDefaultState(this.getStateContainer().getBaseState().with(HORIZONTAL_FACING, Direction.NORTH).with(LIT,Boolean.valueOf(false)).with(STICK, Boolean.valueOf(false)).with(FIRE_LEVEL,Integer.valueOf(0)));
  446. }
  447.  
  448. @Override
  449. protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
  450. builder.add(HORIZONTAL_FACING,LIT, STICK, FIRE_LEVEL, WATERLOGGED);
  451. }
  452.  
  453. @Nullable
  454. @Override
  455. public BlockState getStateForPlacement(BlockItemUseContext context) {
  456. IWorld iWorld = context.getWorld();
  457. BlockPos blockPos = context.getPos();
  458. boolean flag = iWorld.getFluidState(blockPos).getFluid() == Fluids.WATER;
  459. return this.getDefaultState().with(WATERLOGGED,Boolean.valueOf(flag)).with(LIT,Boolean.valueOf(!flag)).with(HORIZONTAL_FACING, context.getPlacementHorizontalFacing().getOpposite());
  460. }
  461.  
  462. @Override
  463. public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
  464. super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
  465. worldIn.setBlockState(pos,state.with(Campfire.LIT,false),3);
  466. }
  467.  
  468. @Nullable
  469. @Override
  470. public TileEntity createTileEntity(BlockState state, IBlockReader world) {
  471. return ModTileEntities.CAMPFIRE_TILE.get().create();
  472. }
  473.  
  474. @Override
  475. public boolean hasTileEntity(BlockState state) {
  476. return true;
  477. }
  478.  
  479. @Override
  480. public BlockRenderType getRenderType(BlockState state) {
  481. return BlockRenderType.MODEL;
  482. }
  483.  
  484. @Override
  485. public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
  486. return SHAPE;
  487. }
  488.  
  489. @Override
  490. public void onBlockAdded(BlockState state, World worldIn, BlockPos pos, BlockState oldState, boolean isMoving) {
  491. super.onBlockAdded(state, worldIn, pos, oldState, isMoving);
  492. TileEntity tileEntity = worldIn.getTileEntity(pos);
  493. if(tileEntity instanceof Campfire_Tile){
  494. tileEntity.markDirty();
  495. }
  496. }
  497.  
  498. @SuppressWarnings("deprecation")
  499. @Override
  500. public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
  501. ItemStack heldItem = player.getHeldItem(handIn);
  502. TileEntity tileEntity = worldIn.getTileEntity(pos);
  503. Random random = new Random();
  504.  
  505. if(tileEntity instanceof Campfire_Tile){
  506. Campfire_Tile campfireTileEntity = (Campfire_Tile) tileEntity;
  507. HarderHarvest.LOGGER.info(campfireTileEntity.getInventory());
  508. HarderHarvest.LOGGER.info(campfireTileEntity.getInventory().get(0));
  509. campfireTileEntity.markDirty();
  510. worldIn.notifyBlockUpdate(pos,state,state,3);
  511.  
  512.  
  513.  
  514. // Check if the player is trying to retrieve the cooked item
  515. if(heldItem.isEmpty() && !campfireTileEntity.getStackInSlot(0).isEmpty()){
  516. ItemStack cookedItem = campfireTileEntity.removeStackFromSlot(0);
  517. player.addItemStackToInventory(cookedItem);
  518. return ActionResultType.SUCCESS;
  519. }
  520.  
  521. // Logic for adding food items
  522. Optional<CampfireCookingRecipe> recipeOptional = campfireTileEntity.findMatchingRecipe(heldItem);
  523. if(recipeOptional.isPresent()){
  524. HarderHarvest.LOGGER.info("Recipe fount for:"+ heldItem.getItem().getRegistryName());
  525. if (!worldIn.isRemote && campfireTileEntity.addItem(heldItem, recipeOptional.get().getCookTime())) {
  526. player.addStat(Stats.INTERACT_WITH_CAMPFIRE);
  527. campfireTileEntity.markDirty();
  528. worldIn.notifyBlockUpdate(pos,state,state,3);
  529. //campfireTileEntity.addItem(heldItem,recipeOptional.get().getCookTime());
  530. //campfireTileEntity.setItemInInventory(heldItem);
  531. return ActionResultType.SUCCESS;
  532. }else {
  533. return ActionResultType.PASS;
  534. }
  535.  
  536. }
  537. else {
  538. HarderHarvest.LOGGER.info("No recipes found!!!");
  539. }
  540.  
  541.  
  542. //Adding fuel logic
  543. if(!worldIn.isRemote && state.get(FIRE_LEVEL) != 5){
  544. Integer burnTime = ForgeHooks.getBurnTime(heldItem,null);
  545. if(burnTime == null || burnTime <= 0){
  546. Map<Item, Integer> burnTimes = FurnaceTileEntity.getBurnTimes();
  547. burnTime = burnTimes.get(heldItem.getItem());
  548. }
  549.  
  550. HarderHarvest.LOGGER.info(burnTime+ " Burn time");
  551.  
  552. if(burnTime != null && burnTime > 0){
  553. if(campfireTileEntity.addFuel(heldItem)){
  554. if(state.get(FIRE_LEVEL) == 4){
  555. if(worldIn.rand.nextInt(100) < 30){
  556. worldIn.playSound(player,pos,SoundEvents.ITEM_FLINTANDSTEEL_USE,SoundCategory.BLOCKS,1,random.nextFloat() * 0.4F + 0.8F);
  557. ((ServerPlayerEntity) player).connection.sendPacket(new SPlaySoundPacket(SoundEvents.ITEM_FLINTANDSTEEL_USE.getRegistryName(), SoundCategory.BLOCKS, new Vector3d(pos.getX(), pos.getY(), pos.getZ()), 1.0F, random.nextFloat() * 0.4F + 0.8F));
  558. worldIn.playSound(player,pos,SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM,SoundCategory.BLOCKS,0.5F,random.nextFloat() * 0.4F + 0.8F);
  559. ((ServerPlayerEntity) player).connection.sendPacket(new SPlaySoundPacket(SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM.getRegistryName(), SoundCategory.BLOCKS, new Vector3d(pos.getX(), pos.getY(), pos.getZ()), 0.5F, random.nextFloat() * 0.4F + 0.8F));
  560. return ActionResultType.SUCCESS;
  561. }
  562. else {
  563. worldIn.playSound(player,pos,SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM,SoundCategory.BLOCKS,1,random.nextFloat() * 0.4F + 0.8F);
  564. ((ServerPlayerEntity) player).connection.sendPacket(new SPlaySoundPacket(SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM.getRegistryName(), SoundCategory.BLOCKS, new Vector3d(pos.getX(), pos.getY(), pos.getZ()), 1.0F, random.nextFloat() * 0.4F + 0.8F));
  565. return ActionResultType.SUCCESS;
  566. }
  567. }
  568. else {
  569. worldIn.playSound(player,pos,SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM,SoundCategory.BLOCKS,1,random.nextFloat() * 0.4F + 0.8F);
  570. ((ServerPlayerEntity) player).connection.sendPacket(new SPlaySoundPacket(SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM.getRegistryName(), SoundCategory.BLOCKS, new Vector3d(pos.getX(), pos.getY(), pos.getZ()), 1.0F, random.nextFloat() * 0.4F + 0.8F));
  571. return ActionResultType.SUCCESS;
  572. }
  573. }
  574. else {
  575. HarderHarvest.LOGGER.info("Failed to add fuel");
  576. }
  577. }
  578.  
  579.  
  580. }
  581.  
  582.  
  583. }
  584.  
  585.  
  586.  
  587. //other logic such as flint and steel
  588. if(heldItem.getItem() instanceof FlintAndSteelItem){
  589. Campfire_Tile campfireTileEntity = (Campfire_Tile) tileEntity;
  590. if(!state.get(LIT) && !state.get(WATERLOGGED)){
  591. worldIn.playSound(player, pos, SoundEvents.ITEM_FLINTANDSTEEL_USE, SoundCategory.BLOCKS, 1.0F, random.nextFloat() * 0.4F + 0.8F);
  592. worldIn.setBlockState(pos,state.with(Campfire.LIT,true).with(FIRE_LEVEL,1),11);
  593.  
  594. if(heldItem.getItem() instanceof FlintAndSteelItem){
  595. heldItem.damageItem(1,player,player1->
  596. player1.sendBreakAnimation(handIn));
  597. worldIn.playSound(player, pos, SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE, SoundCategory.BLOCKS, 1.0F, random.nextFloat() * 0.4F + 0.8F);
  598.  
  599. }
  600. }
  601.  
  602. return ActionResultType.SUCCESS;
  603.  
  604. }
  605.  
  606. return ActionResultType.PASS;
  607. }
  608.  
  609. //Maybe delete later no usage
  610. public static boolean canBeLit(BlockState state) {
  611. return state.isInAndMatches(BlockTags.CAMPFIRES, (stateIn) -> {
  612. return stateIn.hasProperty(BlockStateProperties.WATERLOGGED) && stateIn.hasProperty(BlockStateProperties.LIT);
  613. }) && !state.get(BlockStateProperties.WATERLOGGED) && !state.get(BlockStateProperties.LIT);
  614. }
  615.  
  616. @Override
  617. public int getLightValue(BlockState state, IBlockReader world, BlockPos pos) {
  618. if(state.get(Campfire.LIT) && state.get(FIRE_LEVEL) < 4){ //Emit light only if lit and not FireLevel [4,5]
  619. return LIGHT_LEVEL;
  620. }
  621. return 0; //No light
  622. }
  623.  
  624. @Override
  625. public boolean receiveFluid(IWorld worldIn, BlockPos pos, BlockState state, FluidState fluidStateIn) {
  626. if(!state.get(BlockStateProperties.WATERLOGGED) && fluidStateIn.getFluid() == Fluids.WATER){
  627. boolean wasLit = state.get(LIT);
  628. if(wasLit){
  629. if(!worldIn.isRemote()){
  630. worldIn.playSound((PlayerEntity)null, pos, SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE, SoundCategory.BLOCKS, 1.0F, 1.0F);
  631. }
  632. //extinguish
  633. }
  634.  
  635. int currentFireLevel = state.get(FIRE_LEVEL);
  636. if(currentFireLevel != 5){
  637. worldIn.setBlockState(pos,state.with(WATERLOGGED,Boolean.valueOf(true)).with(LIT,Boolean.valueOf(false)).with(FIRE_LEVEL,0),11);
  638. }else {
  639. worldIn.setBlockState(pos,state.with(WATERLOGGED,Boolean.valueOf(true)).with(LIT,Boolean.valueOf(true)).with(FIRE_LEVEL,5),11);
  640. }
  641.  
  642. worldIn.getPendingFluidTicks().scheduleTick(pos,fluidStateIn.getFluid(),fluidStateIn.getFluid().getTickRate(worldIn));
  643. return true;
  644. }else {
  645. return false;
  646. }
  647. }
  648.  
  649. @Override
  650. public void fillWithRain(World worldIn, BlockPos pos) {
  651. super.fillWithRain(worldIn, pos);
  652.  
  653. BlockState state = worldIn.getBlockState(pos);
  654. if(worldIn.isRainingAt(pos.up())){
  655. if(state.get(Campfire.LIT)){
  656. worldIn.playSound(null,pos, SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE, SoundCategory.BLOCKS,1.0F,1.0F);
  657. //extinguish(worldIn,pos,state);
  658. worldIn.setBlockState(pos,state.with(Campfire.LIT,false),11);
  659. }
  660. }
  661.  
  662. }
  663.  
  664. @Override
  665. public void onProjectileCollision(World worldIn, BlockState state, BlockRayTraceResult hit, ProjectileEntity projectile) {
  666. if(!worldIn.isRemote && projectile.isBurning()){
  667. Entity entity = projectile.getShooter();
  668. boolean flag = entity == null || entity instanceof PlayerEntity || net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(worldIn, entity);
  669. if(flag && !state.get(LIT) && !state.get(WATERLOGGED)){
  670. BlockPos blockPos = hit.getPos();
  671. worldIn.setBlockState(blockPos,state.with(BlockStateProperties.LIT,Boolean.valueOf(true)),11);
  672. }
  673. }
  674. }
  675.  
  676. @OnlyIn(Dist.CLIENT)
  677. public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
  678. if (stateIn.get(LIT) && stateIn.get(FIRE_LEVEL) < 4) {
  679. if (rand.nextInt(10) == 0) {
  680. worldIn.playSound((double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, SoundEvents.BLOCK_CAMPFIRE_CRACKLE, SoundCategory.BLOCKS, 0.5F + rand.nextFloat(), rand.nextFloat() * 0.7F + 0.6F, false);
  681. }
  682.  
  683. if (this.smokey && rand.nextInt(5) == 0) {
  684. for(int i = 0; i < rand.nextInt(1) + 1; ++i) {
  685. worldIn.addParticle(ParticleTypes.LAVA, (double)pos.getX() + 0.5D, (double)pos.getY() + 0.5D, (double)pos.getZ() + 0.5D, (double)(rand.nextFloat() / 2.0F), 5.0E-5D, (double)(rand.nextFloat() / 2.0F));
  686. }
  687. }
  688.  
  689. }
  690. }
  691.  
  692.  
  693. @Nullable
  694. @Override
  695. public TileEntity createNewTileEntity(IBlockReader worldIn) {
  696. return new Campfire_Tile();
  697. }
  698.  
  699. @Override
  700. public void randomTick(BlockState state, ServerWorld worldIn, BlockPos pos, Random random) {
  701. super.randomTick(state, worldIn, pos, random);
  702. TileEntity tileEntity = worldIn.getTileEntity(pos);
  703. if(tileEntity instanceof Campfire_Tile){
  704. Campfire_Tile campfireTile = (Campfire_Tile) tileEntity;
  705. campfireTile.tick();
  706. }
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714. }
  715.  
  716. @Override
  717. public boolean ticksRandomly(BlockState state) {
  718. return true;
  719. }
  720.  
  721. @Override
  722. public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
  723. if(stateIn.get(WATERLOGGED)){
  724. worldIn.getPendingFluidTicks().scheduleTick(currentPos,Fluids.WATER,Fluids.WATER.getTickRate(worldIn));
  725. }
  726. return super.updatePostPlacement(stateIn,facing,facingState,worldIn,currentPos,facingPos);
  727. }
  728.  
  729. @Override
  730. public FluidState getFluidState(BlockState state) {
  731. return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : Fluids.EMPTY.getDefaultState();
  732. }
  733. }
  734.  
  735. Other code;
  736.  
  737. package com.Triandon.harderHarvest;
  738.  
  739. import com.Triandon.harderHarvest.block.ModBlocks;
  740. import com.Triandon.harderHarvest.core.config.HarderHarvestConfig;
  741. import com.Triandon.harderHarvest.events.woodEvent.WoodenByProduct;
  742. import com.Triandon.harderHarvest.items.ModItems;
  743. import com.Triandon.harderHarvest.tileentity.Campfire_Tile_Renderer;
  744. import com.Triandon.harderHarvest.tileentity.ModTileEntities;
  745. import net.minecraft.block.Block;
  746. import net.minecraft.block.Blocks;
  747. import net.minecraft.client.renderer.RenderType;
  748. import net.minecraft.client.renderer.RenderTypeLookup;
  749. import net.minecraftforge.common.MinecraftForge;
  750. import net.minecraftforge.event.RegistryEvent;
  751. import net.minecraftforge.eventbus.api.IEventBus;
  752. import net.minecraftforge.eventbus.api.SubscribeEvent;
  753. import net.minecraftforge.fml.InterModComms;
  754. import net.minecraftforge.fml.ModList;
  755. import net.minecraftforge.fml.ModLoadingContext;
  756. import net.minecraftforge.fml.client.registry.ClientRegistry;
  757. import net.minecraftforge.fml.common.Mod;
  758. import net.minecraftforge.fml.config.ModConfig;
  759. import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
  760. import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
  761. import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
  762. import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
  763. import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
  764. import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
  765. import org.apache.logging.log4j.LogManager;
  766. import org.apache.logging.log4j.Logger;
  767.  
  768. // The value here should match an entry in the META-INF/mods.toml file
  769. @Mod(HarderHarvest.MOD_ID)
  770. public class HarderHarvest
  771. {
  772. public static final String MOD_ID = "harderharvest";
  773.  
  774. // Directly reference a log4j logger.
  775. public static final Logger LOGGER = LogManager.getLogger();
  776.  
  777. public HarderHarvest() {
  778. IEventBus eventBus = FMLJavaModLoadingContext.get().getModEventBus();
  779.  
  780. ModItems.register(eventBus);
  781. if(ModList.get().isLoaded("harderstones")){
  782. ModItems.register_HARDERSTONES(eventBus);
  783. }
  784. ModBlocks.register(eventBus);
  785. ModTileEntities.register(eventBus);
  786.  
  787. ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, HarderHarvestConfig.SPEC,"harderharvest-common.toml");
  788.  
  789. //ModRecipeTypes.register(eventBus);
  790.  
  791. // Register the setup method for modloading
  792. eventBus.addListener(this::setup);
  793. // Register the enqueueIMC method for modloading
  794. eventBus.addListener(this::enqueueIMC);
  795. // Register the processIMC method for modloading
  796. eventBus.addListener(this::processIMC);
  797. // Register the doClientStuff method for modloading
  798. eventBus.addListener(this::doClientStuff);
  799.  
  800. // Register ourselves for server and other game events we are interested in
  801. MinecraftForge.EVENT_BUS.register(this);
  802. }
  803.  
  804. private void setup(final FMLCommonSetupEvent event)
  805. {
  806. WoodenByProduct.registerStrippableBlocks();
  807. // some preinit code
  808. LOGGER.info("HELLO FROM PREINIT");
  809. LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
  810. }
  811.  
  812. private void doClientStuff(final FMLClientSetupEvent event) {
  813. // do something that can only be done on the client
  814.  
  815. event.enqueueWork(()->{
  816. RenderTypeLookup.setRenderLayer(ModBlocks.FIREWOOD_BLOCK.get(), RenderType.getCutout());
  817. RenderTypeLookup.setRenderLayer(ModBlocks.CAMPFIRE_BLOCK.get(), RenderType.getCutout());
  818. ClientRegistry.bindTileEntityRenderer(ModTileEntities.CAMPFIRE_TILE.get(), Campfire_Tile_Renderer::new);
  819. });
  820. }
  821.  
  822. private void enqueueIMC(final InterModEnqueueEvent event)
  823. {
  824. // some example code to dispatch IMC to another mod
  825. InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
  826. }
  827.  
  828. private void processIMC(final InterModProcessEvent event)
  829. {
  830. // some example code to receive and process InterModComms from other mods
  831. }
  832. // You can use SubscribeEvent and let the Event Bus discover methods to call
  833. @SubscribeEvent
  834. public void onServerStarting(FMLServerStartingEvent event) {
  835. // do something when the server starts
  836. LOGGER.info("HELLO from server starting");
  837. }
  838.  
  839. // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
  840. // Event bus for receiving Registry Events)
  841. @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
  842. public static class RegistryEvents {
  843. @SubscribeEvent
  844. public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
  845. // register a new block here
  846. LOGGER.info("HELLO from Register Block");
  847. }
  848. }
  849. }
  850.  
  851.  
  852.  
  853.  
  854. package com.Triandon.harderHarvest.tileentity;
  855.  
  856. import com.Triandon.harderHarvest.HarderHarvest;
  857. import com.Triandon.harderHarvest.block.ModBlocks;
  858. import net.minecraft.tileentity.TileEntityType;
  859. import net.minecraftforge.eventbus.api.IEventBus;
  860. import net.minecraftforge.fml.RegistryObject;
  861. import net.minecraftforge.registries.DeferredRegister;
  862. import net.minecraftforge.registries.ForgeRegistries;
  863.  
  864. public class ModTileEntities {
  865.  
  866. public static DeferredRegister<TileEntityType<?>> TILE_ENTITIES =
  867. DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, HarderHarvest.MOD_ID);
  868.  
  869. public static RegistryObject<TileEntityType<Firewood_Tile>> FIREWOOD_TILE =
  870. TILE_ENTITIES.register("firewood_tile",()->TileEntityType.Builder.create(
  871. Firewood_Tile::new, ModBlocks.FIREWOOD_BLOCK.get()).build(null));
  872.  
  873. public static RegistryObject<TileEntityType<Campfire_Tile>> CAMPFIRE_TILE =
  874. TILE_ENTITIES.register("campfire_tile",()->TileEntityType.Builder.create(
  875. Campfire_Tile::new, ModBlocks.CAMPFIRE_BLOCK.get()).build(null));
  876.  
  877. public static void register(IEventBus eventBus){
  878. TILE_ENTITIES.register(eventBus);
  879. }
  880. }
  881.  
  882.  
  883.  
Add Comment
Please, Sign In to add comment