refactoring and code cleanup
All checks were successful
Build / build (push) Successful in 1m27s

This commit is contained in:
unurled 2024-02-29 15:44:28 +01:00
parent 7cc9395cbc
commit 1380e7479a
Signed by: unurled
GPG key ID: FDBC9CBE1F82423F
11 changed files with 45 additions and 77 deletions

View file

@ -4,6 +4,7 @@ import me.unurled.sacredrealms.sr.SR;
import me.unurled.sacredrealms.sr.commands.admin.AttributeCommand;
import me.unurled.sacredrealms.sr.commands.player.ResetAdventureCommand;
import me.unurled.sacredrealms.sr.managers.Manager;
import org.bukkit.command.PluginCommand;
public class CommandManager extends Manager {
@ -11,10 +12,17 @@ public class CommandManager extends Manager {
@Override
public void load() {
super.load();
SR.getInstance().getCommand("attributes").setExecutor(new AttributeCommand());
SR.getInstance().getCommand("attributes").setTabCompleter(new AttributeCommand());
SR instance = SR.getInstance();
PluginCommand attributes = instance.getCommand("attributes");
if (attributes != null) {
attributes.setExecutor(new AttributeCommand());
attributes.setTabCompleter(new AttributeCommand());
}
SR.getInstance().getCommand("resetadventure").setExecutor(new ResetAdventureCommand());
SR.getInstance().getCommand("resetadventure").setTabCompleter(new ResetAdventureCommand());
PluginCommand resetadventure = instance.getCommand("resetadventure");
if (resetadventure != null) {
resetadventure.setExecutor(new ResetAdventureCommand());
resetadventure.setTabCompleter(new ResetAdventureCommand());
}
}
}

View file

@ -133,12 +133,19 @@ public class ResetAdventureCommand implements TabExecutor {
if (args[0].isBlank()) {
return Arrays.stream(Bukkit.getOfflinePlayers())
.map(OfflinePlayer::getName)
.toList().stream().limit(10).toList();
.toList()
.stream()
.limit(10)
.toList();
}
return Arrays.stream(Bukkit.getOfflinePlayers())
.map(OfflinePlayer::getName).filter(Objects::nonNull)
.map(OfflinePlayer::getName)
.filter(Objects::nonNull)
.filter(name -> name.toLowerCase().startsWith(args[0].toLowerCase()))
.toList().stream().limit(10).toList();
.toList()
.stream()
.limit(10)
.toList();
}
return null;
}

View file

@ -50,6 +50,7 @@ public enum Attribute {
return minValue;
}
@SuppressWarnings("SameReturnValue")
public PersistentDataType<Double, Double> getType() {
return PersistentDataType.DOUBLE;
}

View file

@ -17,11 +17,13 @@ import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.EntityEquipment;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("EmptyMethod")
public class CombatManager extends Manager {
@EventHandler
public void onDamage(EntityDamageByBlockEvent e) {
public void onDamage(@NotNull EntityDamageByBlockEvent e) {
// TODO: Implement
// blast enchant :shrug:
// fall damage, fire damage, etc
@ -31,7 +33,7 @@ public class CombatManager extends Manager {
// TODO: Finish this
@EventHandler
public void onDamage(EntityDamageByEntityEvent e) {
public void onDamage(@NotNull EntityDamageByEntityEvent e) {
e.setDamage(0.0);
if (!(e.getDamager() instanceof LivingEntity damager)

View file

@ -38,7 +38,7 @@ public class EntityManager extends Manager {
}
@EventHandler
public void onEntitySpawn(EntitySpawnEvent e) {
public void onEntitySpawn(@NotNull EntitySpawnEvent e) {
if (e.getEntity() instanceof Player) return;
if (isSREntity(e.getEntity().getUniqueId())) return;

View file

@ -4,6 +4,7 @@ import java.util.HashMap;
import me.unurled.sacredrealms.sr.managers.Manager;
import org.bukkit.NamespacedKey;
@SuppressWarnings("EmptyMethod")
public class ItemManager extends Manager {
public static final NamespacedKey ID = new NamespacedKey("sr", "id");

View file

@ -1,6 +1,7 @@
package me.unurled.sacredrealms.sr.components.item.abilities;
/** Represents an ability that an item can have. TODO: Implement this class */
@SuppressWarnings("unused")
public class Ability {
private final String name;

View file

@ -40,11 +40,19 @@ public class PlayerManager extends Manager {
return;
}
DataHandler dh = dm.getDataHandler();
}
// save all players
Gson gson =
new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(Inventory.class, new InventorySerializer())
.registerTypeAdapter(PotionEffect.class, new PotionEffectSerializer())
.create();
for (SRPlayer player : players.values()) {
dh.set(PLAYER_KEY + player.getUuid(), gson.toJson(player));
}
/** Load the data */
/*@Override
public void loadData() {}*/
players.clear();
}
@Nullable
public SRPlayer getPlayer(UUID uuid) {
@ -89,7 +97,7 @@ public class PlayerManager extends Manager {
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
public void onPlayerQuit(@NotNull PlayerQuitEvent e) {
SRPlayer player = getPlayer(e.getPlayer().getUniqueId());
if (player == null) {
return;

View file

@ -1,6 +1,5 @@
package me.unurled.sacredrealms.sr.data;
import com.google.gson.Gson;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -14,14 +13,6 @@ public interface DataHandler {
*/
String get(@NotNull String key);
/**
* Get a value from the data source
*
* @param key The key to get the value from
* @return The value of the key
*/
Object getGson(@NotNull String key);
/**
* Set a value in the data source
*
@ -30,23 +21,6 @@ public interface DataHandler {
*/
void set(@NotNull String key, @Nullable String value);
/**
* Set a value in the data source
*
* @param key The key to set the value to
* @param value The value to set
*/
void set(@NotNull String key, Gson value);
/**
* Append a value to the data source
*
* @param key The key to append the value to
* @param path The path to append the value to
* @param json The json to append
*/
void jsonAppend(@NotNull String key, @NotNull String path, @NotNull Object... json);
/**
* Remove a value from the data source
*
@ -56,6 +30,7 @@ public interface DataHandler {
/**
* Check if a key exists in the data source
*
* @param key The key to check
* @return If the key exists
*/

View file

@ -2,11 +2,9 @@ package me.unurled.sacredrealms.sr.data;
import static me.unurled.sacredrealms.sr.utils.Logger.error;
import com.google.gson.Gson;
import me.unurled.sacredrealms.sr.SR;
import org.jetbrains.annotations.NotNull;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.json.Path2;
public class Redis implements DataHandler {
@ -41,17 +39,6 @@ public class Redis implements DataHandler {
return client.get(key);
}
/**
* Get a value from the data source
*
* @param key The key to get the value from
* @return The value of the key
*/
@Override
public Object getGson(@NotNull String key) {
return client.jsonGet(key);
}
/**
* Set a value in the data source
*
@ -63,28 +50,6 @@ public class Redis implements DataHandler {
client.set(key, value);
}
/**
* Set a value in the data source
*
* @param key The key to set the value to
* @param value The value to set
*/
public void set(@NotNull String key, Gson value) {
client.jsonSet(key, value);
}
/**
* Append a value to the data source
*
* @param key The key to append the value to
* @param path The path to append the value to
* @param json The json to append
*/
@Override
public void jsonAppend(@NotNull String key, @NotNull String path, @NotNull Object... json) {
client.jsonArrAppend(key, Path2.of(path), json);
}
/**
* Remove a value from the data source
*

View file

@ -17,6 +17,7 @@ import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("UnusedReturnValue")
public class Items {
public static @NotNull Double getTotalAttribute(Player player, Attribute attribute) {
return getTotalAttribute(player.getEquipment(), attribute);
@ -63,7 +64,6 @@ public class Items {
* <dark_purple>Enchantments: <blue> Enchantment <if max level gold>1 <blue> Enchantment <if max
* level gold>2
*
* @param item the item
* @param stack the itemstack
* @return the itemstack with lore
*/