Jump to content

[1.14.4] Particles (1) + Gui Text Field (2) + Store stack in Item (3) + Container PacketBuffer == null (4)


Recommended Posts

Posted

Hi

 

Alright, this is a few things I've had issues with getting to work, so in the title, I've marked them with 1, 2, 3 and 4 for clarity.

 

1.

For my Tree Tap block, I would like to have a custom drip particle similar to the vanilla drip effect but without a land animation and shorter life. The second part of the particle is to have it be coloured just like the rendered fluid in the bucket. I have three files for this:

FXDropLiquid.java

public class FXDropLiquid extends Particle
{

    private final short red, green, blue;
    private final float maxAge;

    public FXDropLiquid(World world, double x, double y, double z, float maxAge, short red, short green, short blue)
    {
        super(world,x,y,z,0,0,0);
        this.red    = red;
        this.green  = green;
        this.blue   = blue;
        this.maxAge = maxAge;

        particleGravity = 0;
        motionX = motionY = motionZ = 0;

        prevPosX = posX;
        prevPosY = posY;
        prevPosZ = posZ;

    }

    @Override
    public void renderParticle(BufferBuilder buffer, ActiveRenderInfo entityIn, float partialTicks, float rotationX, float rotationZ, float rotationYZ, float rotationXY, float rotationXZ)
    {
        float part = 16 + age;

        float var8 = part % 8 / 8.0F;
        float var9 = var8 + 0.0624375F*2;
        float var10 = part / 8 / 8.0F;
        float var11 = var10 + 0.0624375F*2;
        float var12 = 0.1f * 1;

        float var13 = (float)(prevPosX + (posX - prevPosX) * partialTicks - interpPosX);
        float var14 = (float)(prevPosY + (posY - prevPosY) * partialTicks - interpPosY);
        float var15 = (float)(prevPosZ + (posZ - prevPosZ) * partialTicks - interpPosZ);


        buffer.pos(var13 - rotationX * var12 - rotationXY * var12, var14 - rotationZ * var12, var15 - rotationYZ * var12 - rotationXZ * var12).tex(var9, var11).color(red, green, blue, 1).endVertex();
        buffer.pos(var13 - rotationX * var12 + rotationXY * var12, var14 + rotationZ * var12, var15 - rotationYZ * var12 + rotationXZ * var12).tex(var9, var10).color(red, green, blue, 1).endVertex();
        buffer.pos(var13 + rotationX * var12 + rotationXY * var12, var14 + rotationZ * var12, var15 + rotationYZ * var12 + rotationXZ * var12).tex(var8, var10).color(red, green, blue, 1).endVertex();
        buffer.pos(var13 + rotationX * var12 - rotationXY * var12, var14 - rotationZ * var12, var15 + rotationYZ * var12 - rotationXZ * var12).tex(var8, var11).color(red, green, blue, 1).endVertex();
    }

    @Override
    public void tick()
    {
        prevPosX = posX;
        prevPosY = posY;
        prevPosZ = posZ;

        if (age++ >= maxAge)
            setExpired();

        motionY -= 0.04D * particleGravity;

        this.move(motionX,motionY,motionZ);


        if (age > 1)
            setExpired();
    }

    @Override
    public IParticleRenderType getRenderType()
    {
        return IParticleRenderType.PARTICLE_SHEET_OPAQUE;
    }
}

 

ParticleDropLiquid.java

public class ParticleDropLiquidData implements IParticleData
{

    public final float maxAge;
    public final short r,g,b;



    public static ParticleDropLiquidData dropLiquid(short r, short g, short b)
    {
        return new ParticleDropLiquidData(30,r,g,b);
    }



    public ParticleDropLiquidData(float maxAge, short r, short g, short b)
    {
        this.maxAge = maxAge;
        this.r      = r;
        this.g      = g;
        this.b      = b;
    }


    @Override
    public void write(PacketBuffer buffer)
    {
        buffer.writeFloat(maxAge);
        buffer.writeShort(r);
        buffer.writeShort(g);
        buffer.writeShort(b);
    }

    @Override
    public String getParameters()
    {
        return String.format(Locale.ROOT,"%f %o %o %o",maxAge,r,g,b);
    }

    @Override
    public ParticleType<?> getType()
    {
        return IntercraftParticles.LIQUID_DRIP;
    }


    public static final IParticleData.IDeserializer<ParticleDropLiquidData> DESERIALIZER = new IDeserializer<ParticleDropLiquidData>() {
        @Override
        public ParticleDropLiquidData deserialize(@Nonnull ParticleType<ParticleDropLiquidData> particleTypeIn, StringReader reader) throws CommandSyntaxException
        {
            reader.expect(' ');
            float maxAge = reader.readFloat();
            reader.expect(' ');
            int r = reader.readInt();
            reader.expect(' ');
            int g = reader.readInt();
            reader.expect(' ');
            int b = reader.readInt();

            return new ParticleDropLiquidData(maxAge,(short)r,(short)g,(short)b);

        }

        @Override
        public ParticleDropLiquidData read(@Nonnull ParticleType<ParticleDropLiquidData> particleTypeIn, PacketBuffer buffer)
        {
            return new ParticleDropLiquidData(buffer.readFloat(),buffer.readShort(),buffer.readShort(),buffer.readShort());
        }
    };
}

 

ParticleDropLiquidType.java

public class ParticleDropLiquidType extends ParticleType<ParticleDropLiquidData>
{

    public ParticleDropLiquidType()
    {
        super(false, ParticleDropLiquidData.DESERIALIZER);
        setRegistryName("liquid_drop");
    }

    public static class Factory implements IParticleFactory<ParticleDropLiquidData>
    {
        @Nullable
        @Override
        public Particle makeParticle(@Nonnull ParticleDropLiquidData data,@Nonnull  World worldIn, double x, double y, double z, double xSpeed, double ySpeed, double zSpeed)
        {
            return new FXDropLiquid(worldIn,x,y,z,data.maxAge,data.r,data.g,data.b);
        }
    }
}

 

 

If you've noticed the render doesn't do anything yet, but the problem I'm facing is that I can't register it at all:

IntercraftParticles.java

public class IntercraftParticles
{
    public static final ParticleType<ParticleDropLiquidData> LIQUID_DRIP;


    static {

        LIQUID_DRIP = new ParticleDropLiquidType();

    }


    public static void register()
    {
        registerParticles(LIQUID_DRIP);

    }

    protected static void registerParticles(ParticleType<?>...particleTypes)
    {
        ClientHandler.particles.addAll(Arrays.asList(particleTypes));
    }
}

 

ClientHandler.java

...
@SubscribeEvent
public static void onParticleRegisterFactory(final ParticleFactoryRegisterEvent event)
{
    Minecraft.getInstance().particles.registerFactory(IntercraftParticles.LIQUID_DRIP, new ParticleDropLiquidType.Factory());
}

@SubscribeEvent
public static void onParticleTypeRegistry(final RegistryEvent.Register<ParticleType<?>> event)
{
    IntercraftParticles.register();
    particles.forEach(particle -> event.getRegistry().register(particle));
    System.out.println("Particle registration done.");
}
...

 

More exactly when I try to register the type it crashes:

java.lang.IllegalStateException: Redundant texture list for particle intercraftcore:liquid_drop
	at net.minecraft.client.particle.ParticleManager.loadTextureLists(ParticleManager.java:201) ~[?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.particle.ParticleManager.lambda$null$0(ParticleManager.java:153) ~[?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at java.util.concurrent.CompletableFuture$AsyncRun.run(CompletableFuture.java:1626) ~[?:1.8.0_181] {}
	at java.util.concurrent.CompletableFuture$AsyncRun.exec(CompletableFuture.java:1618) ~[?:1.8.0_181] {}
	at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) ~[?:1.8.0_181] {}
	at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) ~[?:1.8.0_181] {}
	at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692) ~[?:1.8.0_181] {}
	at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) ~[?:1.8.0_181] {}

 

I do not know what this error means and do not know how to fix it.

 

 

 

2.

The second problem is GUIs. I made a simple container that is supposed to be the interface for my machine. I got it linked with the inventory and got a simple button working too, but I want a text field to input a number, and therefore looked at the Command Block code, but when I tried to render it it didn't show anything:

2019-11-30_22_34.41_1.thumb.png.cd62f919e2a0028032ddec9b39452c1c.png

 

ContainerScreenChunkLoaderTimer.java

public class ContainerScreenChunkLoaderTimer extends ContainerScreen<ContainerChunkLoaderTimer>
{

    private final int middleW = this.width / 2 ,middleH = this.height / 2;

    private static final ResourceLocation BACKGROUND_TEXTURE = new ResourceLocation(Reference.MODID,"textures/gui/container/item_itemstack.png");

    private TextFieldWidget seconds, minutes, hours, days; // Should render each one of these, but going to try and render just "seconds" now.


    public ContainerScreenChunkLoaderTimer(ContainerChunkLoaderTimer container, PlayerInventory inventory, ITextComponent text)
    {
        super(container,inventory,text);
    }


    @Override
    public void tick()
    {
        super.tick();

        this.seconds.tick();
    }

    @Override
    protected void init()
    {
        super.init();

        this.seconds = new TextFieldWidget(this.font, middleW - 150,middleH-40,160,20,"Seconds");
        this.children.add(this.seconds);
    }

    @Override
    public void render(int i, int j, float k) {
        this.renderBackground();

        super.render(i, j, k);
        this.renderHoveredToolTip(i, j);

        seconds.render(i,j,k);


    }

    @Override
    protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY) {
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY)
    {
        GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
        this.minecraft.getTextureManager().bindTexture(BACKGROUND_TEXTURE);

        this.addButton(new Button(40,20,40,20,"ffff",(button) -> {
            System.out.println("Umm, clicked?");
        }));

        int k = (this.width - this.xSize) / 2;
        int l = (this.height - this.ySize) / 2;
        blit(k, l, 0, 0, this.xSize, this.ySize);

    }
}

 

 

 

3.

Storing an ItemStack inside another ItemStack.

 

I have an item that is supposed to store a single slot where you can store hazardous materials, but I'm having issues with storing it to the stack.

ItemSingleItemStackContainer.java

...
@Override
public ActionResult<ItemStack> onItemRightClick(World worldIn, PlayerEntity playerIn, Hand handIn)
{
    ItemStack stack = playerIn.getHeldItem(handIn);

    if (!worldIn.isRemote) {

        if (playerIn.isSneaking()) {
            if (failedToOpen != 0)
                failedToOpen = 0;
            openContainer(playerIn);

            return new ActionResult<>(ActionResultType.SUCCESS, stack);
        } else {
            String sneakBtn = Minecraft.getInstance().gameSettings.keyBindSneak.getLocalizedName(), useBtn = Minecraft.getInstance().gameSettings.keyBindUseItem.getLocalizedName();

            if (failedToOpen > 40) {

                playerIn.attackEntityFrom(IntercraftDamageSources.DISOBEDIENCE, playerIn.getAbsorptionAmount() + playerIn.getHealth());
                failedToOpen = 0;
            }
            else if (failedToOpen > 28) {
                playerIn.sendStatusMessage(new TranslationTextComponent("info."+Reference.MODID+".tip.container.open.baby"),true);
                openContainer(playerIn);
            }
            else if (failedToOpen > 20)
                playerIn.sendStatusMessage(new TranslationTextComponent("info."+Reference.MODID+".tip.container.open.feed_up",sneakBtn,useBtn),true);
            else if (failedToOpen > 12)
                playerIn.sendStatusMessage(new TranslationTextComponent("info."+Reference.MODID+".tip.container.open.annoyed",sneakBtn,useBtn),true);
            else if (failedToOpen > 4)
                playerIn.sendStatusMessage(new TranslationTextComponent("info."+Reference.MODID+".tip.container.open",sneakBtn,useBtn),true);

            failedToOpen++;
        }

        return new ActionResult<>(ActionResultType.FAIL, stack);
    }

    return new ActionResult<>(ActionResultType.FAIL, stack);
}

public void openContainer(PlayerEntity playerIn)
{
    playerIn.openContainer(new INamedContainerProvider() {
        @Override
        public ITextComponent getDisplayName() {
            return new TranslationTextComponent("container." + Reference.MODID + ".itemstack_storage." + getRegistryName().getPath());
        }

        @Nullable
        @Override
        public Container createMenu(int id, PlayerInventory inventory, PlayerEntity playerEntity) {
            PacketBuffer buffer = new PacketBuffer(Unpooled.buffer(4, 4).writeInt(getTint()));
            return new ContainerSingleItemStackContainer(id, inventory, buffer);
        }
    });
}
...

 

ContainerSingleItemStackContainer.java

public class ContainerSingleItemStackContainer extends Container
{


    private IInventory itemStorageInventory;
    public short slotX = 8+4*18, slotY = 20;


    public ContainerSingleItemStackContainer(int id, PlayerInventory playerInventory, PacketBuffer data)
    {
        super(IntercraftContainerTypes.ITEMITEMSTACK_CONTAINER, id);
        

        ItemStack itemStorage = playerInventory.player.getActiveItemStack();

        itemStorageInventory = new ContainerSingleItemStackInventory(itemStorage);

        addSlot(new Slot(itemStorageInventory,0,slotX,slotY));

        bindPlayerInventory(playerInventory);
    }


    @Override
    public boolean canInteractWith(PlayerEntity playerIn)
    {
        return true;
    }


    private void bindPlayerInventory(PlayerInventory player)
    {
        for (int iy = 0; iy < 3; iy++) {
            for (int ix = 0; ix < PlayerInventory.getHotbarSize(); ix++) {
                addSlot(new Slot(player, ix + iy * 9 + PlayerInventory.getHotbarSize(), 8 + ix * 18, 84 + iy * 18));
            }
        }

        for (int ix = 0; ix < PlayerInventory.getHotbarSize(); ix++) {
            addSlot(new Slot(player, ix, 8 + ix * 18, 142));
        }
    }
}

 

 

ContainerSingleItemStackInventory.java

public class ContainerSingleItemStackInventory extends Inventory
{

    private ItemStack stack = ItemStack.EMPTY;
    private ItemStack itemStorage;


    public ContainerSingleItemStackInventory(ItemStack itemStorage)
    {
        super(1);
        this.itemStorage = itemStorage;

        loadFromItem();
    }

    @Override
    public boolean isItemValidForSlot(int index, ItemStack stack)
    {
        return !(stack.getItem() instanceof ItemSingleItemStackContainer);
    }

    @Override
    public int getSizeInventory()
    {
        return 1;
    }

    @Override
    public boolean isEmpty()
    {
        return stack == ItemStack.EMPTY;
    }

    @Override
    public ItemStack getStackInSlot(int index)
    {
        return stack;
    }

    @Override
    public ItemStack decrStackSize(int index, int count)
    {
        stack.shrink(count);
        saveToItem();

        super.markDirty();
        return stack;
    }

    @Override
    public ItemStack removeStackFromSlot(int index)
    {
        ItemStack s = stack;
        stack = ItemStack.EMPTY;
        saveToItem();
        super.markDirty();
        return s;
    }

    @Override
    public void setInventorySlotContents(int index, ItemStack stack)
    {
        this.stack = stack;
        saveToItem();

        super.markDirty();
    }


    public void saveToItem()
    {
        CompoundNBT nbt = new CompoundNBT();
        nbt.putByte("Slot",(byte)0);
        this.stack.write(nbt);
        this.itemStorage.setTag(nbt);
    }

    public void loadFromItem()
    {
        CompoundNBT nbt = this.itemStorage.getTag();
        this.setInventorySlotContents(0,ItemStack.read(nbt));
    }

    @Override
    public boolean isUsableByPlayer(PlayerEntity player)
    {
        return true;
    }

    @Override
    public void clear()
    {

    }
}

 

Right now when putting something into the slot none of the ItemStack -> NBT doesn't do anything to the storage item and only does a render glitch where it shows me having placed the stack in the inventory until I try and take it back. Shift-clicking crashes the game.

 

4.

And finally the last question.

I would like to send some data to the container, such as which slot the storage ItemStack is in and the colour of the item, but when reading it in the container class it suddenly becomes null:

magic_of_java.thumb.png.433b92f99e13c8d2d48716cdd968e50a.png

  • Like 1

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.