Jump to content

Recommended Posts

Posted

I have a block that checks the nbt of an item in it to display upgrades, when the item is in the slot the game crashes. How would I go about to not tick the block entity so the game doesn't crash?

 

	public static class Fireproof {
		
		static int index;
		static Boolean EditMode;
		public static Boolean editThis;
		static ItemStack item;
		static CompoundNBT nbtTagCompound;
		static NonNullList<ItemStack> inventory;
		
		public static Item upgrade = MIItems.upgrade_card_fireproof;
		public static String name = "Fireproof";
		
		public static void init() {
			inventory = DataEncoderTileEntity.getInstance().getItems();
			index = SlotFireproof.index;
			EditMode = DataEncoderTileEntity.getInstance().getEdit();
			item = inventory.get(0);
			if(!EditMode) nbtTagCompound = item.getTag();
			if(EditMode) editThis = false;
				

			
			if(!EditMode && nbtTagCompound.contains(name)) {
				
				editThis = true;
				run();
								
			} else {
				
				editThis = false;
			}
		}
		
		static void run() {
			
			
			DataEncoderTileEntity.getInstance().setInventorySlotContents(index, item);
			if(!DataEncoderTileEntity.getInstance().getStackInSlot(index).isEmpty()) {
				
				nbtTagCompound.putBoolean(name, true);
			} else {
				
				nbtTagCompound.putBoolean(name, false);
			}
			
		}
	}

 

~ Ronaldi2001

Posted

Why is all of this shit static? Where's your actual block class? Your tile entity class?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

The code above im planning on moving it into another class so each upgrade can be modular. This is the code of the tile entity:

 

public class DataEncoderTileEntity  extends LockableLootTileEntity  implements ITickableTileEntity, INamedContainerProvider{
	
	public static final int SLOTS = 13;
	private static final DataEncoderTileEntity INSTANCE = new DataEncoderTileEntity();
	public NonNullList<ItemStack> inventory;
	public boolean EditMode = false;	
	CompoundNBT nbtTagCompound;
	
	public DataEncoderTileEntity() {
		super(MITileEntities.DATAENCODER_TILEENTITY);
		
		this.inventory = NonNullList.withSize(SLOTS, ItemStack.EMPTY);
	}
	
	public static DataEncoderTileEntity getInstance() {
		
		return INSTANCE;
	}
	
	@Override
    public void tick() {

		if(getStackInSlot(0).isEmpty()) EditMode = true; else EditMode = false;
		if(!EditMode) nbtTagCompound = getStackInSlot(0).getTag();
		if(!EditMode && getItem(getStackInSlot(0)) && nbtTagCompound.size() == isEnchanted()) addNBT(getStackInSlot(0));
		if(!EditMode) if(getItem(getStackInSlot(0))) init();
		if(EditMode) clear();
	}
	
	public static void init() {
		
		Fireproof.init();
	}
	
	@Mod.EventBusSubscriber(bus=Mod.EventBusSubscriber.Bus.MOD)
	public static class Fireproof {
		
		static int index;
		static Boolean EditMode;
		public static Boolean editThis;
		static ItemStack item;
		static CompoundNBT nbtTagCompound;
		static NonNullList<ItemStack> inventory;
		
		public static Item upgrade = MIItems.upgrade_card_fireproof;
		public static String name = "Fireproof";
		
		public static void init() {
			inventory = DataEncoderTileEntity.getInstance().getItems();
			index = SlotFireproof.index;
			EditMode = DataEncoderTileEntity.getInstance().getEdit();
			item = inventory.get(0);
			if(!EditMode) nbtTagCompound = item.getTag();
			if(EditMode) editThis = false;
				

			
			if(!EditMode && nbtTagCompound.contains(name)) {
				
				editThis = true;
				run();
								
			} else {
				
				editThis = false;
			}
		}
		
		static void run() {
			
			
			DataEncoderTileEntity.getInstance().setInventorySlotContents(index, item);
			if(!DataEncoderTileEntity.getInstance().getStackInSlot(index).isEmpty()) {
				
				nbtTagCompound.putBoolean(name, true);
			} else {
				
				nbtTagCompound.putBoolean(name, false);
			}
			
		}
	}
	
	void addNBT(ItemStack stack) {
		
		if(stack.getItem() == MIItems.ultimate_boots) {
			
			nbtTagCompound.putBoolean(Fireproof.name, false);
		}
	}
	
	public Boolean getItem(ItemStack stack) {
		
		if(stack.getItem() == MIItems.ultimate_boots) return true;
		else return false;
		
	}
	
	public int isEnchanted() {
		
		Boolean ench = getStackInSlot(0).isEnchanted();
		
		if(ench) return 2; else return 1;
	}
	
	@Override
    public void read(CompoundNBT compound) {
        super.read(compound);
        
        this.loadFromNbt(compound);
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        super.write(compound);
        
        return this.saveToNbt(compound);
    }

    public void loadFromNbt(CompoundNBT compound) {
    	
        this.inventory = NonNullList.withSize(this.getSizeInventory(), ItemStack.EMPTY);

        if (!this.checkLootAndRead(compound) && compound.contains("Items", 9)) {
        	
            ItemStackHelper.loadAllItems(compound, this.inventory);
        }
    }

    public CompoundNBT saveToNbt(CompoundNBT compound) {
    	
        if (!this.checkLootAndWrite(compound)) {
        	
            ItemStackHelper.saveAllItems(compound, this.inventory, false);
        }

        return compound;
    }
	
	public int getSizeInventory() {
		
		return inventory.size();
	}

	public boolean isEmpty() {
		
		return inventory.isEmpty();
	}

	public ItemStack getStackInSlot(int index) {
		
        return inventory.get(index);
    }

	public ItemStack decrStackSize(int index, int count) {
		
		ItemStack itemStack = getStackInSlot(index);
        if (!itemStack.isEmpty()) {
            if (itemStack.getCount() <= count) {
                setInventorySlotContents(index, ItemStack.EMPTY);
            } else {
                itemStack = itemStack.split(count);
                if (itemStack.getCount() == 0) {
                    setInventorySlotContents(index, ItemStack.EMPTY);
                }
            }
        }

        return itemStack;
	}

	public ItemStack removeStackFromSlot(int index) {
		
		return getStackInSlot(index);
	}
	
	public void setInventorySlotContents(int index, ItemStack stack) {
		
		inventory.set(index, stack);
 	}
	
	public void clear() {
		
		for(int i = 1; i < SLOTS; i++) {
			
			setInventorySlotContents(i, ItemStack.EMPTY);
		}
	}

	@Override
	protected NonNullList<ItemStack> getItems() {
		
		return this.inventory;
	}

	@Override
	protected void setItems(NonNullList<ItemStack> itemsIn) {
		
		this.inventory = itemsIn;
	}

	@Override
	protected ITextComponent getDefaultName() {
		
		return new StringTextComponent("Data Encoder");
	}

	@Override
	protected Container createMenu(int id, PlayerInventory player) {
		
		return new DataEncoderContainer(id, player, this);
	}
	
	public Boolean getEdit() {
		
		return EditMode;
	}
}

 

And the code for the block:

 

public class DataEncoderBlock extends Block {
	
	public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
	
	public DataEncoderBlock(String name) {
		super(Properties.create(Material.IRON)
				.sound(SoundType.METAL)
				.hardnessAndResistance(2.0F)
				);
		
		this.setDefaultState(this.stateContainer.getBaseState().with(FACING, Direction.NORTH));
		setRegistryName(name);
	}
	
	public BlockState getStateForPlacement(BlockItemUseContext context) {
		
	      return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite());
	   }
		
		
	@Override
	public boolean hasTileEntity(BlockState state) {
		
		return true;
	}

	@Nullable
	@Override
	public TileEntity createTileEntity(BlockState state, IBlockReader world) {
		
		return new DataEncoderTileEntity();
	}
	
	 public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {
	    if (!world.isRemote) {
	    	TileEntity tileEntity = world.getTileEntity(pos);
		    if (tileEntity instanceof INamedContainerProvider) {
		    	
		    	NetworkHooks.openGui((ServerPlayerEntity) player, (INamedContainerProvider) tileEntity, tileEntity.getPos());
	      }

	    }
	    return ActionResultType.SUCCESS;
	  }
		 
 
		  
	public static Direction getFacingFromEntity(BlockPos clickedBlock, LivingEntity entity) {
		
		return Direction.getFacingFromVector((float) (entity.prevPosX - clickedBlock.getX()), (float) (entity.prevPosY - clickedBlock.getY()), (float) (entity.prevPosZ - clickedBlock.getZ()));
	}

	@Override
	protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
		
		builder.add(FACING);
	}
}

 

~ Ronaldi2001

Posted
59 minutes ago, Ronaldi2001 said:

private static final DataEncoderTileEntity INSTANCE = new DataEncoderTileEntity();

This makes no sense.

That's what this is for:

59 minutes ago, Ronaldi2001 said:

    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        
        return new DataEncoderTileEntity();
    }

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

Howdy

 

I'm not sure what your mod is intended to do.  Your block interacts with some sort of item (an item that it contains?) to read NBT from the item and display it?  Something like a vanilla Anvil?

 

You might find this example tutorial project useful; it has some working examples of tile entities (mbe20), reading/writing NBT for items (mbe12), and containers/tileentity (mbe30)

https://github.com/TheGreyGhost/MinecraftByExample

 

-TGG

Posted (edited)
1 hour ago, Ronaldi2001 said:

So with that how would I access everything in the TileEntity class? Also would that fix the crash?

  

Do either are:

a) already in it, just do whatever you want

b) in a place where you have access to a World object and BlockPos location, so you can call World#getTileEntity(pos) and have access to your TileEntity. Remember, there is more than one copy of your TE in the world.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
25 minutes ago, Ronaldi2001 said:

Yes I do program my own mod. I was wondering if you could help with the problem I asked

Cool you replied to a version of my post that doesn't exist any more because I thought I was in another thread and posted the wrong thing.

image.thumb.png.0ec738b53a8d1756f5e41f84371a8854.png

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.