-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
FishingHelperImpl.java
166 lines (150 loc) · 6.78 KB
/
FishingHelperImpl.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
package com.denizenscript.denizen.nms.v1_18.helpers;
import com.denizenscript.denizen.nms.interfaces.FishingHelper;
import com.denizenscript.denizen.nms.v1_18.ReflectionMappingsInfo;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.projectile.FishingHook;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.storage.loot.BuiltInLootTables;
import net.minecraft.world.level.storage.loot.LootContext;
import net.minecraft.world.level.storage.loot.LootTables;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
import net.minecraft.world.phys.Vec3;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_18_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_18_R1.entity.CraftFishHook;
import org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_18_R1.inventory.CraftItemStack;
import org.bukkit.entity.FishHook;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;
import java.lang.reflect.Field;
import java.util.List;
public class FishingHelperImpl implements FishingHelper {
@Override
public org.bukkit.inventory.ItemStack getResult(FishHook fishHook, CatchType catchType) {
ItemStack result = null;
FishingHook nmsHook = ((CraftFishHook) fishHook).getHandle();
if (catchType == CatchType.DEFAULT) {
float f = ((CraftWorld) fishHook.getWorld()).getHandle().random.nextFloat();
int i = EnchantmentHelper.getMobLooting(nmsHook.getPlayerOwner());
int j = EnchantmentHelper.getEnchantmentLevel(Enchantments.FISHING_LUCK, nmsHook.getPlayerOwner());
float f1 = 0.1F - (float) i * 0.025F - (float) j * 0.01F;
float f2 = 0.05F + (float) i * 0.01F - (float) j * 0.01F;
f1 = Mth.clamp(f1, 0.0F, 1.0F);
f2 = Mth.clamp(f2, 0.0F, 1.0F);
if (f < f1) {
result = catchRandomJunk(nmsHook);
}
else {
f -= f1;
if (f < f2) {
result = catchRandomTreasure(nmsHook);
}
else {
result = catchRandomFish(nmsHook);
}
}
}
else if (catchType == CatchType.JUNK) {
result = catchRandomJunk(nmsHook);
}
else if (catchType == CatchType.TREASURE) {
result = catchRandomTreasure(nmsHook);
}
else if (catchType == CatchType.FISH) {
result = catchRandomFish(nmsHook);
}
if (result != null) {
return CraftItemStack.asBukkitCopy(result);
}
else {
return null;
}
}
public ItemStack getRandomReward(FishingHook hook, ResourceLocation key) {
ServerLevel worldServer = (ServerLevel) hook.level;
LootContext.Builder playerFishEvent2 = new LootContext.Builder(worldServer);
LootTables registry = ((ServerLevel) hook.level).getServer().getLootTables();
// registry.getLootTable(key).getLootContextParameterSet()
LootContext info = playerFishEvent2.withOptionalParameter(LootContextParams.ORIGIN, new Vec3(hook.getX(), hook.getY(), hook.getZ()))
.withOptionalParameter(LootContextParams.TOOL, new ItemStack(Items.FISHING_ROD)).create(LootContextParamSets.FISHING);
List<ItemStack> itemStacks = registry.get(key).getRandomItems(info);
return itemStacks.get(worldServer.random.nextInt(itemStacks.size()));
}
@Override
public FishHook spawnHook(Location location, Player player) {
ServerLevel nmsWorld = ((CraftWorld) location.getWorld()).getHandle();
FishingHook hook = new FishingHook(((CraftPlayer) player).getHandle(), nmsWorld, 0, 0);
nmsWorld.addEntity(hook, CreatureSpawnEvent.SpawnReason.CUSTOM);
return (FishHook) hook.getBukkitEntity();
}
private ItemStack catchRandomJunk(FishingHook fishHook) {
return getRandomReward(fishHook, BuiltInLootTables.FISHING_JUNK);
}
private ItemStack catchRandomTreasure(FishingHook fishHook) {
return getRandomReward(fishHook, BuiltInLootTables.FISHING_TREASURE);
}
private ItemStack catchRandomFish(FishingHook fishHook) {
return getRandomReward(fishHook, BuiltInLootTables.FISHING_FISH);
}
public static Field FISHING_HOOK_NIBBLE_SETTER = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_nibble);
public static Field FISHING_HOOK_LURE_TIME_SETTER = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_timeUntilLured);
public static Field FISHING_HOOK_HOOK_TIME_SETTER = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_timeUntilHooked);
@Override
public FishHook getHookFrom(Player player) {
FishingHook hook = ((CraftPlayer) player).getHandle().fishing;
if (hook == null) {
return null;
}
return (FishHook) hook.getBukkitEntity();
}
@Override
public void setNibble(FishHook hook, int ticks) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
try {
FISHING_HOOK_NIBBLE_SETTER.setInt(nmsEntity, ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
@Override
public void setHookTime(FishHook hook, int ticks) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
try {
FISHING_HOOK_HOOK_TIME_SETTER.setInt(nmsEntity, ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
@Override
public int getLureTime(FishHook hook) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
try {
return FISHING_HOOK_LURE_TIME_SETTER.getInt(nmsEntity);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
return -1;
}
@Override
public void setLureTime(FishHook hook, int ticks) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
try {
FISHING_HOOK_LURE_TIME_SETTER.setInt(nmsEntity, ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
}
}
}