java - 测试和自定义 Java 模型 (1.12.2)

标签 java minecraft minecraft-forge

这里是第一篇文章。 嗯,我在 TESR 和 Java 模型方面遇到了一些问题; 所以,首先,我对 Java 和编程并不陌生,但我对 Forge 没有太多经验。

我想根据 TileEntity 提供的值重新缩放此模型中的立方体。

我已经尝试在模型中创建变量并在外部更改它,但失败了。在此之前,模型已在世界中渲染到正确的位置等。

现在我尝试将重新缩放值添加到我的模型构造函数中,但现在模型甚至没有在世界中渲染。

//Model Class
package com.gpginc.gp_extended.blocks.renderer.models;

//Made with Blockbench
//Paste this code into your mod.

import net.minecraft.client.model.ModelBase;
import net.minecraft.client.model.ModelBox;
import net.minecraft.client.model.ModelRenderer;
import net.minecraft.entity.Entity;

public class ModelFluidContainer extends ModelBase {
    private final ModelRenderer bone;
    private final ModelRenderer glasses_left;
    private final ModelRenderer glasses_right;
    private final ModelRenderer lava;
    private final ModelRenderer water;

    /**
     * I've tried changing this values, and they were placed where u found water_y and lava_y, respectively
     */
    public int waterY = 0;
    public int lavaY = 0;

    public ModelFluidContainer(int water_y, int lava_y) {
        textureWidth = 128;
        textureHeight = 128;

        bone = new ModelRenderer(this);
        bone.setRotationPoint(0.0F, 24.0F, 0.0F);
        bone.cubeList.add(new ModelBox(bone, 56, 0, -8.0F, -1.0F, -8.0F, 16, 1, 16, 0.0F, false));
        bone.cubeList.add(new ModelBox(bone, 0, 28, -2.0F, -15.0F, -8.0F, 4, 14, 16, 0.0F, false));
        bone.cubeList.add(new ModelBox(bone, 0, 49, -8.0F, -16.0F, -8.0F, 16, 1, 1, 0.0F, false));
        bone.cubeList.add(new ModelBox(bone, 0, 55, -8.0F, -16.0F, 7.0F, 16, 1, 1, 0.0F, false));
        bone.cubeList.add(new ModelBox(bone, 1, 35, -2.0F, -16.0F, -7.0F, 4, 1, 14, 0.0F, false));
        bone.cubeList.add(new ModelBox(bone, 4, 40, 7.0F, -16.0F, -7.0F, 1, 1, 14, 0.0F, false));
        bone.cubeList.add(new ModelBox(bone, 4, 41, -8.0F, -16.0F, -7.0F, 1, 1, 14, 0.0F, false));

        glasses_left = new ModelRenderer(this);
        glasses_left.setRotationPoint(0.0F, 24.0F, 0.0F);
        glasses_left.cubeList.add(new ModelBox(glasses_left, 0, 0, 7.0F, -15.0F, -7.0F, 0, 14, 14, 0.0F, false));
        glasses_left.cubeList.add(new ModelBox(glasses_left, 9, 0, 2.0F, -15.0F, -7.0F, 5, 14, 0, 0.0F, false));
        glasses_left.cubeList.add(new ModelBox(glasses_left, 9, 0, 2.0F, -15.0F, 7.0F, 5, 14, 0, 0.0F, false));

        glasses_right = new ModelRenderer(this);
        glasses_right.setRotationPoint(0.0F, 24.0F, 0.0F);
        setRotationAngle(glasses_right, 0.0F, 3.1416F, 0.0F);
        glasses_right.cubeList.add(new ModelBox(glasses_right, 0, 0, 7.0F, -15.0F, -7.0F, 0, 14, 14, 0.0F, false));
        glasses_right.cubeList.add(new ModelBox(glasses_right, 9, 0, 2.0F, -15.0F, -7.0F, 5, 14, 0, 0.0F, false));
        glasses_right.cubeList.add(new ModelBox(glasses_right, 9, 0, 2.0F, -15.0F, 7.0F, 5, 14, 0, 0.0F, false));

        lava = new ModelRenderer(this);
        lava.setRotationPoint(0.0F, 24.0F, 0.0F);
        lava.cubeList.add(new ModelBox(lava, 88, 21, 2.0F, -1.0F, -6.7F, 4, lava_y, 13, 0.0F, false));

        water = new ModelRenderer(this);
        water.setRotationPoint(0.0F, 24.0F, 0.0F);
        water.cubeList.add(new ModelBox(water, 90, 80, -6.7F, -1.0F, -7.0F, 4, water_y, 13, 0.0F, false));
    }

    @Override
    public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5) {
        bone.render(f5);
        glasses_left.render(f5);
        glasses_right.render(f5);
        lava.render(f5);
        water.render(f5);
    }
    public void setRotationAngle(ModelRenderer modelRenderer, float x, float y, float z) {
        modelRenderer.rotateAngleX = x;
        modelRenderer.rotateAngleY = y;
        modelRenderer.rotateAngleZ = z;
    }
}
//TESR class
package com.gpginc.gp_extended.blocks.renderer.tileentity;

import com.gpginc.gp_extended.blocks.machines.fluidcontainer.FluidContainerBlock;
import com.gpginc.gp_extended.blocks.machines.fluidcontainer.TileEntityFluidContainer;
import com.gpginc.gp_extended.blocks.renderer.models.ModelFluidContainer;
import com.gpginc.gp_extended.util.Reference;

import net.minecraft.block.Block;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class TileEntityFluidContainerRenderer extends TileEntitySpecialRenderer<TileEntityFluidContainer>
{
    private static final ResourceLocation TEXTURE_ALL = new ResourceLocation(Reference.MOD_ID + ":textures/blocks/special/fluid_block_container.png");

    public TileEntityFluidContainerRenderer() {}

    public void render(TileEntityFluidContainer te, double x, double y, double z, float partialTicks, int destroyStage, float alpha)
    {
        GlStateManager.enableDepth();
        GlStateManager.depthFunc(515);
        GlStateManager.depthMask(true);

        ModelFluidContainer newmodel = new ModelFluidContainer(Math.round(te.waterValue / 10), Math.round(te.lavaValue / 10));
        if (destroyStage >= 0)
        {
            this.bindTexture(DESTROY_STAGES[destroyStage]);
            GlStateManager.matrixMode(5890);
            GlStateManager.pushMatrix();
            GlStateManager.scale(4.0F, 4.0F, 1.0F);
            GlStateManager.translate(0.0625F, 0.0625F, 0.0625F);
            GlStateManager.matrixMode(5888);
        } else this.bindTexture(TEXTURE_ALL);

        GlStateManager.pushMatrix();
        GlStateManager.enableRescaleNormal();
        GlStateManager.translate((float)x, (float)y + 1.0F, (float)z + 1.0F);
        GlStateManager.scale(1.0F, -1.0F, -1.0F);
        GlStateManager.translate(0.5f,-0.5f,0.5f);

        newmodel.waterY = Math.round(te.waterValue / 10);
        newmodel.lavaY = Math.round(te.lavaValue / 10);

        newmodel.render(null, 0, 0, 0, 0, 0, 0.0625f);
        GlStateManager.disableRescaleNormal();
        GlStateManager.popMatrix();
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);

        if (destroyStage >= 0)
        {
            GlStateManager.matrixMode(5890);
            GlStateManager.popMatrix();
            GlStateManager.matrixMode(5888);
        }

    }
}

//For some use, tileentity class
package com.gpginc.gp_extended.blocks.machines.fluidcontainer;

import java.util.logging.Logger;

import com.gpginc.gp_extended.blocks.machines.fluidcontainer.container.ContainerFluidContainer;
import com.gpginc.gp_extended.util.Reference;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.ItemStackHelper;
import net.minecraft.item.ItemBucket;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntityLockableLoot;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ITickable;
import net.minecraft.util.NonNullList;

public class TileEntityFluidContainer extends TileEntityLockableLoot implements ITickable
{

    private NonNullList<ItemStack> blockContents = NonNullList.<ItemStack>withSize(16, ItemStack.EMPTY);
    public int waterValue = 0;
    public int lavaValue = 0;


    private int currentWaterValue, currentLavaValue;

    private final int maxWater = 100;
    private final int maxLava = 100;

    private int numPlayersUsing, tickSinceSync;


    @Override
    public boolean hasFastRenderer() 
    {
        return true;
    }
    public void increaseWaterCount(EntityPlayer player, EnumHand hand)
    {
        if(player.getHeldItem(hand).getItem() instanceof ItemBucket &&!player.isCreative())
        {
            player.getHeldItem(hand).shrink(1);
            player.setHeldItem(hand, new ItemStack(Items.BUCKET));
        }
        ++this.waterValue;
        Logger.getGlobal().warning("AUMENTO DE AGUAA "+this.waterValue);
    }
    public void increaseLavaCount(EntityPlayer player, EnumHand hand)
    {
        if(player.getHeldItem(hand).getItem() instanceof ItemBucket&&!player.isCreative())
        {
            player.getHeldItem(hand).shrink(1);
            player.setHeldItem(hand, new ItemStack(Items.BUCKET));
        }
        ++this.lavaValue;
        Logger.getGlobal().warning("AUMENTO DE lavaaa " + this.lavaValue);
    }
    @Override
    public void readFromNBT(NBTTagCompound compound) {
        super.readFromNBT(compound);
        this.blockContents = NonNullList.<ItemStack>withSize(this.getSizeInventory(), ItemStack.EMPTY);

        if(!this.checkLootAndRead(compound)) ItemStackHelper.loadAllItems(compound, blockContents);
        if(compound.hasKey("CustomName", 8)) this.customName = compound.getString("CustomName");
    }
    @Override
    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
        super.writeToNBT(compound);

        if(!this.checkLootAndRead(compound)) ItemStackHelper.saveAllItems(compound, blockContents);
        if(compound.hasKey("CustomName", 8)) compound.setString("CustomName", this.customName);
        return compound;

    }

    @Override
    public String getName()
    {
        return this.hasCustomName() ? this.customName : "container.fluid_container";
    }
    public void setCustomName(String displayName)
    {
        this.customName = displayName;      
    }
    @Override
    public int getSizeInventory() {
        return 16;
    }
    @Override
    public boolean isEmpty()
    {
        for(ItemStack stack: this.blockContents)
        {
            if(!stack.isEmpty()) return false;
        }           
        return true;
    }
    @Override
    public int getInventoryStackLimit()
    {
        return 128;
    }
    @Override
    public Container createContainer(InventoryPlayer playerInventory, EntityPlayer playerIn) 
    {
        return new ContainerFluidContainer(playerInventory, this, playerIn);
    }
    @Override
    public String getGuiID() 
    {
        return Reference.MOD_ID + ":fluid_container";
    }
    @Override
    protected NonNullList<ItemStack> getItems() 
    {
        return this.blockContents;
    }
    @Override
    public void openInventory(EntityPlayer player)
    {
        ++this.numPlayersUsing;
        this.world.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
        this.world.notifyNeighborsOfStateChange(this.pos, this.getBlockType(), true);
    }
    @Override
    public void closeInventory(EntityPlayer player) 
    {
        --this.numPlayersUsing;
        this.world.addBlockEvent(this.pos, this.getBlockType(), 1, this.numPlayersUsing);
        this.world.notifyNeighborsOfStateChange(this.pos, this.getBlockType(), true);
    }
    @Override
    public void update() {
        // TODO Auto-generated method stub

    }
}

结果是这样的: 当为空时(两个值都等于 0) http://prntscr.com/nnmvgx

满时(两个值都等于 13) http://prntscr.com/nnmw4c

提前致谢!

最佳答案

关于您的代码,我需要提及很多事情......

  1. 您正在运行 1.12,这意味着您可以访问 Capabilities ,您应该使用它来代替所有这些整数值。事实上,已经具备了流体处理功能; CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY

  2. 不要扩展TileEntityLockableLoot,它几乎肯定不会给你带来任何好处。原生《我的世界》实现某些功能(如库存)的方式不灵活,在 retrofit 时并不是一个好方法。 TileEntityLockableLoot(以及扩展,IInventory)就是其中之一。

  3. 现在我们来了解为什么它不起作用。您没有告诉客户储存的水和熔岩的数量。您永远不会将此数据从服务器同步到客户端。

您需要覆盖 these three methods 1 并确保 call these four每当数据发生变化时。

  1. 请注意,将整个 TE 序列化为 NBT 并不是一个很好的解决方案(因为通过网络传递的大量数据将不会被使用),您应该只序列化必要的数据,但是这是一个快速的解决方案-and-dirty 方法可行。

关于java - 测试和自定义 Java 模型 (1.12.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56102526/

相关文章:

java - 返回一个可关闭的可迭代对象

java - 使用 PDFBox 2.0 从 PDF 中提取文本

java.sql.SQLException : Access denied for user 'wineship' @'localhost' (using password: YES) 异常

java - 注册项目时出错 [1.12.2] Minecraft Forge

java - 在应用程序服务器中运行 java "background application"

java - 如何使一个枚举项包含另一个枚举项?

java - 重新启动游戏服务器后的 Ghost Java 进程

java - 如何检测门户何时损坏?

java - 我的世界 : Item not registering

minecraft-forge - Minecraft 透明渲染隐藏方 block