-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathPotionsPlus.java
More file actions
281 lines (223 loc) · 10.4 KB
/
Copy pathPotionsPlus.java
File metadata and controls
281 lines (223 loc) · 10.4 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
Copyright (c) 2012, Mushroom Hostage
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 <organization> 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 <COPYRIGHT HOLDER> 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 me.exphc.PotionsPlus;
import java.util.Collections;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.UUID;
import java.util.Iterator;
import java.util.logging.Logger;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Formatter;
import java.lang.Byte;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.io.*;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.*;
import org.bukkit.event.*;
import org.bukkit.event.block.*;
import org.bukkit.event.player.*;
import org.bukkit.event.entity.*;
import org.bukkit.Material.*;
import org.bukkit.material.*;
import org.bukkit.block.*;
import org.bukkit.entity.*;
import org.bukkit.command.*;
import org.bukkit.inventory.*;
import org.bukkit.configuration.*;
import org.bukkit.configuration.file.*;
import org.bukkit.scheduler.*;
import org.bukkit.enchantments.*;
import org.bukkit.potion.*;
import org.bukkit.*;
import net.minecraft.server.CraftingManager;
import org.bukkit.craftbukkit.enchantments.CraftEnchantment;
import org.bukkit.craftbukkit.inventory.CraftItemStack;
class PotionsPlusListener implements Listener {
PotionsPlus plugin;
public PotionsPlusListener(PotionsPlus plugin) {
this.plugin = plugin;
Bukkit.getServer().getPluginManager().registerEvents(this, plugin);
}
// EntityPotion
// List list = Item.POTION.b(this) = MCP Item.potion.getEffects(potionDamage)
// Event only fires if there are any effects:
// if (list != null && !list.isEmpty()) {
/* TODO: needed?
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled=false) //true)
public void onPotionSplash(PotionSplashEvent event) {
plugin.log.info("Splash "+event);
ThrownPotion potion = event.getPotion();
plugin.log.info("pot = " + potion);
Collection<PotionEffect> potionEffects = potion.getEffects();
Collection<LivingEntity> hits = event.getAffectedEntities();
for (LivingEntity hit: hits) {
for (PotionEffect potionEffect: potionEffects) {
plugin.log.info("pe = " + potionEffect);
plugin.log.info(" amp="+potionEffect.getAmplifier());
plugin.log.info(" dur="+potionEffect.getDuration());
plugin.log.info(" type="+potionEffect.getType());
}
}
}
*/
}
public class PotionsPlus extends JavaPlugin implements Listener {
Logger logger = Logger.getLogger("Minecraft");
public void onEnable() {
//new PotionsPlusListener(this);
getConfig().options().copyDefaults(true);
saveConfig();
reloadConfig();
HashMap effectsCache = getEffectsCache();
log("Effects cache size: " + effectsCache.size());
for (Object key: effectsCache.keySet()) {
Object value = effectsCache.get(key);
log("Effects cache: " + key + " = " + value);
}
loadConfig(effectsCache);
}
public void log(String message) {
if (getConfig().getBoolean("verbose")) {
logger.info(message);
}
}
@SuppressWarnings("unchecked")
public void loadConfig(HashMap effectsCache) {
// Bukkit MobEffectList = MCP Potion
// Bukkit MobEffect = MCP PotionEffect
// TODO: call for customized effects
//new net.minecraft.server.MobEffect
// TODO: new effects (add custom to byId[32])
HashMap<String,Integer> mobEffectNames = getMobEffectNames();
List<Map<?,?>> maps = getConfig().getMapList("potions");
for (Map<?,?> map: maps) {
// Get potion name and damage value(s)
List<Integer> damageValues;
if (map.get("potion") instanceof Integer) {
damageValues = new ArrayList<Integer>();
damageValues.add((Integer)map.get("potion"));
} else {
String name = (String)map.get("potion");
damageValues = getConfig().getIntegerList("damageValues."+name);
if (damageValues == null) {
logger.warning("Invalid potion name "+name+", no damage values found in config");
continue;
}
}
// TODO: configurable splash // if (map.get("splash") != null && ((Boolean)map.get("splash")).booleanValue()) {
for (int dv: new ArrayList<Integer>(damageValues)) {
damageValues.add(dv + 16384);
}
//}
// Build native effects list from config
ArrayList<net.minecraft.server.MobEffect> nmsEffectsList = new ArrayList<net.minecraft.server.MobEffect>();
List<List<Object>> effectsList = (List<List<Object>>)map.get("effects");
if (effectsList == null) {
log("No effects listed for "+map.get("potion")+", ignoring");
continue;
}
for (List effectLine: effectsList) {
String effectName = (String)effectLine.get(0);
int amplification = ((Integer)effectLine.get(1)).intValue();
int duration = ((Integer)effectLine.get(2)).intValue();
int effectId = mobEffectNames.get(effectName);
// Apply effects
nmsEffectsList.add(new net.minecraft.server.MobEffect(effectId, duration, amplification));
}
// Apply all effects to all damage values
log("Potion " + map.get("potion"));
for (int damageValue: damageValues) {
effectsCache.put(damageValue, nmsEffectsList);
log("- Adding "+damageValue+" = "+nmsEffectsList);
}
}
}
/** Get map of internal mob effect names (potion.digSpeed) to ids (3) */
HashMap<String,Integer> getMobEffectNames() {
HashMap<String,Integer> map = new HashMap<String,Integer>();
for (int i = 0; i < net.minecraft.server.MobEffectList.byId.length; i += 1) {
if (net.minecraft.server.MobEffectList.byId[i] != null) {
String name = net.minecraft.server.MobEffectList.byId[i].c(); // returns I; = MCP getName()
map.put(name, i);
//map.put(name.replace("potion.", ""), i);
}
}
return map;
}
/** Get localization strings (potion.prefix.*) for potions.
Note: the indexes do NOT correspond to damage values
And not all are used.. or even unused! (there is Clear Potion but no Milky Potion anywhere I can see)
private static final String potionPrefixes[] =
{
"potion.prefix.mundane", "potion.prefix.uninteresting", "potion.prefix.bland", "potion.prefix.clear", "potion.prefix.milky", "potion.prefix.diffuse", "potion.prefix.artless", "potion.prefix.thin", "potion.prefix.awkward", "potion.prefix.flat",
"potion.prefix.bulky", "potion.prefix.bungling", "potion.prefix.buttered", "potion.prefix.smooth", "potion.prefix.suave", "potion.prefix.debonair", "potion.prefix.thick", "potion.prefix.elegant", "potion.prefix.fancy", "potion.prefix.charming",
"potion.prefix.dashing", "potion.prefix.refined", "potion.prefix.cordial", "potion.prefix.sparkling", "potion.prefix.potent", "potion.prefix.foul", "potion.prefix.odorless", "potion.prefix.rank", "potion.prefix.harsh", "potion.prefix.acrid",
"potion.prefix.gross", "potion.prefix.stinky"
};
*/
/* unused in our code, too!
String[] getAppearances() {
try {
Field field = net.minecraft.server.PotionBrewer.class.getDeclaredField("appearances"); // MCP potionPrefixes
field.setAccessible(true);
Object obj = field.get(null);
if (obj instanceof String[]) {
return (String[])obj;
} else {
throw new RuntimeException("Unable to access potion appearances, unexpected type: " + obj);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
*/
/** Get the internal map of potion effects.
ItemPotion has a MCP 'private HashMap effectsCache', Bukkit 'private HashMap a()', public List b(int i)
builds from MCP PotionHelper.getPotionEffects() = Bukkit PotionBrewer
*/
HashMap getEffectsCache() {
try {
Field effectsCacheField = net.minecraft.server.ItemPotion.class.getDeclaredField("a");
effectsCacheField.setAccessible(true);
Object obj = effectsCacheField.get(net.minecraft.server.Item.POTION);
if (obj instanceof HashMap) {
return (HashMap)obj;
} else {
throw new RuntimeException("Unable to access effects cache, unexpected type: " + obj);
}
} catch (Exception e) {
logger.severe("Reflection failed, nothing will work: " + e);
throw new RuntimeException(e);
// TODO: disable plugin
}
}
public void onDisable() {
}
}