Jump to content

Recommended Posts

Posted (edited)

Here is my TileEntity class:

import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IStringSerializable;

import java.util.Arrays;

public class MasherBowlTileEntity extends TileEntity {

    public enum FoodType implements IStringSerializable {
        apple,
        baked_potato,
        beetroot,
        bread,
        carrot,
        chorus_fruit,
        dried_kelp,
        kelp,
        honey_bottle,
        melon_slice,
        potato,
        poisonous_potato,
        pumpkin,
        sweet_berries,
        nothing;

        @Override
        public String getName() {
            return this.name();
        }
    }

    private FoodType[] ingredients = new FoodType[4];

    public MasherBowlTileEntity(){
        super(TileEntitiesList.masher_bowl);
        clearFoods();
    }

    @Override
    public void read(CompoundNBT compound) {
        super.read(compound);
        int [] arr = compound.getIntArray("ingredients");
        System.out.print("reading ingredients: "+ arr[0] + "," + arr[1]+ "," + arr[2]+ "," + arr[3]+ "\n");
        for(int i = 0; i < arr.length; i++){
            ingredients[i] = FoodType.values()[arr[i]];
        }
    }

    private int[] foodToIntArray(FoodType[] arr){
        int[] result = new int[arr.length];
        for(int i = 0; i < arr.length; i++){
            result[i] = arr[i].ordinal();
        }
        return result;
    }

    @Override
    public CompoundNBT write(CompoundNBT compound) {
        super.write(compound);
        int [] arr = foodToIntArray(ingredients);
        compound.putIntArray("ingredients", arr);
        System.out.print("writing ingredients: "+ arr[0] + "," + arr[1]+ "," + arr[2]+ "," + arr[3]+ "\n");
        return compound;
    }

    public boolean addFood(FoodType food){
        for(int i = 0; i < ingredients.length; i++){
            if(ingredients[i] == FoodType.nothing){
                markDirty();
                ingredients[i] = food;
                return true;
            }
        }
        return false;
    }

    public void clearFoods(){
        markDirty();
        Arrays.fill(ingredients, FoodType.nothing);
    }

    public FoodType getIngredient(int index) {
        return ingredients[index];
    }
}

 

I'm having 2 issues:

 

1. I can only get write() to be called by pausing the game, despite me calling markDirty() whenever data is updated.

 

2. Even if write() was called beforehand, the array gotten in read() is of length 0, which I assume means it couldn't find an NBT matching the key. I know that the array write() is attempting to write has the correct information because the System.out.print call prints the correct information, and the key is correct.

 

Edited by Jipthechip
Posted (edited)
7 hours ago, diesieben07 said:

That is normal. If the game were to save the chunk to disk every time a tile entity said "my data changed" then it would have to save to disk whenever you moved something around in a chest. That is not feasible. markDirty simply tells the game "next time you get around to saving, I need saving".

Oh ok, that makes sense. I was just concerned because I noticed that it wasn't calling my write function before the chunk was unloaded, and then I'd reload the chunk and my data would be gone. But now I think this is simply due to the fact that my write function isn't properly writing data to the NBT, or I'm not properly reading somehow.

7 hours ago, diesieben07 said:

When overriding TileEntity#write you must call super first and put your data in the resulting CompoundNBT. Otherwise your tile entity will not save its ID and position and cannot be loaded back in.

I added the super call to my read and write and the problem persists. Looking at some other TileEntities, I assume you need to add the super call to TileEntity#read as well? I updated my code at the top.

7 hours ago, diesieben07 said:

Also I would recommend saving your enum by name instead of by ordinal, because if you use the ordinal that means you can never change the order of entries in that enum.

I could just remember to add new entries to the end, but if I'm gonna be adding a lot, which I may have to if I want to integrate with other mods, that might get messy. But it may be worth it because it's reading/writing less data.

Edited by Jipthechip
Posted
2 hours ago, diesieben07 said:

Show your block class and where you register your TileEntityType.

My registration class:

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class RegisterBlocks {
    @SubscribeEvent
    public static void registerBlocks(final RegistryEvent.Register<Block> event){
        event.getRegistry().registerAll(
            BlocksList.masher_bowl = new MasherBowlBlock(Block.Properties.create(Material.IRON))
            .setRegistryName(new ResourceLocation(MOD_ID, "masher_bowl"))
    }
    @SubscribeEvent
    public static void registerTE(RegistryEvent.Register<TileEntityType<?>> event) {

        event.getRegistry().registerAll(
                TileEntitiesList.masher_bowl =
                TileEntityType.Builder.create(MasherBowlTileEntity::new, BlocksList.masher_bowl).build(null)
                .setRegistryName(MOD_ID, "masher_bowl")
        );
    }
}

My Block class:

public class MasherBowlBlock extends Block {

    public static final DirectionProperty FACING = HorizontalBlock.HORIZONTAL_FACING;
    public static final BooleanProperty CONTAINS_ROD = BooleanProperty.create("contains_rod");
    public static final DirectionProperty ROD_FACING = DirectionProperty.create("rod_facing", Direction.NORTH, Direction.SOUTH, Direction.EAST, Direction.WEST);
    public static final BooleanProperty CONTAINS_WATER = BooleanProperty.create("contains_water");
    public static final IntegerProperty STIR_PROGRESS = IntegerProperty.create("stir_progress", 0,10);


    public MasherBowlBlock(Properties properties) {
        super(properties.notSolid());
        this.setDefaultState(this.getStateContainer().getBaseState()
                .with(FACING, Direction.NORTH)
                .with(CONTAINS_ROD, false)
                .with(ROD_FACING, Direction.NORTH)
                .with(CONTAINS_WATER, false)
                .with(STIR_PROGRESS, 0)
        );
    }

    @Override
    public BlockState getStateForPlacement(BlockItemUseContext context){

        return this.getDefaultState()
                .with(FACING, context.getPlacementHorizontalFacing().getOpposite())
                .with(CONTAINS_ROD, false)
                .with(ROD_FACING, context.getPlacementHorizontalFacing().getOpposite())
                .with(CONTAINS_WATER, false)
                .with(STIR_PROGRESS, 0);
    }

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

    @Override
    public boolean hasTileEntity(BlockState state) {
        return true;
    }

    @Nullable
    @Override
    public TileEntity createTileEntity(BlockState state, IBlockReader world) {
        return new MasherBowlTileEntity();
    }

    @Override
    public BlockState rotate(BlockState state, Rotation rot) {
        return state.with(FACING, rot.rotate(state.get(FACING)));
    }

    @Override
    public BlockState mirror(BlockState state, Mirror mirrorIn) {
        return state.rotate(mirrorIn.toRotation(state.get(FACING)));
    }

    @Override
    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit) {

        ItemStack heldItem = player.getHeldItem(handIn);
        String itemRegistryName = Objects.requireNonNull(heldItem.getItem().getRegistryName()).toString();

        System.out.println(itemRegistryName);

        switch(itemRegistryName){
            case MOD_ID+":masher_rod_item":
                if(!player.isCreative()) {
                    heldItem.setCount(heldItem.getCount() - 1);
                }
                player.sendBreakAnimation(handIn);
                worldIn.setBlockState(pos, state.with(CONTAINS_ROD, true));
                return ActionResultType.SUCCESS;

            case "minecraft:water_bucket":
                if(!state.get(CONTAINS_WATER)){
                    if(!player.isCreative()){
                        ItemStack bucket = new ItemStack(Items.BUCKET);
                        player.setHeldItem(handIn, bucket);
                    }
                    worldIn.playSound(player, pos, SoundEvents.ITEM_BUCKET_EMPTY, SoundCategory.BLOCKS, 10.0f, 1.0f);
                    worldIn.setBlockState(pos, state.with(CONTAINS_WATER, true));
                    return ActionResultType.SUCCESS;
                }
                break;

            case "minecraft:bucket":
                if(state.get(CONTAINS_WATER)){
                    if(!player.isCreative()){
                        ItemStack water_bucket = new ItemStack(Items.WATER_BUCKET);
                        player.setHeldItem(handIn, water_bucket);
                    }
                    worldIn.playSound(player, pos, SoundEvents.ITEM_BUCKET_FILL, SoundCategory.BLOCKS, 10.0f, 1.0f);
                    worldIn.setBlockState(pos, state.with(CONTAINS_WATER, false)
                                                    .with(STIR_PROGRESS, 0));
                    return ActionResultType.SUCCESS;
                }
                break;
        }

        // check if the item is one of the valid foods
        String path = itemRegistryName.split(":")[1];

        if(EnumUtils.isValidEnum(FoodType.class, path) && state.get(CONTAINS_WATER)) {
            FoodType food = EnumUtils.getEnum(FoodType.class, path);
            MasherBowlTileEntity tileEntity = (MasherBowlTileEntity)worldIn.getTileEntity(pos);
            assert tileEntity != null;

            if(tileEntity.addFood(food)) {
                return ActionResultType.SUCCESS;
            }
        }

        return ActionResultType.PASS;
    }
}

I also have 2 classes that store registered blocks and tile entities:

public class TileEntitiesList {
    public static TileEntityType<?> masher_bowl;
}

public class BlocksList {
    public static Block masher_bowl;
}

 

Posted
2 hours ago, Jipthechip said:

build(null)

I am almost certain that this is wrong.

 

2 hours ago, Jipthechip said:

BlocksList.masher_bowl =

Don't set this field yourself, use @ObjectHolder annotations.

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
6 minutes ago, Draco18s said:

I am almost certain that this is wrong.

That's just for the data fixer type, which as far as I understand is unnecessary.

(To be honest I've never been able to figure out data fixers in the first place...)

I'm eager to learn and am prone to mistakes. Don't hesitate to tell me how I can improve.

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.