aboutsummaryrefslogtreecommitdiff
path: root/src/Java/gtPlusPlus/plugin/villagers/tile/TileEntityGenericSpawner.java
blob: 2dbd21dd3b2f8c149516d6277ecf9b6bd1b2f0ed (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package gtPlusPlus.plugin.villagers.tile;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import gtPlusPlus.core.util.reflect.ReflectionUtils;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.MobSpawnerBaseLogic;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityMobSpawner;

public class TileEntityGenericSpawner extends TileEntityMobSpawner {

	/*
	 * Static Variables
	 */

	/** A HashMap storing string names of classes mapping to the actual java.lang.Class type. */
	private static Map nameToClassMap_Ex = new HashMap();
	/** A HashMap storing the classes and mapping to the string names (reverse of nameToClassMap). */
	private static Map classToNameMap_Ex = new HashMap();

	/**
	 * The Mob Spawner Map
	 */
	public static HashMap<Integer, Entity> mSpawners = new HashMap<Integer, Entity>();

	/**
	 * Registers a New Mob Spawner Type
	 * 
	 * @param aID
	 *            - the Spawner type ID
	 * @param aEntity
	 *            - the Entity which you'd like to spawn
	 */
	public static boolean registerNewMobSpawner(int aID, Entity aEntity) {
		int registered = mSpawners.size();
		mSpawners.put(aID, aEntity);
		return mSpawners.size() > registered;
	}

	/*
	 * Instance Variables
	 */

	/**
	 * The {@link Entity} type which spawns.
	 */
	private final Entity mSpawnType;
	private MobSpawnerCustomLogic spawnerLogic;

	/*
	 * Constructors
	 */

	/**
	 * Constructs a new Spawner, based on an existing type registered.
	 * 
	 * @param aID
	 *            - The ID in the {@link mSpawners} map.
	 */
	public TileEntityGenericSpawner(int aID) {
		if (mSpawners.get(aID) != null) {
			mSpawnType = mSpawners.get(aID);
		} else {
			mSpawnType = null;
		}

		// Last thing to Init
		generateLogicObject();
	}

	/**
	 * Constructs a new Spawner, then registers it.
	 * 
	 * @param aID
	 *            - The ID to be used in the {@link mSpawners} map.
	 * @param aEntity
	 *            - The {@link Entity} type which will be spawned.
	 */
	public TileEntityGenericSpawner(int aID, Entity aEntity) {
		if (aEntity != null) {
			mSpawnType = aEntity;
			registerNewMobSpawner(aID, aEntity);
		} else {
			mSpawnType = null;
		}

		// Last thing to Init
		generateLogicObject();
	}

	public final MobSpawnerCustomLogic getLogic() {
		if (spawnerLogic == null || spawnerLogic.getSpawnerWorld() == null) {
			generateLogicObject();
		}
		return spawnerLogic;
	}

	private final void generateLogicObject() {
		spawnerLogic = new MobSpawnerCustomLogic(this);
	}

	@Override
	public void readFromNBT(NBTTagCompound p_145839_1_) {
		if (hasInternalFieldBeenSet()) {
			this.getLogic().readFromNBT(p_145839_1_);
			this.xCoord = p_145839_1_.getInteger("x");
			this.yCoord = p_145839_1_.getInteger("y");
			this.zCoord = p_145839_1_.getInteger("z");
		}
	}

	@Override
	public void writeToNBT(NBTTagCompound p_145841_1_) {
		if (hasInternalFieldBeenSet()) {

			String s = (String) classToNameMap_Ex.get(TileEntity.class);

			if (s == null){
				throw new RuntimeException(this.getClass() + " is missing a mapping! This is a bug!");
			}
			else {
				p_145841_1_.setString("id", s);
				p_145841_1_.setInteger("x", this.xCoord);
				p_145841_1_.setInteger("y", this.yCoord);
				p_145841_1_.setInteger("z", this.zCoord);
			}


			this.getLogic().writeToNBT(p_145841_1_);
		}
	}

	@Override
	public void updateEntity() {




		if (hasInternalFieldBeenSet()) {
			this.getLogic().updateSpawner();
		}
	}

	private boolean isReady = false;
	private long mTicks = 0;

	private boolean hasInternalFieldBeenSet() {
		if (isReady && mTicks % 200 != 0) {
			return true;
		} else {
			try {
				if (nameToClassMap_Ex == null) {
					nameToClassMap_Ex = (Map) ReflectionUtils.getField(getClass(), "nameToClassMap").get(this);
				}
				if (classToNameMap_Ex == null) {
					classToNameMap_Ex = (Map) ReflectionUtils.getField(getClass(), "classToNameMap").get(this);			
				}
				if (nameToClassMap_Ex != null && classToNameMap_Ex != null) {
					isReady = true;
					return true;
				}
				/*Field mInternalLogicField = ReflectionUtils.getField(getClass(), "field_145882_a");
				if (mInternalLogicField != null) {
					MobSpawnerBaseLogic a = (MobSpawnerBaseLogic) mInternalLogicField.get(this);
					if (a != null) {
						ReflectionUtils.setField(this, "field_145882_a", getLogic());
						if (a.equals(getLogic())) {
							isReady = true;
							return true;
						}
					}
				}*/
			} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
			}
			return false;
		}
	}

	/**
	 * Called when a client event is received with the event number and argument,
	 * see World.sendClientEvent
	 */
	@Override
	public boolean receiveClientEvent(int p_145842_1_, int p_145842_2_) {
		return this.spawnerLogic.setDelayToMin(p_145842_1_) ? true : super.receiveClientEvent(p_145842_1_, p_145842_2_);
	}

	@Override
	public MobSpawnerBaseLogic func_145881_a() {
		return this.spawnerLogic;
	}

}