Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Here is my entire code
- Tile rendere;
- package com.Triandon.harderHarvest.tileentity;
- import com.Triandon.harderHarvest.HarderHarvest;
- import com.mojang.blaze3d.matrix.MatrixStack;
- import net.minecraft.client.Minecraft;
- import net.minecraft.client.renderer.IRenderTypeBuffer;
- import net.minecraft.client.renderer.model.ItemCameraTransforms;
- import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
- import net.minecraft.client.renderer.tileentity.TileEntityRendererDispatcher;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.Items;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.math.vector.Vector3f;
- public class Campfire_Tile_Renderer extends TileEntityRenderer<Campfire_Tile> {
- private final Minecraft mc = Minecraft.getInstance();
- public Campfire_Tile_Renderer(TileEntityRendererDispatcher rendererDispatcherIn) {
- super(rendererDispatcherIn);
- }
- @Override
- public void render(Campfire_Tile tileEntityIn, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int combinedLightIn, int combinedOverlayIn) {
- NonNullList<ItemStack> nonnulllist = tileEntityIn.getInventory();
- ItemStack itemStack = tileEntityIn.getItem();
- if (!itemStack.isEmpty()) {
- System.out.println("ItemStack in slot 0: " + itemStack.getItem().getRegistryName() + ", Count: " + itemStack.getCount());
- } else {
- System.out.println("ItemStack is empty!");
- HarderHarvest.LOGGER.info(nonnulllist);
- HarderHarvest.LOGGER.info(tileEntityIn.getInventory());
- HarderHarvest.LOGGER.info(itemStack);
- }
- if (itemStack != ItemStack.EMPTY || itemStack.getItem() != Items.AIR || !itemStack.isEmpty()) {
- matrixStackIn.push();
- matrixStackIn.translate(0.5D, 0.5D, 0.5D);
- matrixStackIn.rotate(Vector3f.XP.rotationDegrees(90.0F));
- //matrixStackIn.translate(-0.3125D, -0.3125D, 0.0D);
- matrixStackIn.scale(0.75F, 0.75F, 0.75F);
- Minecraft.getInstance().getItemRenderer().renderItem(itemStack, ItemCameraTransforms.TransformType.GROUND, combinedLightIn, combinedOverlayIn, matrixStackIn, bufferIn);
- matrixStackIn.pop();
- }
- }
- }
- The tile entity (campfrie tile)
- package com.Triandon.harderHarvest.tileentity;
- import com.Triandon.harderHarvest.block.custom.Campfire;
- import net.minecraft.block.BlockState;
- import net.minecraft.entity.item.ItemEntity;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.inventory.IClearable;
- import net.minecraft.inventory.IInventory;
- import net.minecraft.inventory.Inventory;
- import net.minecraft.inventory.ItemStackHelper;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.Items;
- import net.minecraft.item.crafting.CampfireCookingRecipe;
- import net.minecraft.item.crafting.IRecipeType;
- import net.minecraft.item.crafting.RecipeManager;
- import net.minecraft.nbt.CompoundNBT;
- import net.minecraft.network.play.server.SUpdateTileEntityPacket;
- import net.minecraft.tileentity.FurnaceTileEntity;
- import net.minecraft.tileentity.ITickableTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.tileentity.TileEntityType;
- import net.minecraft.util.NonNullList;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.world.server.ServerWorld;
- import net.minecraftforge.common.ForgeHooks;
- import javax.annotation.Nullable;
- import java.util.Map;
- import java.util.Optional;
- import java.util.Random;
- public class Campfire_Tile extends TileEntity implements IInventory, ITickableTileEntity, IClearable {
- public static int slots = 1;
- private int burningTimeTicks;
- protected NonNullList<ItemStack> inventory = NonNullList.withSize(slots,ItemStack.EMPTY);
- protected NonNullList<ItemStack> fuelInventory = NonNullList.withSize(16,ItemStack.EMPTY);
- private int cookingTime;
- private int totalCookingTime;
- public Campfire_Tile(TileEntityType<?> tileEntityTypeIn) {
- super(tileEntityTypeIn);
- this.burningTimeTicks = 1600;
- }
- public Campfire_Tile(){
- this(ModTileEntities.CAMPFIRE_TILE.get());
- }
- public void tick(){
- boolean lit = this.getBlockState().get(Campfire.LIT);
- boolean flag = this.world.isRemote;
- if(!flag){
- if(!inventory.get(0).isEmpty()){
- this.inventoryChanged();
- }
- }
- if(!world.isRemote){
- //HarderHarvest.LOGGER.info(getItem());
- ItemStack stack = inventory.get(0);
- if(!stack.isEmpty()){
- Optional<CampfireCookingRecipe> recipe = findMatchingRecipe(stack);
- BlockState state = getBlockState();
- if(recipe.isPresent() && state.get(Campfire.FIRE_LEVEL) < 4 && !state.get(Campfire.WATERLOGGED) && state.get(Campfire.LIT)){
- cookingTime++;
- if(cookingTime >= totalCookingTime){
- ItemStack cookedItem = recipe.get().getCraftingResult(new Inventory(stack));
- BlockPos pos1 = getPos();
- ItemEntity itemEntity = new ItemEntity(world,pos1.getX(),pos1.getY(),pos1.getZ(),
- cookedItem);
- world.addEntity(itemEntity);
- inventory.set(0,ItemStack.EMPTY);
- markDirty();
- }
- }
- }
- if(burningTimeTicks > 0){
- if(!fuelInventory.isEmpty()){
- removeRandomFuel();
- }
- burningTimeTicks--;
- updateModel(burningTimeTicks);
- }
- else {
- world.setBlockState(getPos(),getBlockState().with(Campfire.LIT,true).with(Campfire.FIRE_LEVEL,5),11);
- markDirty();
- }
- }
- }
- @Nullable
- public Optional<CampfireCookingRecipe> findMatchingRecipe(ItemStack stack){
- if(world instanceof ServerWorld){
- RecipeManager recipeManager = world.getRecipeManager();
- return recipeManager.getRecipe(IRecipeType.CAMPFIRE_COOKING, new Inventory(stack),world);
- }
- return Optional.empty();
- }
- public void setInventory(NonNullList<ItemStack> inventory) {
- this.inventory = inventory;
- }
- @Override
- public int getSizeInventory() {
- return inventory.size();
- }
- @Override
- public boolean isEmpty() {
- return inventory.stream().allMatch(ItemStack::isEmpty);
- }
- @Override
- public ItemStack getStackInSlot(int index) {
- return this.inventory.get(index);
- }
- public ItemStack getItem(){
- return this.inventory.get(0);
- }
- public NonNullList<ItemStack> getInventory() {
- return this.inventory;
- }
- @Override
- public ItemStack decrStackSize(int index, int count) {
- return ItemStackHelper.getAndSplit(inventory,index,count);
- }
- @Override
- public ItemStack removeStackFromSlot(int index) {
- this.inventoryChanged();
- return ItemStackHelper.getAndRemove(inventory,index);
- }
- @Override
- public void setInventorySlotContents(int index, ItemStack stack) {
- inventory.set(index,stack);
- markDirty();
- }
- @Override
- public boolean isUsableByPlayer(PlayerEntity player) {
- return true;
- }
- public void read(BlockState state, CompoundNBT nbt) {
- super.read(state, nbt);
- this.inventory.clear();
- ItemStackHelper.loadAllItems(nbt, this.inventory);
- if (nbt.contains("CookingTimes", 99)) {
- this.cookingTime = nbt.getInt("CookingTimes");
- }
- if (nbt.contains("TotalCookingTime", 99)) {
- this.totalCookingTime = nbt.getInt("TotalCookingTime");
- }
- if(nbt.contains("burningTimeTicks", 99)){
- this.burningTimeTicks = nbt.getInt("burningTimeTicks");
- }
- }
- public CompoundNBT write(CompoundNBT compound) {
- this.writeItems(compound);
- compound.putInt("CookingTimes", this.cookingTime);
- compound.putInt("TotalCookingTime", this.totalCookingTime);
- compound.putInt("burningTimeTicks", this.burningTimeTicks);
- return compound;
- }
- private CompoundNBT writeItems(CompoundNBT compound) {
- super.write(compound);
- ItemStackHelper.saveAllItems(compound, this.inventory, true);
- return compound;
- }
- @Nullable
- @Override
- public SUpdateTileEntityPacket getUpdatePacket() {
- return new SUpdateTileEntityPacket(this.pos,0,this.getUpdateTag());
- }
- @Override
- public CompoundNBT getUpdateTag() {
- CompoundNBT compoundNBT = new CompoundNBT();
- this.write(compoundNBT);
- return compoundNBT;
- }
- @Override
- public void clear() {
- inventory.clear();
- }
- public boolean addItem(ItemStack stack, int cookingTime){
- if(inventory.get(0).isEmpty() || inventory.get(0).equals(Items.AIR)){
- //inventory.add(0,stack.split(1));
- this.totalCookingTime = cookingTime; //Set the tot cooking time
- this.cookingTime = 0; //Reset cooking time
- this.inventory.set(0,stack.split(1));
- this.inventoryChanged();
- markDirty();
- return true;
- }
- return false;
- }
- private void inventoryChanged(){
- this.markDirty();
- this.getWorld().notifyBlockUpdate(this.getPos(),this.getBlockState(),this.getBlockState(),11);
- }
- public NonNullList<ItemStack> getFuelInventory() {
- return fuelInventory;
- }
- public void setFuelInventory(NonNullList<ItemStack> fuelInventory) {
- this.fuelInventory = fuelInventory;
- }
- public int getBurningTimeTicks() {
- return burningTimeTicks;
- }
- public void setBurningTimeTicks(int burningTimeTicks) {
- this.burningTimeTicks = burningTimeTicks;
- markDirty();
- }
- public boolean isCampfireLit(){
- BlockState state = this.getBlockState();
- return state.get(Campfire.LIT);
- }
- public boolean addFuel(ItemStack stack){
- Integer burnTime1 = ForgeHooks.getBurnTime(stack);
- int burnTime3 = 0;
- if(burnTime1 > 0 && burnTime1 != null){
- burnTime3 = burnTime1;
- }
- else {
- Map<Item, Integer> burnTimes = FurnaceTileEntity.getBurnTimes();
- Integer burnTime2 = burnTimes.get(stack.getItem());
- if(burnTime2 != null){
- burnTime3 = burnTime2;
- }
- }
- if(burnTime3 <= 0){
- return false; // Item cannot be used as fue
- }
- for (int i = 0; i < fuelInventory.size(); i++) {
- if(fuelInventory.get(i).isEmpty()){
- fuelInventory.set(i,stack.split(1));
- burningTimeTicks += burnTime3;
- markDirty();
- return true;
- }
- }
- return false; // no space
- }
- public void removeRandomFuel(){
- Random random = new Random();
- int randomIndex = random.nextInt(fuelInventory.size());
- //Check if fuel inventory is not empty
- if(!fuelInventory.get(randomIndex).isEmpty()){
- ItemStack removedItem = fuelInventory.get(randomIndex);
- fuelInventory.set(randomIndex,ItemStack.EMPTY);
- markDirty();
- }
- }
- public void updateModel(int burningTimeTicks){
- boolean litState = isCampfireLit();
- BlockState currentState = getBlockState();
- BlockState newState = currentState;
- if(litState){
- if(burningTimeTicks <= 0){
- newState = currentState.with(Campfire.FIRE_LEVEL,5);
- }else if(burningTimeTicks < 600){
- newState = currentState.with(Campfire.FIRE_LEVEL,4);
- }else if (burningTimeTicks < 1600){
- newState = currentState.with(Campfire.FIRE_LEVEL,1);
- }else if (burningTimeTicks < 3200){
- newState = currentState.with(Campfire.FIRE_LEVEL,2);
- }else if (burningTimeTicks < 6400){
- newState = currentState.with(Campfire.FIRE_LEVEL,3);
- }else {
- newState = currentState.with(Campfire.FIRE_LEVEL,3);
- }
- }
- else {
- newState = currentState.with(Campfire.FIRE_LEVEL,0);
- }
- //Only update if states dont match
- if(currentState.get(Campfire.FIRE_LEVEL) != newState.get(Campfire.FIRE_LEVEL)){
- world.setBlockState(pos,newState,11);
- }
- markDirty();
- }
- }
- The block (campfireblock)
- package com.Triandon.harderHarvest.block.custom;
- import com.Triandon.harderHarvest.HarderHarvest;
- import com.Triandon.harderHarvest.tileentity.Campfire_Tile;
- import com.Triandon.harderHarvest.tileentity.ModTileEntities;
- import net.minecraft.block.*;
- import net.minecraft.entity.Entity;
- import net.minecraft.entity.LivingEntity;
- import net.minecraft.entity.player.PlayerEntity;
- import net.minecraft.entity.player.ServerPlayerEntity;
- import net.minecraft.entity.projectile.ProjectileEntity;
- import net.minecraft.fluid.FluidState;
- import net.minecraft.fluid.Fluids;
- import net.minecraft.item.BlockItemUseContext;
- import net.minecraft.item.FlintAndSteelItem;
- import net.minecraft.item.Item;
- import net.minecraft.item.ItemStack;
- import net.minecraft.item.crafting.CampfireCookingRecipe;
- import net.minecraft.network.play.server.SPlaySoundPacket;
- import net.minecraft.particles.ParticleTypes;
- import net.minecraft.state.BooleanProperty;
- import net.minecraft.state.DirectionProperty;
- import net.minecraft.state.IntegerProperty;
- import net.minecraft.state.StateContainer;
- import net.minecraft.state.properties.BlockStateProperties;
- import net.minecraft.stats.Stats;
- import net.minecraft.tags.BlockTags;
- import net.minecraft.tileentity.FurnaceTileEntity;
- import net.minecraft.tileentity.TileEntity;
- import net.minecraft.util.*;
- import net.minecraft.util.math.BlockPos;
- import net.minecraft.util.math.BlockRayTraceResult;
- import net.minecraft.util.math.shapes.ISelectionContext;
- import net.minecraft.util.math.shapes.VoxelShape;
- import net.minecraft.util.math.vector.Vector3d;
- import net.minecraft.world.IBlockReader;
- import net.minecraft.world.IWorld;
- import net.minecraft.world.World;
- import net.minecraft.world.server.ServerWorld;
- import net.minecraftforge.api.distmarker.Dist;
- import net.minecraftforge.api.distmarker.OnlyIn;
- import net.minecraftforge.common.ForgeHooks;
- import javax.annotation.Nullable;
- import java.util.Map;
- import java.util.Optional;
- import java.util.Random;
- public class Campfire extends ContainerBlock implements IWaterLoggable {
- private static final int LIGHT_LEVEL = 15; //Max light level
- protected static final VoxelShape SHAPE = Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 7.0D, 16.0D);
- public static final BooleanProperty LIT = BlockStateProperties.LIT;
- public static final BooleanProperty STICK = BooleanProperty.create("stick");
- public static final IntegerProperty FIRE_LEVEL = IntegerProperty.create("fire_level",0,5);
- public static final DirectionProperty HORIZONTAL_FACING = BlockStateProperties.HORIZONTAL_FACING;
- public static final BooleanProperty WATERLOGGED = BlockStateProperties.WATERLOGGED;
- private final boolean smokey;
- private final int fireDamage;
- public Campfire(boolean smokey,int firedDamage,Properties builder) {
- super(builder);
- this.smokey = smokey;
- this.fireDamage = firedDamage;
- 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)));
- }
- @Override
- protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
- builder.add(HORIZONTAL_FACING,LIT, STICK, FIRE_LEVEL, WATERLOGGED);
- }
- @Nullable
- @Override
- public BlockState getStateForPlacement(BlockItemUseContext context) {
- IWorld iWorld = context.getWorld();
- BlockPos blockPos = context.getPos();
- boolean flag = iWorld.getFluidState(blockPos).getFluid() == Fluids.WATER;
- return this.getDefaultState().with(WATERLOGGED,Boolean.valueOf(flag)).with(LIT,Boolean.valueOf(!flag)).with(HORIZONTAL_FACING, context.getPlacementHorizontalFacing().getOpposite());
- }
- @Override
- public void onBlockPlacedBy(World worldIn, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
- super.onBlockPlacedBy(worldIn, pos, state, placer, stack);
- worldIn.setBlockState(pos,state.with(Campfire.LIT,false),3);
- }
- @Nullable
- @Override
- public TileEntity createTileEntity(BlockState state, IBlockReader world) {
- return ModTileEntities.CAMPFIRE_TILE.get().create();
- }
- @Override
- public boolean hasTileEntity(BlockState state) {
- return true;
- }
- @Override
- public BlockRenderType getRenderType(BlockState state) {
- return BlockRenderType.MODEL;
- }
- @Override
- public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
- return SHAPE;
- }
- @Override
- public void onBlockAdded(BlockState state, World worldIn, BlockPos pos, BlockState oldState, boolean isMoving) {
- super.onBlockAdded(state, worldIn, pos, oldState, isMoving);
- TileEntity tileEntity = worldIn.getTileEntity(pos);
- if(tileEntity instanceof Campfire_Tile){
- tileEntity.markDirty();
- }
- }
- @SuppressWarnings("deprecation")
- @Override
- public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
- ItemStack heldItem = player.getHeldItem(handIn);
- TileEntity tileEntity = worldIn.getTileEntity(pos);
- Random random = new Random();
- if(tileEntity instanceof Campfire_Tile){
- Campfire_Tile campfireTileEntity = (Campfire_Tile) tileEntity;
- HarderHarvest.LOGGER.info(campfireTileEntity.getInventory());
- HarderHarvest.LOGGER.info(campfireTileEntity.getInventory().get(0));
- campfireTileEntity.markDirty();
- worldIn.notifyBlockUpdate(pos,state,state,3);
- // Check if the player is trying to retrieve the cooked item
- if(heldItem.isEmpty() && !campfireTileEntity.getStackInSlot(0).isEmpty()){
- ItemStack cookedItem = campfireTileEntity.removeStackFromSlot(0);
- player.addItemStackToInventory(cookedItem);
- return ActionResultType.SUCCESS;
- }
- // Logic for adding food items
- Optional<CampfireCookingRecipe> recipeOptional = campfireTileEntity.findMatchingRecipe(heldItem);
- if(recipeOptional.isPresent()){
- HarderHarvest.LOGGER.info("Recipe fount for:"+ heldItem.getItem().getRegistryName());
- if (!worldIn.isRemote && campfireTileEntity.addItem(heldItem, recipeOptional.get().getCookTime())) {
- player.addStat(Stats.INTERACT_WITH_CAMPFIRE);
- campfireTileEntity.markDirty();
- worldIn.notifyBlockUpdate(pos,state,state,3);
- //campfireTileEntity.addItem(heldItem,recipeOptional.get().getCookTime());
- //campfireTileEntity.setItemInInventory(heldItem);
- return ActionResultType.SUCCESS;
- }else {
- return ActionResultType.PASS;
- }
- }
- else {
- HarderHarvest.LOGGER.info("No recipes found!!!");
- }
- //Adding fuel logic
- if(!worldIn.isRemote && state.get(FIRE_LEVEL) != 5){
- Integer burnTime = ForgeHooks.getBurnTime(heldItem,null);
- if(burnTime == null || burnTime <= 0){
- Map<Item, Integer> burnTimes = FurnaceTileEntity.getBurnTimes();
- burnTime = burnTimes.get(heldItem.getItem());
- }
- HarderHarvest.LOGGER.info(burnTime+ " Burn time");
- if(burnTime != null && burnTime > 0){
- if(campfireTileEntity.addFuel(heldItem)){
- if(state.get(FIRE_LEVEL) == 4){
- if(worldIn.rand.nextInt(100) < 30){
- worldIn.playSound(player,pos,SoundEvents.ITEM_FLINTANDSTEEL_USE,SoundCategory.BLOCKS,1,random.nextFloat() * 0.4F + 0.8F);
- ((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));
- worldIn.playSound(player,pos,SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM,SoundCategory.BLOCKS,0.5F,random.nextFloat() * 0.4F + 0.8F);
- ((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));
- return ActionResultType.SUCCESS;
- }
- else {
- worldIn.playSound(player,pos,SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM,SoundCategory.BLOCKS,1,random.nextFloat() * 0.4F + 0.8F);
- ((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));
- return ActionResultType.SUCCESS;
- }
- }
- else {
- worldIn.playSound(player,pos,SoundEvents.ENTITY_ITEM_FRAME_ADD_ITEM,SoundCategory.BLOCKS,1,random.nextFloat() * 0.4F + 0.8F);
- ((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));
- return ActionResultType.SUCCESS;
- }
- }
- else {
- HarderHarvest.LOGGER.info("Failed to add fuel");
- }
- }
- }
- }
- //other logic such as flint and steel
- if(heldItem.getItem() instanceof FlintAndSteelItem){
- Campfire_Tile campfireTileEntity = (Campfire_Tile) tileEntity;
- if(!state.get(LIT) && !state.get(WATERLOGGED)){
- worldIn.playSound(player, pos, SoundEvents.ITEM_FLINTANDSTEEL_USE, SoundCategory.BLOCKS, 1.0F, random.nextFloat() * 0.4F + 0.8F);
- worldIn.setBlockState(pos,state.with(Campfire.LIT,true).with(FIRE_LEVEL,1),11);
- if(heldItem.getItem() instanceof FlintAndSteelItem){
- heldItem.damageItem(1,player,player1->
- player1.sendBreakAnimation(handIn));
- worldIn.playSound(player, pos, SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE, SoundCategory.BLOCKS, 1.0F, random.nextFloat() * 0.4F + 0.8F);
- }
- }
- return ActionResultType.SUCCESS;
- }
- return ActionResultType.PASS;
- }
- //Maybe delete later no usage
- public static boolean canBeLit(BlockState state) {
- return state.isInAndMatches(BlockTags.CAMPFIRES, (stateIn) -> {
- return stateIn.hasProperty(BlockStateProperties.WATERLOGGED) && stateIn.hasProperty(BlockStateProperties.LIT);
- }) && !state.get(BlockStateProperties.WATERLOGGED) && !state.get(BlockStateProperties.LIT);
- }
- @Override
- public int getLightValue(BlockState state, IBlockReader world, BlockPos pos) {
- if(state.get(Campfire.LIT) && state.get(FIRE_LEVEL) < 4){ //Emit light only if lit and not FireLevel [4,5]
- return LIGHT_LEVEL;
- }
- return 0; //No light
- }
- @Override
- public boolean receiveFluid(IWorld worldIn, BlockPos pos, BlockState state, FluidState fluidStateIn) {
- if(!state.get(BlockStateProperties.WATERLOGGED) && fluidStateIn.getFluid() == Fluids.WATER){
- boolean wasLit = state.get(LIT);
- if(wasLit){
- if(!worldIn.isRemote()){
- worldIn.playSound((PlayerEntity)null, pos, SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE, SoundCategory.BLOCKS, 1.0F, 1.0F);
- }
- //extinguish
- }
- int currentFireLevel = state.get(FIRE_LEVEL);
- if(currentFireLevel != 5){
- worldIn.setBlockState(pos,state.with(WATERLOGGED,Boolean.valueOf(true)).with(LIT,Boolean.valueOf(false)).with(FIRE_LEVEL,0),11);
- }else {
- worldIn.setBlockState(pos,state.with(WATERLOGGED,Boolean.valueOf(true)).with(LIT,Boolean.valueOf(true)).with(FIRE_LEVEL,5),11);
- }
- worldIn.getPendingFluidTicks().scheduleTick(pos,fluidStateIn.getFluid(),fluidStateIn.getFluid().getTickRate(worldIn));
- return true;
- }else {
- return false;
- }
- }
- @Override
- public void fillWithRain(World worldIn, BlockPos pos) {
- super.fillWithRain(worldIn, pos);
- BlockState state = worldIn.getBlockState(pos);
- if(worldIn.isRainingAt(pos.up())){
- if(state.get(Campfire.LIT)){
- worldIn.playSound(null,pos, SoundEvents.ENTITY_GENERIC_EXTINGUISH_FIRE, SoundCategory.BLOCKS,1.0F,1.0F);
- //extinguish(worldIn,pos,state);
- worldIn.setBlockState(pos,state.with(Campfire.LIT,false),11);
- }
- }
- }
- @Override
- public void onProjectileCollision(World worldIn, BlockState state, BlockRayTraceResult hit, ProjectileEntity projectile) {
- if(!worldIn.isRemote && projectile.isBurning()){
- Entity entity = projectile.getShooter();
- boolean flag = entity == null || entity instanceof PlayerEntity || net.minecraftforge.event.ForgeEventFactory.getMobGriefingEvent(worldIn, entity);
- if(flag && !state.get(LIT) && !state.get(WATERLOGGED)){
- BlockPos blockPos = hit.getPos();
- worldIn.setBlockState(blockPos,state.with(BlockStateProperties.LIT,Boolean.valueOf(true)),11);
- }
- }
- }
- @OnlyIn(Dist.CLIENT)
- public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
- if (stateIn.get(LIT) && stateIn.get(FIRE_LEVEL) < 4) {
- if (rand.nextInt(10) == 0) {
- 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);
- }
- if (this.smokey && rand.nextInt(5) == 0) {
- for(int i = 0; i < rand.nextInt(1) + 1; ++i) {
- 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));
- }
- }
- }
- }
- @Nullable
- @Override
- public TileEntity createNewTileEntity(IBlockReader worldIn) {
- return new Campfire_Tile();
- }
- @Override
- public void randomTick(BlockState state, ServerWorld worldIn, BlockPos pos, Random random) {
- super.randomTick(state, worldIn, pos, random);
- TileEntity tileEntity = worldIn.getTileEntity(pos);
- if(tileEntity instanceof Campfire_Tile){
- Campfire_Tile campfireTile = (Campfire_Tile) tileEntity;
- campfireTile.tick();
- }
- }
- @Override
- public boolean ticksRandomly(BlockState state) {
- return true;
- }
- @Override
- public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
- if(stateIn.get(WATERLOGGED)){
- worldIn.getPendingFluidTicks().scheduleTick(currentPos,Fluids.WATER,Fluids.WATER.getTickRate(worldIn));
- }
- return super.updatePostPlacement(stateIn,facing,facingState,worldIn,currentPos,facingPos);
- }
- @Override
- public FluidState getFluidState(BlockState state) {
- return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : Fluids.EMPTY.getDefaultState();
- }
- }
- Other code;
- package com.Triandon.harderHarvest;
- import com.Triandon.harderHarvest.block.ModBlocks;
- import com.Triandon.harderHarvest.core.config.HarderHarvestConfig;
- import com.Triandon.harderHarvest.events.woodEvent.WoodenByProduct;
- import com.Triandon.harderHarvest.items.ModItems;
- import com.Triandon.harderHarvest.tileentity.Campfire_Tile_Renderer;
- import com.Triandon.harderHarvest.tileentity.ModTileEntities;
- import net.minecraft.block.Block;
- import net.minecraft.block.Blocks;
- import net.minecraft.client.renderer.RenderType;
- import net.minecraft.client.renderer.RenderTypeLookup;
- import net.minecraftforge.common.MinecraftForge;
- import net.minecraftforge.event.RegistryEvent;
- import net.minecraftforge.eventbus.api.IEventBus;
- import net.minecraftforge.eventbus.api.SubscribeEvent;
- import net.minecraftforge.fml.InterModComms;
- import net.minecraftforge.fml.ModList;
- import net.minecraftforge.fml.ModLoadingContext;
- import net.minecraftforge.fml.client.registry.ClientRegistry;
- import net.minecraftforge.fml.common.Mod;
- import net.minecraftforge.fml.config.ModConfig;
- import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
- import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
- import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
- import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
- import net.minecraftforge.fml.event.server.FMLServerStartingEvent;
- import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
- import org.apache.logging.log4j.LogManager;
- import org.apache.logging.log4j.Logger;
- // The value here should match an entry in the META-INF/mods.toml file
- @Mod(HarderHarvest.MOD_ID)
- public class HarderHarvest
- {
- public static final String MOD_ID = "harderharvest";
- // Directly reference a log4j logger.
- public static final Logger LOGGER = LogManager.getLogger();
- public HarderHarvest() {
- IEventBus eventBus = FMLJavaModLoadingContext.get().getModEventBus();
- ModItems.register(eventBus);
- if(ModList.get().isLoaded("harderstones")){
- ModItems.register_HARDERSTONES(eventBus);
- }
- ModBlocks.register(eventBus);
- ModTileEntities.register(eventBus);
- ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, HarderHarvestConfig.SPEC,"harderharvest-common.toml");
- //ModRecipeTypes.register(eventBus);
- // Register the setup method for modloading
- eventBus.addListener(this::setup);
- // Register the enqueueIMC method for modloading
- eventBus.addListener(this::enqueueIMC);
- // Register the processIMC method for modloading
- eventBus.addListener(this::processIMC);
- // Register the doClientStuff method for modloading
- eventBus.addListener(this::doClientStuff);
- // Register ourselves for server and other game events we are interested in
- MinecraftForge.EVENT_BUS.register(this);
- }
- private void setup(final FMLCommonSetupEvent event)
- {
- WoodenByProduct.registerStrippableBlocks();
- // some preinit code
- LOGGER.info("HELLO FROM PREINIT");
- LOGGER.info("DIRT BLOCK >> {}", Blocks.DIRT.getRegistryName());
- }
- private void doClientStuff(final FMLClientSetupEvent event) {
- // do something that can only be done on the client
- event.enqueueWork(()->{
- RenderTypeLookup.setRenderLayer(ModBlocks.FIREWOOD_BLOCK.get(), RenderType.getCutout());
- RenderTypeLookup.setRenderLayer(ModBlocks.CAMPFIRE_BLOCK.get(), RenderType.getCutout());
- ClientRegistry.bindTileEntityRenderer(ModTileEntities.CAMPFIRE_TILE.get(), Campfire_Tile_Renderer::new);
- });
- }
- private void enqueueIMC(final InterModEnqueueEvent event)
- {
- // some example code to dispatch IMC to another mod
- InterModComms.sendTo("examplemod", "helloworld", () -> { LOGGER.info("Hello world from the MDK"); return "Hello world";});
- }
- private void processIMC(final InterModProcessEvent event)
- {
- // some example code to receive and process InterModComms from other mods
- }
- // You can use SubscribeEvent and let the Event Bus discover methods to call
- @SubscribeEvent
- public void onServerStarting(FMLServerStartingEvent event) {
- // do something when the server starts
- LOGGER.info("HELLO from server starting");
- }
- // You can use EventBusSubscriber to automatically subscribe events on the contained class (this is subscribing to the MOD
- // Event bus for receiving Registry Events)
- @Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
- public static class RegistryEvents {
- @SubscribeEvent
- public static void onBlocksRegistry(final RegistryEvent.Register<Block> blockRegistryEvent) {
- // register a new block here
- LOGGER.info("HELLO from Register Block");
- }
- }
- }
- package com.Triandon.harderHarvest.tileentity;
- import com.Triandon.harderHarvest.HarderHarvest;
- import com.Triandon.harderHarvest.block.ModBlocks;
- import net.minecraft.tileentity.TileEntityType;
- import net.minecraftforge.eventbus.api.IEventBus;
- import net.minecraftforge.fml.RegistryObject;
- import net.minecraftforge.registries.DeferredRegister;
- import net.minecraftforge.registries.ForgeRegistries;
- public class ModTileEntities {
- public static DeferredRegister<TileEntityType<?>> TILE_ENTITIES =
- DeferredRegister.create(ForgeRegistries.TILE_ENTITIES, HarderHarvest.MOD_ID);
- public static RegistryObject<TileEntityType<Firewood_Tile>> FIREWOOD_TILE =
- TILE_ENTITIES.register("firewood_tile",()->TileEntityType.Builder.create(
- Firewood_Tile::new, ModBlocks.FIREWOOD_BLOCK.get()).build(null));
- public static RegistryObject<TileEntityType<Campfire_Tile>> CAMPFIRE_TILE =
- TILE_ENTITIES.register("campfire_tile",()->TileEntityType.Builder.create(
- Campfire_Tile::new, ModBlocks.CAMPFIRE_BLOCK.get()).build(null));
- public static void register(IEventBus eventBus){
- TILE_ENTITIES.register(eventBus);
- }
- }
Add Comment
Please, Sign In to add comment