Skip to content

Commit

Permalink
Implement Fast Fishing option
Browse files Browse the repository at this point in the history
  • Loading branch information
freneticfeline committed Dec 26, 2017
1 parent c54b566 commit c7a4a35
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.12-1.5
========
Added Fast Fishing option for Single Player
Mod compatibility improvements (Thanks BoxOfFlex)

1.12-1.4
========
Added configuration option for recast delay
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {
apply plugin: 'net.minecraftforge.gradle.forge'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.

version = "1.12-1.4"
version = "1.12-1.5"
group= "net.unladenswallow.minecraft.autofish" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "mod_autofish_forge"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.entity.projectile.EntityFishHook;
import net.minecraft.init.Items;
import net.minecraft.item.ItemFishingRod;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
Expand Down Expand Up @@ -42,6 +42,9 @@ public class AutoFishEventHandler {
the movement method of detection. */
private static final double MOTION_Y_THRESHOLD = -0.05d;

/** The number of ticks to set as the "catchable delay" when Fast Fishing is enabled. */
private static final int FAST_FISH_CATCHABLE_DELAY_TICKS = 42;

public AutoFishEventHandler() {
this.minecraft = FMLClientHandler.instance().getClient();
}
Expand Down Expand Up @@ -75,6 +78,10 @@ public void onClientTickEvent(ClientTickEvent event) {
startFishing();
}

if (ModAutoFish.config_autofish_fastFishing && playerHookInWater() && !isDuringReelDelay()) {
triggerBite();
}

} else {
this.isFishing = false;
}
Expand Down Expand Up @@ -150,7 +157,7 @@ private boolean playerIsHoldingRod() {
ItemStack heldItem = this.player.getHeldItemMainhand();

return (heldItem != null
&& heldItem.getItem() == Items.FISHING_ROD
&& heldItem.getItem() instanceof ItemFishingRod
&& heldItem.getItemDamage() <= heldItem.getMaxDamage());
}

Expand Down Expand Up @@ -195,6 +202,27 @@ private boolean isFishBiting_fromServerEntity(EntityPlayer serverPlayerEntity) t
}
return false;
}

private void triggerBite() {
EntityPlayer serverPlayerEntity = getServerPlayerEntity();
if (serverPlayerEntity != null) {
/*
* If we are single player and have access to the server player entity, try to hack the fish hook entity
* to make fish bite sooner.
*/
EntityFishHook serverFishEntity = serverPlayerEntity.fishEntity;
try {
int currentTicksCatchableDelay = getPrivateIntFieldFromObject(serverFishEntity, "ticksCatchableDelay", "field_146038_az");
if (currentTicksCatchableDelay == 0) {
try {
setPrivateIntFieldOfObject(serverFishEntity, "ticksCatchableDelay", "field_146038_az", FAST_FISH_CATCHABLE_DELAY_TICKS);
} catch (Exception e) {
}
}
} catch (Exception e) {
}
}
}

/**
* Using Java reflection APIs, access a private member data of type int
Expand All @@ -220,6 +248,27 @@ private int getPrivateIntFieldFromObject(Object object, String forgeFieldName, S

}

/**
* Using Java reflection APIs, set a private member data of type int
*
* @param object The target object
* @param fieldName The name of the private data field in object
* @param value The int value to set the private member data from object with fieldName
*
*/
private void setPrivateIntFieldOfObject(Object object, String forgeFieldName, String vanillaFieldName, int value) throws NoSuchFieldException, SecurityException, NumberFormatException, IllegalArgumentException, IllegalAccessException {
Field targetField = null;
try {
targetField = object.getClass().getDeclaredField(forgeFieldName);
} catch (NoSuchFieldException e) {
targetField = object.getClass().getDeclaredField(vanillaFieldName);
}
if (targetField != null) {
targetField.setAccessible(true);
targetField.set(object, value);
}
}

private EntityPlayer getServerPlayerEntity() {
if (this.minecraft.getIntegratedServer() == null || this.minecraft.getIntegratedServer().getEntityWorld() == null) {
return null;
Expand All @@ -244,7 +293,7 @@ private void tryToSwitchRods() {
for (int i = 0; i < 9; i++) {
ItemStack curItemStack = inventory.mainInventory.get(i);
if (curItemStack != null
&& curItemStack.getItem() == Items.FISHING_ROD
&& curItemStack.getItem() instanceof ItemFishingRod
&& (!ModAutoFish.config_autofish_preventBreak || (curItemStack.getMaxDamage() - curItemStack.getItemDamage() > AUTOFISH_BREAKPREVENT_THRESHOLD))
) {
inventory.currentItem = i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ModAutoFish {
public static final boolean CONFIG_DEFAULT_AUTOFISH_ENTITYCLEARPROTECT = false;
public static int config_autofish_recastDelay;
public static final int CONFIG_DEFAULT_AUTOFISH_RECASTDELAY = 2;
public static boolean config_autofish_fastFishing;
public static final boolean CONFIG_DEFAULT_AUTOFISH_FASTFISHING = false;

@SidedProxy(clientSide="net.unladenswallow.minecraft.autofish.ClientProxy", serverSide="net.unladenswallow.minecraft.autofish.ServerProxy")
public static CommonProxy proxy;
Expand Down Expand Up @@ -50,6 +52,7 @@ public static void syncConfig() {
config_autofish_preventBreak = configFile.getBoolean("Enable Break Protection", Configuration.CATEGORY_GENERAL, CONFIG_DEFAULT_AUTOFISH_PREVENTBREAK, "Stop fishing or switch to a new rod before the current rod breaks.");
config_autofish_entityClearProtect = configFile.getBoolean("Enable Enity Clear Protection", Configuration.CATEGORY_GENERAL, CONFIG_DEFAULT_AUTOFISH_ENTITYCLEARPROTECT, "Re-cast after the server clears entities. EXPERIMENTAL");
config_autofish_recastDelay = configFile.getInt("Re-Cast Delay", Configuration.CATEGORY_GENERAL, CONFIG_DEFAULT_AUTOFISH_RECASTDELAY, 1, 10, "Time (in seconds) to wait before automatically re-casting. Increase this value if server lag causes re-casting to fail.");
config_autofish_fastFishing = configFile.getBoolean("Fast Fishing", Configuration.CATEGORY_GENERAL, CONFIG_DEFAULT_AUTOFISH_FASTFISHING, "(Single Player Only) Fish will bite right after casting.");

if (configFile.hasChanged()) {
configFile.save();
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"url": "",
"updateUrl": "",
"authorList": ["FreneticFeline"],
"credits": "troyboy50 for the original LiteMod Autofish; The Forge and FML team; SpacyNG; Maaiins",
"credits": "troyboy50 for the original LiteMod Autofish; The Forge and FML team; SpacyNG; Maaiins; BoxOfFlex",
"logoFile": "",
"screenshots": [],
"dependencies": []
Expand Down

0 comments on commit c7a4a35

Please sign in to comment.