/
BaseType.java
209 lines (189 loc) · 6.76 KB
/
BaseType.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
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
198
199
200
201
202
203
204
205
206
207
208
209
/*
* Copyright (c) 2014, DoubleDoorDevelopment
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.doubledoordev.craycrafting.recipes;
import cpw.mods.fml.common.FMLCommonHandler;
import net.doubledoordev.craycrafting.CrayCrafting;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.CraftingManager;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
/**
* Extend to add new recipe type to the system
* This class registers itself, just make sure you initiate it somewhere.
*
* @param <T>
* @author Dries007
*/
public abstract class BaseType<T extends IRecipe>
{
final Class<T> type;
final protected LinkedList<T> addedList = new LinkedList<>();
final protected LinkedList<T> removedList = new LinkedList<>();
private NBTTagList nbtList = new NBTTagList();
boolean applied;
/**
* Just pass this the class your type wants. Stupid java...
*/
@SuppressWarnings("unchecked")
public BaseType(Class<T> type)
{
this.type = type;
if (!IRecipe.class.isAssignableFrom(type)) throw new IllegalArgumentException("Type must be specified and extend IRecipe.");
RecipeRegistry.register((BaseType<IRecipe>) this);
}
/**
* Don't miss private fields...
* Used on server only.
*/
public abstract NBTTagCompound getNBTFromRecipe(T recipe, ItemStack newOutput) throws IllegalAccessException;
/**
* Don't forget to set special properties if required.
* Used on both sides.
*/
public abstract T[] getRecipesFromNBT(NBTTagCompound nbtRecipe);
/**
* Check everything EXCEPT the output ItemStack.
* Used on both sides.
*/
public abstract boolean equalsExceptOutput(T recipe1, T recipe2) throws IllegalAccessException;
/**
* Must be unique!
* Used on both sides.
*/
public String getTypeName()
{
return type.getSimpleName();
}
/**
* Replaces an instanceof check
* Used on both sides.
*/
public boolean accept(IRecipe recipe)
{
return type.isAssignableFrom(recipe.getClass());
}
/**
* Used to read from disk and from the packet send to the server.
* Make sure you don't apply twice!
* <p/>
* Used on both sides.
*/
@SuppressWarnings("unchecked")
public void loadRecipesFromNBT(NBTTagCompound root) throws Exception
{
nbtList = root.getTagList(getTypeName(), 10);
for (int i = 0; i < nbtList.tagCount(); i++)
{
T[] newRecipes = getRecipesFromNBT(nbtList.getCompoundTagAt(i));
Collections.addAll(addedList, newRecipes);
for (IRecipe oldRecipe : (List<IRecipe>) CraftingManager.getInstance().getRecipeList())
{
if (type.isAssignableFrom(oldRecipe.getClass())) // instanceof basically
{
if (equalsExceptOutput(newRecipes[0], (T) oldRecipe))
{
removedList.add((T) oldRecipe);
break;
}
}
}
}
apply();
}
/**
* Used on both sides.
*/
@SuppressWarnings("unchecked")
public void apply()
{
if (!applied)
{
CrayCrafting.instance.logger.info("APPLY " + getTypeName() + "\t Removed " + removedList.size() + "\t & added " + addedList.size() + "\t recipes from " + FMLCommonHandler.instance().getEffectiveSide());
applied = true;
CraftingManager.getInstance().getRecipeList().removeAll(removedList);
CraftingManager.getInstance().getRecipeList().addAll(addedList);
}
}
/**
* Used on client only.
*/
@SuppressWarnings("unchecked")
public void undo()
{
if (applied)
{
CrayCrafting.instance.logger.info("UNDO " + getTypeName() + "\t Removed " + addedList.size() + "\t & added " + removedList.size() + "\t recipes from " + FMLCommonHandler.instance().getEffectiveSide());
applied = false;
CraftingManager.getInstance().getRecipeList().removeAll(addedList);
CraftingManager.getInstance().getRecipeList().addAll(removedList);
addedList.clear();
removedList.clear();
}
}
/**
* Used on server only.
*/
public NBTTagList getNBTList()
{
return nbtList;
}
/**
* Used to clear existing randomized recipes in nbt file data, before adding the new ones.
* <p/>
* Used on server only.
*/
public void resetNBTList()
{
nbtList = new NBTTagList();
}
/**
* Used on server only.
*/
public void applyRandomization(T recipe, ItemStack itemStack)
{
try
{
removedList.add(recipe);
NBTTagCompound nbtRecipe = getNBTFromRecipe(recipe, itemStack);
nbtList.appendTag(nbtRecipe);
Collections.addAll(addedList, getRecipesFromNBT(nbtRecipe));
}
catch (Exception e)
{
CrayCrafting.instance.logger.warn("Error in " + getTypeName() + " (" + recipe + "), adding back the original.");
removedList.remove(recipe);
e.printStackTrace();
}
}
}