Skip to content

Commit 70d99a2

Browse files
authored
Merge pull request #70 from yL3oft/feature/autofish
Add auto fishing (/auto fishing)
2 parents 8c2f11f + 19974e0 commit 70d99a2

File tree

11 files changed

+254
-0
lines changed

11 files changed

+254
-0
lines changed

src/main/java/us/thezircon/play/autopickup/AutoPickup.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.bukkit.plugin.java.JavaPlugin;
1212
import org.bukkit.scheduler.BukkitRunnable;
1313
import us.thezircon.play.autopickup.commands.AutoDrops;
14+
import us.thezircon.play.autopickup.commands.AutoFishingDrops;
1415
import us.thezircon.play.autopickup.commands.AutoPickup.Auto;
1516
import us.thezircon.play.autopickup.commands.AutoSmelt;
1617
import us.thezircon.play.autopickup.listeners.*;
@@ -28,6 +29,7 @@ public final class AutoPickup extends JavaPlugin {
2829

2930
public HashSet<Player> autopickup_list = new HashSet<>(); // Blocks
3031
public HashSet<Player> autopickup_list_mobs = new HashSet<>(); // Mobs
32+
public HashSet<Player> autopickup_list_fishing = new HashSet<>(); // Fish
3133
public HashSet<Player> auto_smelt_blocks = new HashSet<>(); // AutoSmelt - Blocks
3234
public Messages messages = null;
3335
public boolean UP2Date = true;
@@ -133,6 +135,7 @@ public void onEnable() {
133135
getServer().getPluginManager().registerEvents(new EntityDeathEventListener(), this);
134136
getServer().getPluginManager().registerEvents(new PlayerInteractEventListener(), this);
135137
getServer().getPluginManager().registerEvents(new PlayerDropItemEventListener(), this);
138+
getServer().getPluginManager().registerEvents(new PlayerFishEventListener(), this);
136139
getServer().getPluginManager().registerEvents(new ItemSpawnEventListener(), this);
137140
getServer().getPluginManager().registerEvents(new EntityDropItemEventListener(), this);
138141

@@ -144,6 +147,7 @@ public void onEnable() {
144147
// Commands
145148
getCommand("autopickup").setExecutor(new Auto());
146149
getCommand("autodrops").setExecutor(new AutoDrops());
150+
getCommand("autofishingdrops").setExecutor(new AutoFishingDrops());
147151
getCommand("autosmelt").setExecutor(new AutoSmelt());
148152

149153
// Crops by version
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package us.thezircon.play.autopickup.commands;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandExecutor;
5+
import org.bukkit.command.CommandSender;
6+
import org.bukkit.entity.Player;
7+
import us.thezircon.play.autopickup.AutoPickup;
8+
import us.thezircon.play.autopickup.utils.PickupPlayer;
9+
10+
public class AutoFishingDrops implements CommandExecutor {
11+
12+
private static final AutoPickup PLUGIN = AutoPickup.getPlugin(AutoPickup.class);
13+
14+
@Override
15+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
16+
boolean requirePermsAUTO = PLUGIN.getConfig().getBoolean("requirePerms.autopickup");
17+
18+
if (sender instanceof Player) {
19+
Player player = (Player) sender;
20+
if (player.hasPermission("autopickup.pickup.fishing") || (!requirePermsAUTO)) {
21+
toggle(player);
22+
} else {
23+
player.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getNoPerms());
24+
}
25+
}
26+
27+
return false;
28+
29+
}
30+
31+
public static void toggle(Player player) {
32+
PickupPlayer PP = new PickupPlayer(player);
33+
if (PLUGIN.autopickup_list_fishing.contains(player)) {
34+
PLUGIN.autopickup_list_fishing.remove(player);
35+
player.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getMsgAutoFishingdropsDisable());
36+
PP.setEnabledFishing(false);
37+
} else if (!PLUGIN.autopickup_list_fishing.contains(player)) {
38+
PLUGIN.autopickup_list_fishing.add(player);
39+
player.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getMsgAutoFishingdropsEnable());
40+
PP.setEnabledFishing(true);
41+
}
42+
}
43+
}

src/main/java/us/thezircon/play/autopickup/commands/AutoPickup/Auto.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bukkit.entity.Player;
88
import us.thezircon.play.autopickup.AutoPickup;
99
import us.thezircon.play.autopickup.commands.AutoPickup.subcommands.drops;
10+
import us.thezircon.play.autopickup.commands.AutoPickup.subcommands.fishingdrops;
1011
import us.thezircon.play.autopickup.commands.AutoPickup.subcommands.reload;
1112
import us.thezircon.play.autopickup.commands.AutoPickup.subcommands.smelt;
1213
import us.thezircon.play.autopickup.commands.CMDManager;
@@ -24,6 +25,7 @@ public class Auto implements TabExecutor{
2425
public Auto(){
2526
subcommands.add(new reload());
2627
subcommands.add(new drops());
28+
subcommands.add(new fishingdrops());
2729
subcommands.add(new smelt());
2830
}
2931

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package us.thezircon.play.autopickup.commands.AutoPickup.subcommands;
2+
3+
import org.bukkit.command.CommandSender;
4+
import org.bukkit.entity.Player;
5+
import us.thezircon.play.autopickup.AutoPickup;
6+
import us.thezircon.play.autopickup.commands.AutoFishingDrops;
7+
import us.thezircon.play.autopickup.commands.CMDManager;
8+
9+
import java.util.List;
10+
11+
public class fishingdrops extends CMDManager {
12+
13+
private static final AutoPickup PLUGIN = AutoPickup.getPlugin(AutoPickup.class);
14+
15+
@Override
16+
public String getName() {
17+
return "fishingdrops";
18+
}
19+
20+
@Override
21+
public String getDescription() {
22+
return "Picks up fishing drops";
23+
}
24+
25+
@Override
26+
public String getSyntax() {
27+
return "/auto fishingdrops";
28+
}
29+
30+
@Override
31+
public void perform(CommandSender sender, String[] args) {
32+
boolean requirePermsAUTO = PLUGIN.getConfig().getBoolean("requirePerms.autopickup");
33+
34+
if (sender instanceof Player) {
35+
Player player = (Player) sender;
36+
if (player.hasPermission("autopickup.pickup.fishing") || !requirePermsAUTO) {
37+
AutoFishingDrops.toggle(player);
38+
} else {
39+
sender.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getNoPerms());
40+
}
41+
}
42+
}
43+
44+
@Override
45+
public List<String> arg1(Player player, String[] args) {
46+
return null;
47+
}
48+
49+
@Override
50+
public List<String> arg2(Player player, String[] args) {
51+
return null;
52+
}
53+
54+
@Override
55+
public List<String> arg3(Player player, String[] args) {
56+
return null;
57+
}
58+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package us.thezircon.play.autopickup.listeners;
2+
3+
import org.bukkit.Bukkit;
4+
import org.bukkit.Location;
5+
import org.bukkit.entity.Item;
6+
import org.bukkit.entity.Player;
7+
import org.bukkit.event.EventHandler;
8+
import org.bukkit.event.Listener;
9+
import org.bukkit.event.player.PlayerFishEvent;
10+
import org.bukkit.inventory.ItemStack;
11+
import us.thezircon.play.autopickup.AutoPickup;
12+
13+
import java.util.HashMap;
14+
import java.util.List;
15+
16+
public class PlayerFishEventListener implements Listener {
17+
18+
private static final AutoPickup PLUGIN = AutoPickup.getPlugin(AutoPickup.class);
19+
20+
@EventHandler
21+
public void onFish(PlayerFishEvent e) {
22+
Player player = e.getPlayer();
23+
if(!(e.getState().equals(PlayerFishEvent.State.CAUGHT_FISH))) return;
24+
if(!(e.getCaught() instanceof Item)) return;
25+
26+
if (!PLUGIN.autopickup_list_fishing.contains(player)) return;
27+
28+
Bukkit.getScheduler().runTaskAsynchronously(PLUGIN, new Runnable() {
29+
@Override
30+
public void run() {
31+
boolean requirePermsAUTO = PLUGIN.getConfig().getBoolean("requirePerms.autopickup");
32+
if (!requirePermsAUTO) {
33+
return;
34+
}
35+
if (!player.hasPermission("autopickup.pickup.fishing") && !player.hasPermission("autopickup.pickup.fishing.autoenabled")) {
36+
PLUGIN.autopickup_list_fishing.remove(player);
37+
}
38+
}
39+
});
40+
41+
boolean doFullInvMSG = PLUGIN.getConfig().getBoolean("doFullInvMSG");
42+
43+
Location loc = e.getPlayer().getLocation();
44+
if (AutoPickup.worldsBlacklist!=null && AutoPickup.worldsBlacklist.contains(loc.getWorld().getName())) {
45+
return;
46+
}
47+
48+
Bukkit.getScheduler().runTask(PLUGIN, () -> {
49+
Item caught = (Item) e.getCaught();
50+
51+
if (PLUGIN.getBlacklistConf().contains("BlacklistedFishing", true)) {
52+
boolean doBlacklist = PLUGIN.getBlacklistConf().getBoolean("BlacklistedFishing");
53+
List<String> blacklist = PLUGIN.getBlacklistConf().getStringList("BlacklistedFishing");
54+
55+
if (doBlacklist && blacklist.contains(caught.getItemStack().getType().toString())) {
56+
return;
57+
}
58+
}
59+
60+
HashMap<Integer, ItemStack> leftOver = player.getInventory().addItem(caught.getItemStack());
61+
caught.remove();
62+
if (!leftOver.isEmpty()) {
63+
for (ItemStack item : leftOver.values()) {
64+
player.getWorld().dropItemNaturally(loc, item);
65+
}
66+
if (doFullInvMSG) {
67+
long secondsLeft;
68+
long cooldown = 15000; // 15 sec
69+
if (AutoPickup.lastInvFullNotification.containsKey(player.getUniqueId())) {
70+
secondsLeft = (AutoPickup.lastInvFullNotification.get(player.getUniqueId()) / 1000) + cooldown / 1000 - (System.currentTimeMillis() / 1000);
71+
} else {
72+
secondsLeft = 0;
73+
}
74+
if (secondsLeft <= 0) {
75+
player.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getFullInventory());
76+
AutoPickup.lastInvFullNotification.put(player.getUniqueId(), System.currentTimeMillis());
77+
}
78+
}
79+
}
80+
});
81+
}
82+
}

src/main/java/us/thezircon/play/autopickup/listeners/PlayerJoinEventListener.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ public void onJoin(PlayerJoinEvent e) {
5959
}
6060
}
6161

62+
if (player.hasPermission("autopickup.pickup.fishing.autoenabled")) {
63+
if (!PLUGIN.autopickup_list_fishing.contains(player)) {
64+
PLUGIN.autopickup_list_fishing.add(player);
65+
//if (doAutoEnableMSG) {player.sendMessage(PLUGIN.getMsg().getPrefix() + " " + PLUGIN.getMsg().getAutoEnabled());}
66+
}
67+
}
68+
6269
if (player.hasPermission("autopickup.pickup.mined.autosmelt.autoenabled")) {
6370
if (!PLUGIN.auto_smelt_blocks.contains(player)) {
6471
PLUGIN.auto_smelt_blocks.add(player);
@@ -83,6 +90,11 @@ public void onJoin(PlayerJoinEvent e) {
8390
PLUGIN.autopickup_list_mobs.add(player);
8491
}
8592
}
93+
if (PP.getFishDropsToggle() && player.hasPermission("autopickup.pickup.fishing")) {
94+
if (!PLUGIN.autopickup_list_fishing.contains(player)) {
95+
PLUGIN.autopickup_list_fishing.add(player);
96+
}
97+
}
8698
}
8799

88100
@EventHandler

src/main/java/us/thezircon/play/autopickup/utils/Messages.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public String getMsgAutoDropsDisable() {
2323
return msgAutoDropsDisable;
2424
}
2525

26+
public String getMsgAutoFishingdropsEnable() {
27+
return msgAutoFishingDropsEnable;
28+
}
29+
30+
public String getMsgAutoFishingdropsDisable() {
31+
return msgAutoFishingDropsDisable;
32+
}
33+
2634
public String getMsgAutoSmeltEnable() {
2735
return msgAutoSmeltEnable;
2836
}
@@ -33,6 +41,8 @@ public String getMsgAutoSmeltDisable() {
3341

3442
private String msgAutoDropsEnable;
3543
private String msgAutoDropsDisable;
44+
private String msgAutoFishingDropsEnable;
45+
private String msgAutoFishingDropsDisable;
3646
private String msgAutoSmeltEnable;
3747
private String msgAutoSmeltDisable;
3848

@@ -81,6 +91,8 @@ public Messages() {
8191
try {
8292
this.msgAutoDropsEnable = HexFormat.format(PLUGIN.getConfig().getString("msgAutoMobDropsEnable"));
8393
this.msgAutoDropsDisable = HexFormat.format(PLUGIN.getConfig().getString("msgAutoMobDropsDisable"));
94+
this.msgAutoFishingDropsEnable = HexFormat.format(PLUGIN.getConfig().getString("msgAutoFishingDropsEnable"));
95+
this.msgAutoFishingDropsDisable = HexFormat.format(PLUGIN.getConfig().getString("msgAutoFishingDropsDisable"));
8496
this.msgAutoSmeltEnable = HexFormat.format(PLUGIN.getConfig().getString("msgAutoSmeltEnable"));
8597
this.msgAutoSmeltDisable = HexFormat.format(PLUGIN.getConfig().getString("msgAutoSmeltDisable"));
8698
} catch (NullPointerException e) {

src/main/java/us/thezircon/play/autopickup/utils/PickupPlayer.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ public void run() {
9090
}.runTaskLater(plugin, 1);
9191
}
9292

93+
public void setEnabledFishing(boolean e) {
94+
new BukkitRunnable() {
95+
@Override
96+
public void run() {
97+
if (!fileExists()) {createFile();}
98+
99+
File playerFile = new File(plugin.getDataFolder()+File.separator+"PlayerData"+File.separator+uuid+".yml");
100+
FileConfiguration data = YamlConfiguration.loadConfiguration(playerFile);
101+
102+
data.set("enabled_fishing_drops", e);
103+
104+
try {
105+
data.save(playerFile);
106+
} catch (IOException err){
107+
log.warning("[AutoPickup] Unable to update "+uuid+"'s data file.");
108+
}
109+
}
110+
}.runTaskLater(plugin, 1);
111+
}
112+
93113
public void setEnabledAutoSmelt(boolean e) {
94114
new BukkitRunnable() {
95115
@Override
@@ -126,6 +146,16 @@ public boolean getMobDropsToggle(){
126146
}
127147
}
128148

149+
public boolean getFishDropsToggle(){
150+
if (!fileExists()) {createFile();}
151+
FileConfiguration data = YamlConfiguration.loadConfiguration(playerData);
152+
try {
153+
return data.getBoolean("enabled_fishing_drops");
154+
} catch (NullPointerException e) {
155+
return false;
156+
}
157+
}
158+
129159
public boolean getAutoSmeltToggle(){
130160
if (!fileExists()) {createFile();}
131161
FileConfiguration data = YamlConfiguration.loadConfiguration(playerData);

src/main/resources/blacklist.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Blacklisted:
2727
BlacklistedEntities:
2828
- PLAYER
2929

30+
BlacklistedFishing: []
31+
3032
BlacklistedWorlds:
3133
- example
3234

src/main/resources/config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ msgAutoSmeltDisable: "&7You have&c disabled &7auto smelt."
2828
msgAutoMobDropsEnable: "&7You have&a enabled &7auto drops."
2929
msgAutoMobDropsDisable: "&7You have&c disabled &7auto drops."
3030

31+
#Toggle Message - /fishingdrops
32+
msgAutoFishingDropsEnable: "&7You have&a enabled &7auto fishing drops."
33+
msgAutoFishingDropsDisable: "&7You have&c disabled &7auto fishing drops."
34+
3135
#Aditional toggling messages
3236
msgAutoEnable: "&7Auto pickup has been automatically&a enabled&7."
3337
doAutoEnableMSG: true

0 commit comments

Comments
 (0)