Skip to content

Commit

Permalink
Remove duplications.
Browse files Browse the repository at this point in the history
  • Loading branch information
lewmilburn committed Oct 4, 2024
1 parent 1c5d875 commit bda7d75
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package net.lewmc.essence.commands.admin;

import net.lewmc.essence.Essence;
import net.lewmc.essence.utils.CommandUtil;
import net.lewmc.essence.utils.MessageUtil;
import net.lewmc.essence.utils.PermissionHandler;
import net.lewmc.essence.utils.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -33,12 +29,12 @@ public boolean onCommand(
@NotNull String s,
String[] args
) {
if (!(commandSender instanceof Player)) {
commandSender.sendMessage("Only players can use this command.");
if (!(commandSender instanceof Player player)) {
LogUtil log = new LogUtil(this.plugin);
log.noConsole();
return true;
}

Player player = (Player) commandSender;
MessageUtil message = new MessageUtil(commandSender, plugin);
PermissionHandler permission = new PermissionHandler(commandSender, message);

Expand All @@ -48,14 +44,8 @@ public boolean onCommand(
return cmd.disabled(message);
}

if (permission.has("essence.admin.invisible")) {
// Apply invisibility
player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1));
message.send("visibility", "invisible", new String[]{player.getName()});
return true;
} else {
return permission.not();
}
StatsUtil stats = new StatsUtil(this.plugin, player, permission);
return stats.invisible(true);
}

return false;
Expand Down
25 changes: 6 additions & 19 deletions src/main/java/net/lewmc/essence/commands/admin/VisibleCommand.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package net.lewmc.essence.commands.admin;

import net.lewmc.essence.Essence;
import net.lewmc.essence.utils.CommandUtil;
import net.lewmc.essence.utils.MessageUtil;
import net.lewmc.essence.utils.PermissionHandler;
import net.lewmc.essence.utils.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffectType;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -33,12 +30,12 @@ public boolean onCommand(
@NotNull String s,
String[] args
) {
if (!(commandSender instanceof Player)) {
commandSender.sendMessage("Only players can use this command.");
if (!(commandSender instanceof Player player)) {
LogUtil log = new LogUtil(this.plugin);
log.noConsole();
return true;
}

Player player = (Player) commandSender;
MessageUtil message = new MessageUtil(commandSender, plugin);
PermissionHandler permission = new PermissionHandler(commandSender, message);

Expand All @@ -48,18 +45,8 @@ public boolean onCommand(
return cmd.disabled(message);
}

if (permission.has("essence.admin.visible")) {
// Remove invisibility
if (player.hasPotionEffect(PotionEffectType.INVISIBILITY)) {
player.removePotionEffect(PotionEffectType.INVISIBILITY);
message.send("visibility", "visible", new String[]{player.getName()});
} else {
message.send("visibility", "alreadyvisible", new String[]{player.getName()});
}
return true;
} else {
return permission.not();
}
StatsUtil stats = new StatsUtil(this.plugin, player, permission);
return stats.invisible(false);
}

return false;
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/net/lewmc/essence/utils/StatsUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package net.lewmc.essence.utils;

import net.lewmc.essence.Essence;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

/**
* Manages player statistics.
*/
public class StatsUtil {
private final Player player;
private final PermissionHandler permission;
private final Essence plugin;

public StatsUtil(Essence plugin, Player player, PermissionHandler permission) {
this.plugin = plugin;
this.player = player;
this.permission = permission;
}

/**
* Sets the player to be invisible.
* @param isInvisible boolean - Should the player be invisible (true) or visible (false).
* @return boolean - Was the operation successful?
*/
public boolean invisible(boolean isInvisible) {
if (this.permission.has("essence.admin.invisible")) {
if (isInvisible) {
this.player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1));
MessageUtil message = new MessageUtil(this.player, this.plugin);
message.send("visibility", "invisible", new String[]{this.player.getName()});
} else {
this.player.removePotionEffect(PotionEffectType.INVISIBILITY);
MessageUtil message = new MessageUtil(this.player, this.plugin);
message.send("visibility", "visible", new String[]{this.player.getName()});
}
return true;
} else {
return this.permission.not();
}
}
}

0 comments on commit bda7d75

Please sign in to comment.