summaryrefslogtreecommitdiff
path: root/src/main/java/jp/plusplus/fbs/pottery/TileEntityPottery.java
blob: 08dbda919406ee3393f65bfb8607af37857aea9e (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package jp.plusplus.fbs.pottery;

import com.google.common.collect.ImmutableList;
import jp.plusplus.fbs.api.IPottery;
import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
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.world.biome.BiomeGenBase;
import net.minecraftforge.common.BiomeManager;

/**
 * Created by plusplus_F on 2015/08/26.
 * レンダラのための無機能
 */
public class TileEntityPottery extends TileEntity {
    public static int PROGRESS_MAX=10*20*60*20;

    public IPottery.PotteryState state= IPottery.PotteryState.INVALID_VALUE;
    public IPottery.PotterySize size;
    public IPottery.PotteryGrade grade;
    public byte pattern;
    public boolean hasEffect;

    public int progress;

    public NBTTagCompound dummyNBT;

    public void setData(ItemStack itemStack){
        IPottery ip=(IPottery)((ItemBlock) itemStack.getItem()).field_150939_a;

        NBTTagCompound nbt=itemStack.getTagCompound();
        if(nbt==null) return;

        state=ip.getState(nbt);
        size=ip.getSize(nbt);
        grade=ip.getGrade(nbt);
        pattern=ip.getPattern(nbt);
        hasEffect=ip.hasEffect(nbt);
    }
    public int getMetadata(){
        int meta=(pattern%0xf);
        meta=(meta|((state.getValue())<<6));
        meta=(meta|((size.getValue())<<4));
        meta=(meta|((grade.getValue())<<8));
        return meta;
    }

    public void writePotteryData(NBTTagCompound nbt){
        nbt.setByte("state", state.getValue());
        nbt.setByte("size", size.getValue());
        nbt.setByte("grade", grade.getValue());
        nbt.setByte("pattern", pattern);
        nbt.setBoolean("hasEffect", hasEffect);
    }

    @Override
    public void readFromNBT(NBTTagCompound par1NBTTagCompound){
        super.readFromNBT(par1NBTTagCompound);
        progress=par1NBTTagCompound.getInteger("Progress");

        state= IPottery.PotteryState.Get(par1NBTTagCompound.getByte("pState"));
        size= IPottery.PotterySize.Get(par1NBTTagCompound.getByte("pSize"));
        grade= IPottery.PotteryGrade.Get(par1NBTTagCompound.getByte("pGrade"));
        pattern=par1NBTTagCompound.getByte("pPattern");
        hasEffect=par1NBTTagCompound.getBoolean("pEffect");

        dummyNBT=new NBTTagCompound();
        writePotteryData(dummyNBT);
    }
    @Override
    public void writeToNBT(NBTTagCompound par1NBTTagCompound){
        super.writeToNBT(par1NBTTagCompound);
        par1NBTTagCompound.setInteger("Progress", progress);

        par1NBTTagCompound.setByte("pState", state.getValue());
        par1NBTTagCompound.setByte("pSize", size.getValue());
        par1NBTTagCompound.setByte("pGrade", grade.getValue());
        par1NBTTagCompound.setByte("pPattern", pattern);
        par1NBTTagCompound.setBoolean("pEffect", hasEffect);
    }

    @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 updateEntity() {
        //--------------------------IPotteryを得る-------------------------------
        Block b = getBlockType();
        IPottery ip;
        if (b instanceof IPottery) {
            ip = (IPottery) b;
        } else {
            //なんか妙だったら即破壊
            if(!worldObj.isRemote) worldObj.func_147480_a(xCoord, yCoord, zCoord, true);
            return;
        }

        /*
        //--------------------------Stateを得る-------------------------------
        if (state == IPottery.PotteryState.INVALID_VALUE) {
            state = ip.getState(potteryMetadata);
        }
        */

        //--------------------------乾燥-------------------------------
        if(state== IPottery.PotteryState.MOLDED){
            int base=10;

            //乾燥地帯なら乾燥は早い
            BiomeGenBase bgb=worldObj.getBiomeGenForCoords(xCoord, zCoord);
            ImmutableList<BiomeManager.BiomeEntry> list=BiomeManager.getBiomes(BiomeManager.BiomeType.DESERT);
            for(BiomeManager.BiomeEntry be : list){
                if(bgb==be.biome){
                    base*=2;
                    break;
                }
            }

            //雨が降るバイームでかつ雨なら乾燥は遅くなる
            if(worldObj.isRaining() && bgb.canSpawnLightningBolt()){
                if(yCoord==worldObj.getHeightValue(xCoord, zCoord)){
                    //雨で直接濡れていようものなら・・・
                    base=0;
                }
                else{
                    base/=2;
                }
            }

            if(dummyNBT==null){
                dummyNBT=new NBTTagCompound();
                writePotteryData(dummyNBT);
            }

            progress+=base;
            if(progress>=10*20*ip.getDrySec(dummyNBT)){
                progress=0;

                ItemStack tmp=new ItemStack(getBlockType(), 1, 0);
                ip.setState(tmp, IPottery.PotteryState.DRY);
                state=ip.getState(tmp.getTagCompound());
                markDirty();
                worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
            }
        }

    }
}