-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
FishingTrait.java
323 lines (288 loc) · 9.8 KB
/
FishingTrait.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package com.denizenscript.denizen.npc.traits;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.interfaces.FishingHelper;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.util.PlayerAnimation;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.projectiles.ProjectileSource;
import org.bukkit.util.Vector;
import java.util.ArrayList;
public class FishingTrait extends Trait {
@Persist("fishing")
private boolean fishing = false;
@Persist("catch type")
private FishingHelper.CatchType catchType = FishingHelper.CatchType.NONE;
@Persist("fishing spot")
private Location fishingLocation = null;
ArrayList<Location> available = new ArrayList<>();
Location fishingSpot = null;
FishHook fishHook = null;
Item fish = null;
@Persist("catch chance")
int catchPercent = 65;
int reelCount = 100;
int castCount = 0;
@Override
public void run() {
reelCount++;
castCount++;
if (fish != null) {
if (fish.getLocation().distance(npc.getEntity().getLocation()) < 3) {
try {
fish.remove();
}
catch (Exception e) {
}
}
}
if (!fishing) {
return;
}
if (reelCount >= 400) {
reel();
reelCount = 0;
castCount = 325;
}
if (castCount >= 400) {
cast();
castCount = 0;
}
}
// <--[action]
// @Actions
// start fishing
//
// @Triggers when the NPC starts fishing.
//
// @Context
// None
//
// -->
/**
* Makes the NPC fish at the specified location
* <p/>
* TODO Reimplement variance, so each cast doesn't land in the exact same spot.
*
* @param location the location to fish at
*/
public void startFishing(Location location) {
DenizenAPI.getDenizenNPC(npc).action("start fishing", null);
fishingLocation = location.clone();
cast();
fishing = true;
}
// <--[action]
// @Actions
// stop fishing
//
// @Triggers when the NPC stops fishing.
//
// @Context
// None
//
// -->
/**
* Makes the stop fishing.
*/
public void stopFishing() {
DenizenAPI.getDenizenNPC(npc).action("stop fishing", null);
reel();
reelCount = 100;
castCount = 0;
fishingLocation = null;
fishing = false;
}
public boolean scanForFishSpot(Location near, boolean horizontal) {
Block block = near.getBlock();
if (block.getType() == Material.WATER) {
fishingLocation = near;
return true;
}
else if (block.getRelative(BlockFace.DOWN).getType() == Material.WATER) {
fishingLocation = near.add(0, -1, 0);
return true;
}
else if (block.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN).getType() == Material.WATER) {
fishingLocation = near.add(0, -2, 0);
return true;
}
if (horizontal) {
return scanForFishSpot(near.clone().add(1, 0, 0), false)
|| scanForFishSpot(near.clone().add(-1, 0, 0), false)
|| scanForFishSpot(near.clone().add(0, 0, 1), false)
|| scanForFishSpot(near.clone().add(0, 0, -1), false);
}
return false;
}
public void startFishing() {
fishing = true;
Location search = npc.getEntity().getLocation().clone();
Vector direction = npc.getEntity().getLocation().getDirection().clone();
if (direction.getY() > -0.1) {
direction.setY(-0.1);
}
for (int i = 0; i < 10; i++) {
search.add(direction.clone());
if (scanForFishSpot(search, true)) {
break;
}
}
}
// <--[action]
// @Actions
// cast fishing rod
//
// @Triggers when the NPC casts a fishing rod.
//
// @Context
// None
//
// -->
private void cast() {
DenizenAPI.getDenizenNPC(npc).action("cast fishing rod", null);
if (fishingLocation == null) {
Debug.echoError("Fishing location not found!");
return;
}
double v = 34;
double g = 20;
Location from = npc.getEntity().getLocation();
from = from.add(0, .33, 0);
Location to = fishingLocation;
Vector test = to.clone().subtract(from).toVector();
double elev = test.getY();
Double testAngle = launchAngle(from, to, v, elev, g);
if (testAngle == null) {
return;
}
double hangtime = hangtime(testAngle, v, elev, g);
Vector victor = to.clone().subtract(from).toVector();
double dist = Math.sqrt(Math.pow(victor.getX(), 2) + Math.pow(victor.getZ(), 2));
elev = victor.getY();
if (dist == 0) {
return;
}
Double launchAngle = launchAngle(from, to, v, elev, g);
if (launchAngle == null) {
return;
}
victor.setY(Math.tan(launchAngle) * dist);
victor = normalizeVector(victor);
v = v + (.5 * Math.pow(hangtime, 2));
//Random rand = new Random(1234);
v = v + (CoreUtilities.getRandom().nextDouble() - .8) / 2;
victor = victor.multiply(v / 20.0);
if (npc.getEntity() instanceof Player) {
fishHook = NMSHandler.getFishingHelper().spawnHook(from, (Player) npc.getEntity());
fishHook.setShooter((ProjectileSource) npc.getEntity());
fishHook.setVelocity(victor);
PlayerAnimation.ARM_SWING.play((Player) npc.getEntity());
}
}
// <--[action]
// @Actions
// reel in fishing rod
//
// @Triggers when the NPC reels in its fishing rod.
//
// @Context
// None
//
// -->
private void reel() {
DenizenAPI.getDenizenNPC(npc).action("reel in fishing rod", null);
int chance = (int) (Math.random() * 100);
if (fishHook != null && fishHook.isValid()) {
fishHook.remove();
fishHook = null;
}
if (catchPercent > chance && fishHook != null && catchType != FishingHelper.CatchType.NONE) {
try {
fish.remove();
}
catch (Exception e) {
}
Location location = fishHook.getLocation();
ItemStack result = NMSHandler.getFishingHelper().getResult(fishHook, catchType);
if (result != null) {
fish = location.getWorld().dropItem(location, result);
Location npcLocation = npc.getEntity().getLocation();
double d5 = npcLocation.getX() - location.getX();
double d6 = npcLocation.getY() - location.getY();
double d7 = npcLocation.getZ() - location.getZ();
double d8 = Math.sqrt(d5 * d5 + d6 * d6 + d7 * d7);
double d9 = 0.1D;
fish.setVelocity(new Vector(d5 * d9, d6 * d9 + Math.sqrt(d8) * 0.08D, d7 * d9));
}
DenizenAPI.getDenizenNPC(npc).action("catch fish", null);
}
if (npc.getEntity() instanceof Player) {
PlayerAnimation.ARM_SWING.play((Player) npc.getEntity());
}
}
/**
* Checks if the NPC is currently fishing
*
* @return boolean
*/
public boolean isFishing() {
return fishing;
}
/**
* Gets the location the NPC is casting to
* Returns null if the NPC isn't fishing
*
* @return Location
*/
public Location getFishingLocation() {
return fishingLocation;
}
public FishingTrait() {
super("fishing");
}
public static Double launchAngle(Location from, Location to, double v, double elev, double g) {
Vector victor = from.clone().subtract(to).toVector();
double dist = Math.sqrt(Math.pow(victor.getX(), 2) + Math.pow(victor.getZ(), 2));
double v2 = Math.pow(v, 2);
double v4 = Math.pow(v, 4);
double derp = g * (g * Math.pow(dist, 2) + 2 * elev * v2);
if (v4 < derp) {
return null;
}
else {
return Math.atan((v2 - Math.sqrt(v4 - derp)) / (g * dist));
}
}
public static double hangtime(double launchAngle, double v, double elev, double g) {
double a = v * Math.sin(launchAngle);
double b = -2 * g * elev;
if (Math.pow(a, 2) + b < 0) {
return 0;
}
return (a + Math.sqrt(Math.pow(a, 2) + b)) / g;
}
public static Vector normalizeVector(Vector victor) {
double mag = Math.sqrt(Math.pow(victor.getX(), 2) + Math.pow(victor.getY(), 2) + Math.pow(victor.getZ(), 2));
if (mag != 0) {
return victor.multiply(1 / mag);
}
return victor.multiply(0);
}
public void setCatchType(FishingHelper.CatchType catchType) {
this.catchType = catchType;
}
public void setCatchPercent(int catchPercent) {
this.catchPercent = catchPercent;
}
}