Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
assada committed Jan 10, 2022
1 parent e2e9e20 commit 3938e92
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 39 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dead.guru</groupId>
<artifactId>ItemDatabaseLink</artifactId>
<version>1.2-SNAPSHOT</version>
<version>1.3-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/idl/AbstractItemNotifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package idl;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

abstract class AbstractItemNotifier {
private final ItemChecker checker;
private final FileConfiguration config;

public AbstractItemNotifier(ItemChecker checker, FileConfiguration config) {
this.checker = checker;
this.config = config;
}

public void handle(Player player) {
int newItems = this.checker.check(player);
if (newItems > 0) {
player.sendMessage(ChatColor.DARK_GREEN + "[" + config.getString("general.chatPrefix") + ChatColor.DARK_GREEN + "]" + ChatColor.GREEN + " Got " + ChatColor.WHITE + newItems + ChatColor.GREEN + " new items for you. Use " + ChatColor.YELLOW + "/get" + ChatColor.GREEN + " command to get it!");
}
}
}
13 changes: 3 additions & 10 deletions src/main/java/idl/ItemCheckTask.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
package idl;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.Collection;

public class ItemCheckTask implements Runnable {
private ItemChecker checker;
private FileConfiguration config;
public class ItemCheckTask extends AbstractItemNotifier implements Runnable {

public ItemCheckTask(ItemChecker checker, FileConfiguration config) {
this.checker = checker;
this.config = config;
super(checker, config);
}

@Override
public void run() {
Collection<? extends Player> players = Bukkit.getServer().getOnlinePlayers();
for (Player player : players) {
int newItems = this.checker.check(player);
if (newItems > 0) {
player.sendMessage(ChatColor.DARK_GREEN + "[" + config.getString("general.chatPrefix") + ChatColor.DARK_GREEN + "]" + ChatColor.GREEN + " Got " + ChatColor.WHITE + newItems + ChatColor.GREEN + " new items for you. Use " + ChatColor.YELLOW + "/get" + ChatColor.GREEN + " command to get it!");
}
this.handle(player.getPlayer());
}
}
}
18 changes: 4 additions & 14 deletions src/main/java/idl/JoinListener.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
package idl;

import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

public class JoinListener implements Listener {
private final ItemChecker checker;
private FileConfiguration config;

public JoinListener(ItemChecker itemChecker, FileConfiguration config) {
this.checker = itemChecker;
this.config = config;
public class JoinListener extends AbstractItemNotifier implements Listener {
public JoinListener(ItemChecker checker, FileConfiguration config) {
super(checker, config);
}

@EventHandler
public void onAuth(PlayerJoinEvent event) {
Player player = event.getPlayer();
int newItems = this.checker.check(player);
if (newItems > 0) {
player.sendMessage(ChatColor.DARK_GREEN + "[" + config.getString("general.chatPrefix") + ChatColor.DARK_GREEN + "]" + ChatColor.GREEN + " Got " + ChatColor.WHITE + newItems + ChatColor.GREEN + " new items for you. Use " + ChatColor.YELLOW + "/get" + ChatColor.GREEN + " command to get it!");
}
this.handle(event.getPlayer());
}
}
16 changes: 3 additions & 13 deletions src/main/java/idl/LoginListener.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
package idl;

import fr.xephi.authme.events.LoginEvent;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

public class LoginListener implements Listener {
private final ItemChecker checker;
private FileConfiguration config;

public class LoginListener extends AbstractItemNotifier implements Listener {
public LoginListener(ItemChecker checker, FileConfiguration config) {
this.checker = checker;
this.config = config;
super(checker, config);
}

@EventHandler
public void onAuth(LoginEvent event) {
Player player = event.getPlayer();
int newItems = this.checker.check(player);
if (newItems > 0) {
player.sendMessage(ChatColor.DARK_GREEN + "[" + config.getString("general.chatPrefix") + ChatColor.DARK_GREEN + "]" + ChatColor.GREEN + " Got " + ChatColor.WHITE + newItems + ChatColor.GREEN + " new items for you. Use " + ChatColor.YELLOW + "/get" + ChatColor.GREEN + " command to get it!");
}
this.handle(event.getPlayer());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: ItemDatabaseLink # the plugins name as it should appear in the plugin list /pl
version: 1.2 # the plugin's version
version: 1.3 # the plugin's version
author: Assada
main: idl.Main
api-version: 1.18 # the version of the API you want to use, required starting with 1.13
Expand Down

0 comments on commit 3938e92

Please sign in to comment.