MFF

    Minecraft Forge France
    • Récent
    • Mots-clés
    • Populaire
    • Utilisateurs
    • Groupes
    • Forge Events
      • Automatique
      • Foncé
      • Clair
    • S'inscrire
    • Se connecter

    Block avec un container et un gui

    Planifier Épinglé Verrouillé Déplacé Résolu 1.7.x
    1.7.10
    4 Messages 3 Publieurs 1.1k Vues 1 Watching
    Charger plus de messages
    • Du plus ancien au plus récent
    • Du plus récent au plus ancien
    • Les plus votés
    Répondre
    • Répondre à l'aide d'un nouveau sujet
    Se connecter pour répondre
    Ce sujet a été supprimé. Seuls les utilisateurs avec les droits d'administration peuvent le voir.
    • Wind_BladeW Hors-ligne
      Wind_Blade
      dernière édition par

      Salut à tous, j’essaye actuellement de faire un block avec un container et un gui  cependant lorsque je fais clique droit il ne fonctionne tout simplement pas.
      J’ai déjà plusieurs fois regarder différents tutoriel mais je ne trouve pas d’erreur après avoir discuter avec un membre sur discord qui ne trouve pas non plus je poste un sujet ici:

      Version minecraft: 1.7.10
      Version Forge: 10.13.4.1558

      Class du block:

      
      package fr.wind_blade.modassembleur.blocks;
      
      import cpw.mods.fml.common.network.internal.FMLNetworkHandler;
      import fr.wind_blade.modassembleur.Assembleur;
      import fr.wind_blade.modassembleur.tileentity.TileEntityTable;
      import net.minecraft.block.Block;
      import net.minecraft.block.BlockContainer;
      import net.minecraft.block.material.Material;
      import net.minecraft.entity.EntityLivingBase;
      import net.minecraft.entity.item.EntityItem;
      import net.minecraft.entity.player.EntityPlayer;
      import net.minecraft.inventory.IInventory;
      import net.minecraft.item.ItemStack;
      import net.minecraft.nbt.NBTTagCompound;
      import net.minecraft.tileentity.TileEntity;
      import net.minecraft.world.World;
      
      public class Table extends BlockContainer{
      
          public Table(Material material) {
              super(material);
          }
      
          @Override
          public TileEntity createNewTileEntity(World p_149915_1_, int p_149915_2_) {
               return new TileEntityTable();
          }
      
          @Override
          public boolean hasTileEntity(){
              return true;
          }
      
          @Override
          public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player,  int side, float hitX, float hitY, float hitZ){
              if(!world.isRemote){
                  System.out.println("Clique Droit");
                  player.openGui(Assembleur.instance, 0, world, x, y, z);
                  return true;
              }
              return true;
          }
      
          // =============================
      
          public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase living, ItemStack stack)
         {
             TileEntity tile = world.getTileEntity(x, y, z);
             if(tile instanceof TileEntityTable)
             {
                 if(stack.hasDisplayName())
                 {
                     ((TileEntityTable)tile).setCustomName(stack.getDisplayName());
                 }
             }
         }
      
          // ===============================
      
          @Override
          public void breakBlock(World world, int x, int y, int z, Block block, int metadata){
                 TileEntity tileentity = world.getTileEntity(x, y, z);
      
                 if(tileentity instanceof IInventory)
                 {
                     IInventory inv = (IInventory)tileentity;
                     for(int i1 = 0; i1 < inv.getSizeInventory(); ++i1)
                     {
                         ItemStack itemstack = inv.getStackInSlot(i1);
      
                         if(itemstack != null)
                         {
                             float f = world.rand.nextFloat() * 0.8F + 0.1F;
                             float f1 = world.rand.nextFloat() * 0.8F + 0.1F;
                             EntityItem entityitem;
      
                             for(float f2 = world.rand.nextFloat() * 0.8F + 0.1F; itemstack.stackSize > 0; world.spawnEntityInWorld(entityitem))
                             {
                                 int j1 = world.rand.nextInt(21) + 10;
      
                                 if(j1 > itemstack.stackSize)
                                 {
                                     j1 = itemstack.stackSize;
                                 }
      
                                 itemstack.stackSize -= j1;
                                 entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(itemstack.getItem(), j1, itemstack.getItemDamage()));
                                 float f3 = 0.05F;
                                 entityitem.motionX = (double)((float)world.rand.nextGaussian() * f3);
                                 entityitem.motionY = (double)((float)world.rand.nextGaussian() * f3 + 0.2F);
                                 entityitem.motionZ = (double)((float)world.rand.nextGaussian() * f3);
      
                                 if(itemstack.hasTagCompound())
                                 {
                                     entityitem.getEntityItem().setTagCompound((NBTTagCompound)itemstack.getTagCompound().copy());
                                 }
                             }
                         }
                     }
                     world.func_147453_f(x, y, z, block);
                 }
                 super.breakBlock(world, x, y, z, block, metadata);
             }
      }
      
      

      Class du TileEntity:

      
      package fr.wind_blade.modassembleur.tileentity;
      
      import net.minecraft.entity.player.EntityPlayer;
      import net.minecraft.inventory.IInventory;
      import net.minecraft.item.ItemStack;
      import net.minecraft.nbt.NBTTagCompound;
      import net.minecraft.nbt.NBTTagList;
      import net.minecraft.tileentity.TileEntity;
      import net.minecraftforge.common.util.Constants;
      
      public class TileEntityTable extends TileEntity implements IInventory{
      
          private ItemStack[] contents = new ItemStack[9];
          private String customeName;
      
          public TileEntityTable(){
              this.contents = new ItemStack[this.getSizeInventory()];
          }
      
          public String getCustomName(){
              return this.customeName;
          }
      
          public void setCustomName(String customName){
              this.customeName = customName;
          }
      
          @Override
          public int getSizeInventory() {
              return this.contents.length;
          }
      
          @Override
          public ItemStack getStackInSlot(int slotIndex) {
              return this.contents[slotIndex];
          }
      
          @Override
          public ItemStack decrStackSize(int slotIndex, int amount) {
              if(this.contents[slotIndex] != null){
                     ItemStack itemstack;
      
                     if(this.contents[slotIndex].stackSize <= amount){
                         itemstack = this.contents[slotIndex];
                         this.contents[slotIndex] = null;
                         this.markDirty();
                         return itemstack;
                     }
      
                     else{
                         itemstack = this.contents[slotIndex].splitStack(amount);
      
                         if(this.contents[slotIndex].stackSize == 0) {
                             this.contents[slotIndex] = null;
                         }
                         this.markDirty();
                         return itemstack;
                     }
                 }
      
              else{
                         return null;
              }
          }
      
          @Override
          public ItemStack getStackInSlotOnClosing(int slotIndex) {
                 if(this.contents[slotIndex] != null){
                     ItemStack itemstack = this.contents[slotIndex];
                     this.contents[slotIndex] = null;
                     return itemstack;
                 }
                 else{
                     return null;
                 }
          }
      
          @Override
          public void setInventorySlotContents(int slotIndex, ItemStack stack) {
                this.contents[slotIndex] = stack;
      
                 if(stack != null && stack.stackSize > this.getInventoryStackLimit()){
                     stack.stackSize = this.getInventoryStackLimit();
                 }
      
                 this.markDirty();
          }
      
          @Override
          public String getInventoryName() {
              return this.hasCustomInventoryName() ? this.customeName : "container.table_tile_entity";
          }
      
          @Override
          public boolean hasCustomInventoryName() {
              return this.customeName != null || !this.customeName.equals("");
          }
      
          @Override
          public int getInventoryStackLimit() {
              return 64;
          }
      
          @Override
          public boolean isUseableByPlayer(EntityPlayer player) {
              return this.worldObj.getTileEntity(this.xCoord, this.yCoord, this.zCoord) != this ? false : player.getDistanceSq((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D) <= 64.0D;
          }
      
          @Override
          public void openInventory() {
              // TODO Auto-generated method stub
      
          }
      
          @Override
          public void closeInventory() {
              // TODO Auto-generated method stub
      
          }
      
          @Override
          public boolean isItemValidForSlot(int p_94041_1_, ItemStack p_94041_2_) {
              // TODO Auto-generated method stub
              return true;
          }
      
          @Override
          public void readFromNBT(NBTTagCompound compound){
              super.readFromNBT(compound);
      
              if(compound.hasKey("CustomName", Constants.NBT.TAG_STRING)){
                  this.customeName = compound.getString("CustomeName");
              }
      
              NBTTagList nbttaglist = compound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
      
              this.contents = new ItemStack[this.getSizeInventory()];
      
              for(int i = 0; i < nbttaglist.tagCount(); i++){
                  NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
      
                  int j = nbttagcompound1.getByte("Slot");
      
                  if(j >= 0 && j < this.contents.length){
                      this.contents[j] = ItemStack.loadItemStackFromNBT(nbttagcompound1);
                  }
              }
          }
      
          @Override
          public void writeToNBT(NBTTagCompound compound){
              super.writeToNBT(compound);
      
              if(this.hasCustomInventoryName()){
                  compound.setString("CustomName", this.customeName);
              }
      
              NBTTagList nbttaglist = new NBTTagList();
      
              for(int i = 0; i < this.contents.length; i++){
                  if(this.contents* != null){
                      NBTTagCompound nbttagcompound1 = new NBTTagCompound();
      
                      nbttagcompound1.setByte("Slot", (byte)i);
      
                      this.contents*.writeToNBT(nbttagcompound1);
      
                      nbttaglist.appendTag(nbttagcompound1);
                  }
              }
              compound.setTag("Items", nbttaglist);
          }
      }
      
      

      Class du Handler:

      
      package fr.wind_blade.modassembleur.gui;
      
      import net.minecraft.entity.player.EntityPlayer;
      import net.minecraft.tileentity.TileEntity;
      import net.minecraft.world.World;
      import cpw.mods.fml.common.network.IGuiHandler;
      import fr.wind_blade.modassembleur.tileentity.TileEntityTable;
      import fr.wind_blade.modassembleur.gui.ContainerTable;
      
      public class GuiHandlerAss implements IGuiHandler {
      
          @Override
          public Object getServerGuiElement(int ID, EntityPlayer player, World world,
                  int x, int y, int z){
              TileEntity tile = world.getTileEntity(x, y, z);
              if(tile instanceof TileEntityTable){
                  System.out.println("test11");
                  return new ContainerTable((TileEntityTable)tile, player.inventory);
              }
              return null;
          }
      
          @Override
          public Object getClientGuiElement(int ID, EntityPlayer player, World world,
                  int x, int y, int z) {
              TileEntity tile = world.getTileEntity(x, y, z);
              if(tile instanceof TileEntityTable){
                  return null;
                  //return GuiTable();
              }
              return null;
          }
      
      }
      
      

      Class du Container:

      
      package fr.wind_blade.modassembleur.gui;
      
      import fr.wind_blade.modassembleur.tileentity.TileEntityTable;
      import net.minecraft.entity.player.EntityPlayer;
      import net.minecraft.entity.player.InventoryPlayer;
      import net.minecraft.inventory.Container;
      import net.minecraft.inventory.Slot;
      import net.minecraft.item.ItemStack;
      import net.minecraft.tileentity.TileEntity;
      
      public class ContainerTable extends Container {
      
          private final TileEntityTable tileTable;
      
          public ContainerTable(TileEntityTable tile, InventoryPlayer player) {
              this.tileTable = tile;
              tile.openInventory();
              System.out.println("Génération");
      
              this.addSlotToContainer(new Slot(tile, 0, 10, 20));
              this.bindPlayerInventory(player);
          }
      
          public void bindPlayerInventory(InventoryPlayer inventory){
               int i;
                 for(i = 0; i < 3; ++i)
                 {
                     for(int j = 0; j < 9; ++j)
                     {
                         this.addSlotToContainer(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 86 + i * 18));
                     }
                 }
      
                 for(i = 0; i < 9; ++i)
                 {
                     this.addSlotToContainer(new Slot(inventory, i, 8 + i * 18, 144));
                 }
          }
      
          // ==========================
          public ItemStack transferStackInSlot(EntityPlayer player, int slotIndex){
                 ItemStack itemstack = null;
                 Slot slot = (Slot)this.inventorySlots.get(slotIndex);
      
                 if(slot != null && slot.getHasStack())
                 {
                     ItemStack itemstack1 = slot.getStack();
                     itemstack = itemstack1.copy();
      
                     if(slotIndex < this.tileTable.getSizeInventory())
                     {
                         if(!this.mergeItemStack(itemstack1, this.tileTable.getSizeInventory(), this.inventorySlots.size(), true))
                         {
                             return null;
                         }
                     }
                     else if(!this.mergeItemStack(itemstack1, 0, this.tileTable.getSizeInventory(), false))
                     {
                         return null;
                     }
      
                     if(itemstack1.stackSize == 0)
                     {
                         slot.putStack((ItemStack)null);
                     }
                     else
                     {
                         slot.onSlotChanged();
                     }
                 }
                 return itemstack;
             }
      
          @Override
          public boolean canInteractWith(EntityPlayer player) {
              return this.tileTable.isUseableByPlayer(player);
          }
      
          public void onContainerClose(EntityPlayer player){
              super.onContainerClosed(player);
              this.tileTable.closeInventory();
          }
      
      }
      
      

      Les enregistrements:

      
      table = new Table(Material.wood).setBlockName("Table");
      GameRegistry.registerTileEntity(TileEntityTable.class, "TileEntityTable");
      NetworkRegistry.INSTANCE.registerGuiHandler(Assembleur.instance, new GuiHandlerAss());
      
      

      Le gui n’existe pas encore mais il devrait au moins ouvrire avec les slots

      1 réponse Dernière réponse Répondre Citer 0
      • LeBossMax2L Hors-ligne
        LeBossMax2
        dernière édition par

        Non, c’est le gui qui affiche les slots. Si il n’y a pas de gui, ça ne peut pas fonctionner.

        1 réponse Dernière réponse Répondre Citer 0
        • Wind_BladeW Hors-ligne
          Wind_Blade
          dernière édition par

          Quand je disait gui enfaite je parler de la texture, là où j’en suis je devrais normalement pouvoir quand même l’ouvrir (d’après les tutoriels du forum)
          Edit : Ok désoler je me suis tromper (je me suis fait avoir en regardant la vidéo ^^’ il faut quand même l’envoyer)

          1 réponse Dernière réponse Répondre Citer 0
          • BrokenSwingB Hors-ligne
            BrokenSwing Moddeurs confirmés Rédacteurs
            dernière édition par

            Il faut que ça retourne un gui. La fonction s’appelle getClientGuiElement, donc si tu retourne null, tu n’aura rien d’affiché, logique.

            1 réponse Dernière réponse Répondre Citer 0
            • 1 / 1
            • Premier message
              Dernier message
            Design by Woryk
            ContactMentions Légales

            MINECRAFT FORGE FRANCE © 2024

            Powered by NodeBB