TESR和自定义Java模型(1.12.2)

时间:2019-05-12 19:00:48

标签: java minecraft minecraft-forge

第一个帖子在这里。 好吧,我对TESR和Java模型有一点问题; 因此,首先,我对Java和编程都不陌生,但是我对伪造没有太多的经验。

我想根据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 个答案:

答案 0 :(得分:0)

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

  1. 您正在运行1.12,这意味着您可以使用Capabilities,而不是所有这些整数值。实际上,已经有处理流体的功能。 CapabilityFluidHandler.FLUID_HANDLER_CAPABILITY

  2. 请勿扩展TileEntityLockableLoot,这几乎可以肯定对您没有帮助。香草Minecraft实施某些事物(例如库存)的方式是不灵活的,并且不是在改装时做事的好方法。 TileEntityLockableLoot(并扩展为IInventory)就是其中之一。

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

您需要覆盖these three methods 1 并确保每当数据更改时就call these four

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