-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathTileEntityGeoOre.java
151 lines (129 loc) · 4.32 KB
/
TileEntityGeoOre.java
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
/*******************************************************************************
* @author Reika Kalseki
*
* Copyright 2017
*
* All rights reserved.
* Distribution of the software in any form is only allowed with
* explicit, prior permission from the owner.
******************************************************************************/
package Reika.GeoStrata;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
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.util.IIcon;
import Reika.DragonAPI.Interfaces.Registry.OreType;
import Reika.DragonAPI.Libraries.Registry.ReikaOreHelper;
import Reika.DragonAPI.ModRegistry.ModOreList;
import Reika.GeoStrata.Registry.RockShapes;
import Reika.GeoStrata.Registry.RockTypes;
public class TileEntityGeoOre extends TileEntity {
private RockTypes type;
private ReikaOreHelper ore;
private ModOreList modore;
private Block block;
private int metadata;
@Override
public boolean canUpdate() {
return false;
}
public void initialize(RockTypes rock, Block b, int meta) {
type = rock;
ItemStack is = new ItemStack(b, 1, meta);
ore = ReikaOreHelper.getFromVanillaOre(b);
if (ore == null)
ore = ReikaOreHelper.getEntryByOreDict(is);
modore = ModOreList.getModOreFromOre(is);
block = b;
metadata = meta;
}
public RockTypes getType() {
return type != null ? type : RockTypes.SHALE;
}
public IIcon getOreIcon(int side) {
return block.getIcon(worldObj, xCoord, yCoord, zCoord, side);
}
public Block getOreBlock() {
return block != null ? block : type != null ? type.getID(RockShapes.SMOOTH) : Blocks.stone;
}
public int getOreMeta() {
return metadata;
}
@Override
public void writeToNBT(NBTTagCompound NBT) {
super.writeToNBT(NBT);
NBT.setInteger("type", type.ordinal());
if (ore != null)
NBT.setInteger("ore", ore.ordinal());
if (modore != null)
NBT.setInteger("more", modore.ordinal());
NBT.setInteger("orem", metadata);
NBT.setInteger("blockid", Block.getIdFromBlock(block));
}
@Override
public void readFromNBT(NBTTagCompound NBT) {
super.readFromNBT(NBT);
type = RockTypes.rockList[NBT.getInteger("type")];
modore = NBT.hasKey("more") ? ModOreList.oreList[NBT.getInteger("more")] : null;
ore = NBT.hasKey("ore") ? ReikaOreHelper.oreList[NBT.getInteger("ore")] : null;
metadata = NBT.getInteger("orem");
block = Block.getBlockById(NBT.getInteger("blockid"));
if (block == null || block == Blocks.air) { //in case of NBT failure
this.calculateBlock();
}
}
private void calculateBlock() {
if (ore != null) {
block = ore.getOreBlockInstance();
}
else if (modore != null) {
ItemStack is = modore.getFirstOreBlock();
block = Block.getBlockFromItem(is.getItem());
metadata = is.getItemDamage();
}
}
/** Single char names to minimize packet size */
private void writeToPacket(NBTTagCompound NBT) {
NBT.setInteger("r", this.getType().ordinal());
if (ore != null)
NBT.setInteger("a", ore.ordinal());
if (modore != null)
NBT.setInteger("b", modore.ordinal());
NBT.setInteger("m", metadata);
NBT.setInteger("i", Block.getIdFromBlock(block));
}
private void readFromPacket(NBTTagCompound NBT) {
type = RockTypes.rockList[NBT.getInteger("r")];
modore = NBT.hasKey("b") ? ModOreList.oreList[NBT.getInteger("b")] : null;
ore = NBT.hasKey("a") ? ReikaOreHelper.oreList[NBT.getInteger("a")] : null;
metadata = NBT.getInteger("m");
int id = NBT.getInteger("i");
block = Block.getBlockById(id);
if (block == null || block == Blocks.air) { //in case of NBT failure
this.calculateBlock();
}
}
@Override
public Packet getDescriptionPacket() {
NBTTagCompound NBT = new NBTTagCompound();
this.writeToPacket(NBT);
S35PacketUpdateTileEntity pack = new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, NBT);
return pack;
}
@Override
public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity p) {
this.readFromPacket(p.field_148860_e);
}
public OreType getOreType() {
return modore != null ? modore : ore != null ? ore : null;
}
@Override
public String toString() {
return type+" "+block+":"+metadata+" ("+ore+" & "+modore+")";
}
}