Skip to content
This repository has been archived by the owner on Jan 25, 2020. It is now read-only.

Commit

Permalink
Ajout d'un gui et d'un container
Browse files Browse the repository at this point in the history
  • Loading branch information
robin4002 committed Sep 8, 2013
1 parent 45ca37e commit 2d40dba
Show file tree
Hide file tree
Showing 9 changed files with 427 additions and 8 deletions.
5 changes: 4 additions & 1 deletion assets/modtutoriel/lang/en_US.lang
Expand Up @@ -35,4 +35,7 @@ item.TutorialShovel.name=Tutorial Shovel
item.TutorialHoe.name=Tutorial Hoe

#CreativeTabs
itemGroup.TutorialCreativeTabs=Tutorial Creative Table
itemGroup.TutorialCreativeTabs=Tutorial Creative Table

#Container
container.bigchest=Big Chest
5 changes: 4 additions & 1 deletion assets/modtutoriel/lang/fr_FR.lang
Expand Up @@ -35,4 +35,7 @@ item.TutorialShovel.name=Pelle tutoriel
item.TutorialHoe.name=Houe tutoriel

#CreativeTabs
itemGroup.TutorialCreativeTabs=Tutoriel Table Créative
itemGroup.TutorialCreativeTabs=Tutoriel Table Créative

#Container
container.bigchest=Grand coffre
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions tutoriel/client/GuiBigChest.java
@@ -0,0 +1,42 @@
package tutoriel.client;

import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;

import org.lwjgl.opengl.GL11;

import tutoriel.common.ContainerBigChest;
import tutoriel.common.TileEntityBigChest;

public class GuiBigChest extends GuiContainer
{
public static ResourceLocation texture = new ResourceLocation("modtutoriel", "textures/gui/container/bigChest.png");
private TileEntityBigChest bigChest;
private IInventory playerInventory;
public GuiBigChest(InventoryPlayer inventory, TileEntityBigChest tileEntity)
{
super(new ContainerBigChest(inventory, tileEntity));
this.bigChest = tileEntity;
this.playerInventory = inventory;
this.ySize = 230;
}

protected void drawGuiContainerForegroundLayer(int par1, int par2)
{
this.fontRenderer.drawString(this.playerInventory.isInvNameLocalized() ? this.playerInventory.getInvName() : I18n.getString(this.playerInventory.getInvName()), 8, 129, 0);
this.fontRenderer.drawString(this.bigChest.isInvNameLocalized() ? this.bigChest.getInvName() : I18n.getString(this.bigChest.getInvName()), 8, 7, 0);
}

@Override
protected void drawGuiContainerBackgroundLayer(float f, int i, int j)
{
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(texture);
int x = (this.width - this.xSize) / 2;
int y = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(x, y, 0, 0, this.xSize, this.ySize);
}
}
79 changes: 73 additions & 6 deletions tutoriel/common/BlockTutorialMetadata.java
Expand Up @@ -7,13 +7,17 @@
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.FMLNetworkHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

Expand Down Expand Up @@ -117,17 +121,22 @@ public TileEntity createNewTileEntity(World world)
@Override
public TileEntity createTileEntity(World world, int metadata)
{
if(metadata == 0)
switch(metadata)
{
case 0:
return new TileEntityTutorial();
else if(metadata == 2)
case 2:
return new TileEntityTutorial2();
else
case 3:
return new TileEntityBigChest();
default:
return null;
}
}

public boolean hasTileEntity(int metadata)
{
if(metadata == 0 || metadata == 2)
if(metadata == 0 || metadata == 2 || metadata == 3)
return true;
else
return false;
Expand Down Expand Up @@ -160,10 +169,13 @@ public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer p
}
return true;
}
else

if(world.getBlockMetadata(x, y, z) == 3)
{
return false;
FMLNetworkHandler.openGui(player, ModTutoriel.instance, 0, world, x, y, z);
return true;
}
return false;
}

public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase living, ItemStack stack)
Expand All @@ -175,5 +187,60 @@ public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase l
((TileEntityTutorial2)te).setDirection((byte)direction);
world.markBlockForUpdate(x, y, z);
}
if(te != null && stack.getItemDamage() == 3 && te instanceof TileEntityBigChest && stack.hasDisplayName())
{
((TileEntityBigChest)te).setCustomGuiName(stack.getDisplayName());
}
}

public void breakBlock(World world, int x, int y, int z, int side, int metadata)
{
if(metadata == 3)
{
dropContainerItem(world, x, y, z);
}
super.breakBlock(world, x, y, z, side, metadata);
}

protected void dropContainerItem(World world, int x, int y, int z)
{
TileEntityBigChest bigchest = (TileEntityBigChest)world.getBlockTileEntity(x, y, z);

if (bigchest != null)
{
for (int slotId = 0; slotId < bigchest.getSizeInventory(); slotId++)
{
ItemStack stack = bigchest.getStackInSlot(slotId);

if (stack != 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; stack.stackSize > 0; world.spawnEntityInWorld(entityitem))
{
int k1 = world.rand.nextInt(21) + 10;

if (k1 > stack.stackSize)
{
k1 = stack.stackSize;
}

stack.stackSize -= k1;
entityitem = new EntityItem(world, (double)((float)x + f), (double)((float)y + f1), (double)((float)z + f2), new ItemStack(stack.itemID, k1, stack.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 (stack.hasTagCompound())
{
entityitem.getEntityItem().setTagCompound((NBTTagCompound)stack.getTagCompound().copy());
}
}
}
}
}
}
}
83 changes: 83 additions & 0 deletions tutoriel/common/ContainerBigChest.java
@@ -0,0 +1,83 @@
package tutoriel.common;

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;

public class ContainerBigChest extends Container
{
private TileEntityBigChest tileEntity;

public ContainerBigChest(InventoryPlayer playerInventory, TileEntityBigChest teChest)
{
this.tileEntity = teChest;

for(int i = 0; i < 6; i++)
{
for(int j = 0; j < 9; j++)
{
this.addSlotToContainer(new Slot(teChest, j + i * 9, 8 + j * 18, 18 + i * 18));
}
}
this.bindPlayerInventory(playerInventory);
}

private void bindPlayerInventory(InventoryPlayer playerInventory)
{
int i;
for(i = 0; i < 3; i++)
{
for(int j = 0; j < 9; j++)
{
this.addSlotToContainer(new Slot(playerInventory, j + i * 9 + 9, 8 + j * 18, 103 + i * 18 + 37));
}
}

for(i = 0; i < 9; i++)
{
this.addSlotToContainer(new Slot(playerInventory, i, 8 + i * 18, 161 + 37));
}
}

@Override
public boolean canInteractWith(EntityPlayer player)
{
return tileEntity.isUseableByPlayer(player);
}

public ItemStack transferStackInSlot(EntityPlayer player, int slotId)
{
ItemStack itemstack = null;
Slot slot = (Slot)this.inventorySlots.get(slotId);

if(slot != null && slot.getHasStack())
{
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();

if(slotId < 9)
{
if(!this.mergeItemStack(itemstack1, 9, this.inventorySlots.size(), true))
{
return null;
}
}
else if(!this.mergeItemStack(itemstack1, 0, 9, false))
{
return null;
}

if(itemstack1.stackSize == 0)
{
slot.putStack((ItemStack)null);
}
else
{
slot.onSlotChanged();
}
}
return itemstack;
}
}
33 changes: 33 additions & 0 deletions tutoriel/common/GuiHandlerTutorial.java
@@ -0,0 +1,33 @@
package tutoriel.common;

import tutoriel.client.GuiBigChest;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import cpw.mods.fml.common.network.IGuiHandler;

public class GuiHandlerTutorial implements IGuiHandler
{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
TileEntity te = world.getBlockTileEntity(x, y, z);
if(te instanceof TileEntityBigChest)
{
return new ContainerBigChest(player.inventory, (TileEntityBigChest)te);
}
return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z)
{
TileEntity te = world.getBlockTileEntity(x, y, z);
if(te instanceof TileEntityBigChest)
{
return new GuiBigChest(player.inventory, (TileEntityBigChest)te);
}
return null;
}
}
6 changes: 6 additions & 0 deletions tutoriel/common/ModTutoriel.java
Expand Up @@ -19,7 +19,10 @@
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.network.NetworkRegistry;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.TickRegistry;
import cpw.mods.fml.relauncher.Side;

@Mod(modid = "ModTutoriel", name = "Mod Tutoriel", version = "1.0.0", acceptedMinecraftVersions = "[1.6.2,)")
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
Expand Down Expand Up @@ -145,6 +148,9 @@ public void Init(FMLInitializationEvent event)
// Registry
GameRegistry.registerTileEntity(TileEntityTutorial.class, "TileEntityTutorial");
GameRegistry.registerTileEntity(TileEntityTutorial2.class, "TileEntityTutorial2");
GameRegistry.registerTileEntity(TileEntityBigChest.class, "TileEntityBigChest");

NetworkRegistry.instance().registerGuiHandler(this.instance, new GuiHandlerTutorial());

// Tools
MinecraftForge.setToolClass(TutorialPickaxe, "pickaxe", 3);
Expand Down

0 comments on commit 2d40dba

Please sign in to comment.