Skip to content

Instantly share code, notes, and snippets.

@LionZXY
Created July 17, 2015 10:43
Show Gist options
  • Save LionZXY/3609007c08e7620b3cff to your computer and use it in GitHub Desktop.
Save LionZXY/3609007c08e7620b3cff to your computer and use it in GitHub Desktop.
package com.lionzxy.mysticalislands.gun;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
/**
* Created by lionzxy on 16.07.15.
*/
public class ContainerGun extends Container{
private static final int
/*Наш инвентарь идет так:
1 2 3
4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21
22 23 24 25 26 27 28 29 30
__________________________
31 32 33 34 35 36 37 38 39
*/
INV_START = InventoryGun.INV_SIZE,
INV_END = INV_START + 26,
HOTBAR_START = INV_END + 1,
HOTBAR_END = HOTBAR_START + 8;
public ContainerGun(EntityPlayer player){
//Add slots for gun
for(int i=0;i<3;i++){
this.addSlotToContainer(new Slot(new InventoryGun(new ItemStack(Items.carrot)),i,81+i*18,29));
}
//Add inventory slot
for (int i = 1; i < 4; ++i) {
for (int j = 0; j < 9; ++j) {
this.addSlotToContainer(new Slot(player.inventory, j + i * 9+9, 9 + j * 18, 60 + i * 18));
}
}
//Add Hotbar slots
for (int i = 0; i < 9; ++i) {
this.addSlotToContainer(new Slot(player.inventory, i, 9 + i * 18, 136));
}
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return player.inventory.isUseableByPlayer(player);
}
public ItemStack transferStackInSlot(EntityPlayer player, int numSlot) {
ItemStack itemstack = null;
Slot slot =(Slot) this.inventorySlots.get(numSlot);
System.out.println(numSlot);
if (slot != null && slot.getHasStack()) {
ItemStack itemstack1 = slot.getStack();
itemstack = itemstack1.copy();
if (numSlot <INV_START) {
//Move item from gun inventory to inventory
if (!this.mergeItemStack(itemstack1, INV_START, HOTBAR_END + 1, true)) {
return null;
}
slot.onSlotChange(itemstack1, itemstack);
} else {
if (numSlot >= INV_START && numSlot <= INV_END) {
/* Item in Player Inventory, move to the gun inventory */
System.out.println("Item in Player Inventory, move to the gun inventory ");
if (!this.mergeItemStack(itemstack1, 0, 2, false)) {
return null;
}
}
/* Item in the Hot Bar, move it to Player Inventory */
else if (numSlot >= HOTBAR_START && numSlot <= HOTBAR_END) {
System.out.println("Item in the Hot Bar, move it to Player Inventory ");
if (!this.mergeItemStack(itemstack1, INV_START, INV_END + 1, false)) {
return null;
}
}
}
if (itemstack1.stackSize == 0) {
slot.putStack((ItemStack) null);
} else {
slot.onSlotChanged();
}
if (itemstack1.stackSize == itemstack.stackSize) {
return null;
}
slot.onPickupFromSlot(player, itemstack1);
}
return itemstack;
}
/*@Override
public ItemStack slotClick(int slot, int button, int flag, EntityPlayer player) {
// This will prevent the player from interacting with the item that opened the inventory:
if (slot >= 0 && getSlot(slot) != null && getSlot(slot).getStack() == player.getHeldItem()) {
return null;
}
return super.slotClick(slot, button, flag, player);}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment