summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/storage/TileEntityMealInlet.java
blob: d10af3d9b11a698b56332cfa2a78653c3dd6c707 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package jp.plusplus.fbs.storage;

import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityChest;
import net.minecraftforge.common.util.ForgeDirection;

/**
 * Created by plusplus_F on 2016/03/07.
 */
public class TileEntityMealInlet extends TileEntity implements IMealDevice {
    public ItemStack fragment;

    @Override
    public Packet getDescriptionPacket() {
        NBTTagCompound nbtTagCompound = new NBTTagCompound();
        this.writeToNBT(nbtTagCompound);
        return new S35PacketUpdateTileEntity(this.xCoord, this.yCoord, this.zCoord, 1, nbtTagCompound);
    }
    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
        this.readFromNBT(pkt.func_148857_g());
    }

    @Override
    public void readFromNBT(NBTTagCompound par1NBTTagCompound){
        super.readFromNBT(par1NBTTagCompound);
        if(par1NBTTagCompound.hasKey("Fragment")) fragment=ItemStack.loadItemStackFromNBT(par1NBTTagCompound.getCompoundTag("Fragment"));
        else fragment=null;
    }
    @Override
    public void writeToNBT(NBTTagCompound par1NBTTagCompound){
        super.writeToNBT(par1NBTTagCompound);
        if(fragment!=null) par1NBTTagCompound.setTag("Fragment", fragment.writeToNBT(new NBTTagCompound()));
    }

    @Override
    public ItemStack getFragment() {
        return fragment;
    }

    @Override
    public void setFragment(ItemStack f){
        fragment=f;
    }

    @Override
    public boolean hasFragment() {
        return fragment!=null;
    }

    @Override
    public void updateEntity(){
        if(!worldObj.isRemote && worldObj.getWorldTime()%10==0){
            ItemStack packet=null;
            TileEntityMeal tem=ItemMealFragment.getTileEntity(fragment);
            if(tem==null) return;

            //パケットを得る
            ForgeDirection dir=ForgeDirection.getOrientation(worldObj.getBlockMetadata(xCoord, yCoord, zCoord)&7);
            TileEntity te=worldObj.getTileEntity(xCoord+dir.offsetX, yCoord+dir.offsetY, zCoord+dir.offsetZ);
            if(te instanceof ISidedInventory){
                ISidedInventory inv=(ISidedInventory)te;
                int side=dir.ordinal()^1;
                int[] index=inv.getAccessibleSlotsFromSide(side);

                for(int i=0;i<index.length;i++){
                    ItemStack itemStack=inv.getStackInSlot(index[i]);
                    if(itemStack!=null && inv.canExtractItem(index[i], itemStack, side)){
                        packet=itemStack.copy();
                        packet.stackSize=1;
                        itemStack.stackSize--;
                        if(itemStack.stackSize<=0) inv.setInventorySlotContents(index[i], null);
                        te.markDirty();
                        break;
                    }
                }
            }
            else if(te instanceof TileEntityChest){
                TileEntityChest[] c=new TileEntityChest[2];
                c[0]=(TileEntityChest)te;

                if(c[0].adjacentChestXPos!=null) c[1]=c[0].adjacentChestXPos;
                if(c[0].adjacentChestXNeg!=null) c[1]=c[0].adjacentChestXNeg;
                if(c[0].adjacentChestZPos!=null) c[1]=c[0].adjacentChestZPos;
                if(c[0].adjacentChestZNeg!=null) c[1]=c[0].adjacentChestZNeg;

                for(TileEntityChest inv : c){
                    if(inv==null) break;

                    boolean f=false;
                    int size=inv.getSizeInventory();
                    for(int i=0;i<size;i++){
                        ItemStack itemStack=inv.getStackInSlot(i);
                        if(itemStack!=null){
                            packet=itemStack.copy();
                            packet.stackSize=1;
                            itemStack.stackSize--;
                            if(itemStack.stackSize<=0) inv.setInventorySlotContents(i, null);
                            te.markDirty();
                            f=true;
                            break;
                        }
                    }

                    if(f) break;
                }
            }
            else if(te instanceof IInventory){
                IInventory inv=(IInventory)te;
                int size=inv.getSizeInventory();
                for(int i=0;i<size;i++){
                    ItemStack itemStack=inv.getStackInSlot(i);
                    if(itemStack!=null){
                        packet=itemStack.copy();
                        packet.stackSize=1;
                        itemStack.stackSize--;
                        if(itemStack.stackSize<=0) inv.setInventorySlotContents(i, null);
                        te.markDirty();
                        break;
                    }
                }
            }

            //パケットがあったら転送
            if(packet!=null){
                tem.insertItemStack(packet);
            }
        }
    }
}