Skip to content

Commit

Permalink
Fishing helper cleanup (#2506)
Browse files Browse the repository at this point in the history
* Initial update/cleanup

* Naming fixes

* `getRandomReward` cleanups
  • Loading branch information
tal5 committed Jul 20, 2023
1 parent 9056d1f commit cb612da
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 74 deletions.
Expand Up @@ -13,23 +13,13 @@ enum CatchType {NONE, DEFAULT, JUNK, TREASURE, FISH}

FishHook spawnHook(Location location, Player player);

default FishHook getHookFrom(Player player) {
throw new UnsupportedOperationException();
}
FishHook getHookFrom(Player player);

default void setNibble(FishHook hook, int ticks) {
throw new UnsupportedOperationException();
}
void setNibble(FishHook hook, int ticks);

default void setHookTime(FishHook hook, int ticks) {
throw new UnsupportedOperationException();
}
void setHookTime(FishHook hook, int ticks);

default int getLureTime(FishHook hook) {
throw new UnsupportedOperationException();
}
int getLureTime(FishHook hook);

default void setLureTime(FishHook hook, int ticks) {
throw new UnsupportedOperationException();
}
void setLureTime(FishHook hook, int ticks);
}
Expand Up @@ -2,8 +2,8 @@

import com.denizenscript.denizen.nms.interfaces.FishingHelper;
import com.denizenscript.denizen.nms.v1_20.ReflectionMappingsInfo;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.google.common.collect.Maps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
Expand All @@ -14,11 +14,8 @@
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.LootDataManager;
import net.minecraft.world.level.storage.loot.LootParams;
import net.minecraft.world.level.storage.loot.parameters.LootContextParam;
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;
Expand All @@ -38,56 +35,46 @@ 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);
ItemStack result = switch (catchType) {
case 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) {
yield catchRandomJunk(nmsHook);
}
else {
result = catchRandomFish(nmsHook);
f -= f1;
if (f < f2) {
yield catchRandomTreasure(nmsHook);
}
else {
yield 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;
}
case JUNK -> catchRandomJunk(nmsHook);
case TREASURE -> catchRandomTreasure(nmsHook);
case FISH -> catchRandomFish(nmsHook);
default -> null;
};
return result != null ? CraftItemStack.asBukkitCopy(result) : null;
}

public ItemStack getRandomReward(FishingHook hook, ResourceLocation key) {
ServerLevel worldServer = (ServerLevel) hook.level();
public ItemStack getRandomReward(FishingHook nmsHook, ResourceLocation key) {
ServerLevel nmsWorld = (ServerLevel) nmsHook.level();
Map<LootContextParam<?>, Object> params = Maps.newIdentityHashMap();
params.put(LootContextParams.ORIGIN, new Vec3(hook.getX(), hook.getY(), hook.getZ()));
params.put(LootContextParams.ORIGIN, new Vec3(nmsHook.getX(), nmsHook.getY(), nmsHook.getZ()));
params.put(LootContextParams.TOOL, new ItemStack(Items.FISHING_ROD));
LootParams playerFishEvent2 = new LootParams(worldServer, params, Maps.newHashMap(), 0);
LootDataManager registry = worldServer.getServer().getLootData();
List<ItemStack> itemStacks = registry.getLootTable(key).getRandomItems(playerFishEvent2);
return itemStacks.get(worldServer.random.nextInt(itemStacks.size()));
LootParams nmsLootParams = new LootParams(nmsWorld, params, Maps.newHashMap(), 0);
List<ItemStack> nmsItems = nmsWorld.getServer().getLootData().getLootTable(key).getRandomItems(nmsLootParams);
return nmsItems.get(nmsWorld.random.nextInt(nmsItems.size()));
}

@Override
Expand All @@ -110,24 +97,24 @@ 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, int.class);
public static Field FISHING_HOOK_LURE_TIME_SETTER = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_timeUntilLured, int.class);
public static Field FISHING_HOOK_HOOK_TIME_SETTER = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_timeUntilHooked, int.class);
public static final Field FISHING_HOOK_NIBBLE = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_nibble, int.class);
public static final Field FISHING_HOOK_LURE_TIME = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_timeUntilLured, int.class);
public static final Field FISHING_HOOK_HOOK_TIME = ReflectionHelper.getFields(FishingHook.class).get(ReflectionMappingsInfo.FishingHook_timeUntilHooked, int.class);

@Override
public FishHook getHookFrom(Player player) {
FishingHook hook = ((CraftPlayer) player).getHandle().fishing;
if (hook == null) {
FishingHook nmsHook = ((CraftPlayer) player).getHandle().fishing;
if (nmsHook == null) {
return null;
}
return (FishHook) hook.getBukkitEntity();
return (FishHook) nmsHook.getBukkitEntity();
}

@Override
public void setNibble(FishHook hook, int ticks) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
FishingHook nmsHook = ((CraftFishHook) hook).getHandle();
try {
FISHING_HOOK_NIBBLE_SETTER.setInt(nmsEntity, ticks);
FISHING_HOOK_NIBBLE.setInt(nmsHook, ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
Expand All @@ -136,9 +123,9 @@ public void setNibble(FishHook hook, int ticks) {

@Override
public void setHookTime(FishHook hook, int ticks) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
FishingHook nmsHook = ((CraftFishHook) hook).getHandle();
try {
FISHING_HOOK_HOOK_TIME_SETTER.setInt(nmsEntity, ticks);
FISHING_HOOK_HOOK_TIME.setInt(nmsHook, ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
Expand All @@ -147,9 +134,9 @@ public void setHookTime(FishHook hook, int ticks) {

@Override
public int getLureTime(FishHook hook) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
FishingHook nmsHook = ((CraftFishHook) hook).getHandle();
try {
return FISHING_HOOK_LURE_TIME_SETTER.getInt(nmsEntity);
return FISHING_HOOK_LURE_TIME.getInt(nmsHook);
}
catch (Throwable ex) {
Debug.echoError(ex);
Expand All @@ -159,9 +146,9 @@ public int getLureTime(FishHook hook) {

@Override
public void setLureTime(FishHook hook, int ticks) {
FishingHook nmsEntity = ((CraftFishHook) hook).getHandle();
FishingHook nmsHook = ((CraftFishHook) hook).getHandle();
try {
FISHING_HOOK_LURE_TIME_SETTER.setInt(nmsEntity, ticks);
FISHING_HOOK_LURE_TIME.setInt(nmsHook, ticks);
}
catch (Throwable ex) {
Debug.echoError(ex);
Expand Down

0 comments on commit cb612da

Please sign in to comment.