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)
  On 4/21/2020 at 8:00 AM, 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".

Expand  

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.

  On 4/21/2020 at 8:00 AM, 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.

Expand  

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.

  On 4/21/2020 at 8:00 AM, 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.

Expand  

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
  On 4/21/2020 at 6:39 PM, diesieben07 said:

Show your block class and where you register your TileEntityType.

Expand  

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
  On 4/21/2020 at 9:05 PM, Jipthechip said:

build(null)

Expand  

I am almost certain that this is wrong.

 

  On 4/21/2020 at 9:05 PM, Jipthechip said:

BlocksList.masher_bowl =

Expand  

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
  On 4/21/2020 at 11:49 PM, Draco18s said:

I am almost certain that this is wrong.

Expand  

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • wait, i just did that. why didn't it work? also it's the same. help me!!!!
    • Culprit has been located! It was Tough As Nails + (plus) which was among one of the mods *added* before re-entering, in an attempt to improve compatibility. It, for some reason or another, did not want to work!
    • "You can either try to load it with only the vanilla data pack ("safe mode"), or go back to the title screen and fix it manually." Hello! I encountered this error just now while attempting to re-enter my world for the first time. Notably, no mods were removed before encountering this error and, while being able to find this error elsewhere on the internet, in my scenario it is seemingly caused by a specific mod, of which I cannot isolate. Things I have tried: - Enter "Safe Mode" (fails) - Remove datapacks (no change) - Reverted to old save data (no change) Here is the server log: https://mclo.gs/9vJQEfN I use Modrinth mod loader so please let me know how to share my mod list, and if it is needed.
    • So, i'm hosting (or attempting to host) a port-forwarded modded server for 1.12.2. There's quite a few mods in this pack, but they all run and have no compatibility issues (besides something causing the game to crash if you go fullscreen, a problem ive given up trying to identify or fix). It can run on 6gb of ram or less, and works fine in singleplayer. All of my friends that want to join have the exact same mod/config/game setup as I do (I personally helped them set everything up). Just to be safe, we've all allocated 12 gigabytes of RAM to the installation, and I've also allocated 12GB to the server itself. I am able to join with no issue and it runs fine. However, when they try to join, it gets stuck in the 'logging in' screen before their game becomes unresponsive. When they close it, it gives them the exit code 805306369, which usually means not enough RAM, but this cant be the case. On my screen, in the server, it shows them joining, and then disconnecting, so I dont think its a port-forwarding issue. They can all run it just fine in singleplayer as well. The annoying bit about this particular issue is that it generates no crash log. Does anyone have any suggestions? Thanks! Here is my debug log, and his, starting from the same place. MINE: [132647] [Netty Client IO #5INFO] [FML] Attempting connection with missing mods [ctm, fusion] at SERVER 132653.817 [132653] [Client threadINFO] [minecraftGuiConnecting] Connecting to 0, 25565 132654.682 [132654] [Netty Client IO #9INFO] [STDOUT] [xaero.common.core.transformer.ClassNodeTransformertransform29] Transforming class brz net.minecraft.client.network.NetHandlerPlayClient 132654.684 [132654] [Netty Client IO #9INFO] [STDOUT] [xaero.map.core.transformer.ClassNodeTransformertransform29] Transforming class net.minecraft.client.network.NetHandlerPlayClient 132654.764 [132654] [Netty Client IO #9INFO] [FML] Server protocol version 2 132654.777 [132654] [Netty Client IO #9INFO] [FML] Attempting connection with missing mods [ctm, fusion] at SERVER 132656.193 [132656] [Client threadINFO] [FML] Injecting existing registry data into this client instance 132659.081 [132659] [Client threadINFO] [FML] Applying holder lookups 132659.082 [132659] [Client threadINFO] [FML] Holder lookups applied 132659.092 [132659] [Netty Client IO #9INFO] [FML] [Netty Client IO #9] Client side modded connection established HIS: [13:12:16] [Netty Client IO #0/INFO] [FML]: Attempting connection with missing mods [ctm, fusion] at SERVER 13:12:25.597 [13:12:25] [Client thread/INFO] [minecraft/GuiConnecting]: Connecting to (my IP address and the server port 25565) 13:12:26.805 [13:12:26] [Netty Client IO #1/INFO] [STDOUT]: [xaero.common.core.transformer.ClassNodeTransformer:transform:29]: Transforming class brz net.minecraft.client.network.NetHandlerPlayClient 13:12:26.808 [13:12:26] [Netty Client IO #1/INFO] [STDOUT]: [xaero.map.core.transformer.ClassNodeTransformer:transform:29]: Transforming class net.minecraft.client.network.NetHandlerPlayClient 13:12:26.936 [13:12:26] [Netty Client IO #1/INFO] [FML]: Server protocol version 2 13:12:27.114 [13:12:27] [Netty Client IO #1/INFO] [FML]: Attempting connection with missing mods [ctm, fusion] at SERVER 13:12:27.480 [13:12:27] [Client thread/INFO] [FML]: Injecting existing registry data into this client instance 13:12:30.669 [13:12:30] [Client thread/INFO] [FML]: Applying holder lookups 13:12:30.671 [13:12:30] [Client thread/INFO] [FML]: Holder lookups applied 13:13:04.700 Process crashed with exit code -805306369 P.S - Both mods "CTM" and "FUSION" are present in both our folders so idk what that's about.
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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