Skip to content

Commit

Permalink
Create empty user for offline players if required
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Jan 13, 2024
1 parent 36f1323 commit 28e23f1
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,10 @@ public void runConsoleCommand(String command) {

@Override
public void runPlayerCommand(User user, String command) {
getServer().dispatchCommand(((BukkitUser) user).getPlayer(), command);
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
getServer().dispatchCommand(player, command);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import dev.aurelium.auraskills.common.user.User;
import dev.aurelium.auraskills.common.util.data.Pair;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

public class BukkitEventHandler implements EventHandler {

Expand All @@ -21,21 +22,32 @@ public BukkitEventHandler(AuraSkills plugin) {

@Override
public void callUserLoadEvent(User user) {
UserLoadEvent event = new UserLoadEvent(((BukkitUser) user).getPlayer(), user.toApi());
Bukkit.getPluginManager().callEvent(event);
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
UserLoadEvent event = new UserLoadEvent(player, user.toApi());
Bukkit.getPluginManager().callEvent(event);
}
}

@Override
public void callSkillLevelUpEvent(User user, Skill skill, int level) {
SkillLevelUpEvent event = new SkillLevelUpEvent(((BukkitUser) user).getPlayer(), user.toApi(), skill, level);
Bukkit.getPluginManager().callEvent(event);
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
SkillLevelUpEvent event = new SkillLevelUpEvent(player, user.toApi(), skill, level);
Bukkit.getPluginManager().callEvent(event);
}
}

@Override
public Pair<Boolean, Double> callXpGainEvent(User user, Skill skill, double amount) {
XpGainEvent event = new XpGainEvent(((BukkitUser) user).getPlayer(), user.toApi(), skill, amount);
Bukkit.getPluginManager().callEvent(event);
return new Pair<>(event.isCancelled(), event.getAmount());
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
XpGainEvent event = new XpGainEvent(player, user.toApi(), skill, amount);
Bukkit.getPluginManager().callEvent(event);
return new Pair<>(event.isCancelled(), event.getAmount());
} else {
return new Pair<>(false, 0.0);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.aurelium.auraskills.common.hooks.PlaceholderHook;
import dev.aurelium.auraskills.common.user.User;
import me.clip.placeholderapi.PlaceholderAPI;
import org.bukkit.entity.Player;
import org.spongepowered.configurate.ConfigurationNode;

public class PlaceholderApiHook extends PlaceholderHook {
Expand All @@ -18,7 +19,12 @@ public PlaceholderApiHook(AuraSkills plugin, ConfigurationNode config) {

@Override
public String setPlaceholders(User user, String message) {
return PlaceholderAPI.setPlaceholders(((BukkitUser) user).getPlayer(), message);
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
return PlaceholderAPI.setPlaceholders(player, message);
} else {
return message;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.aurelium.auraskills.common.hooks.HookRegistrationException;
import dev.aurelium.auraskills.common.user.User;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.spongepowered.configurate.ConfigurationNode;

Expand All @@ -26,7 +27,10 @@ public VaultHook(AuraSkills plugin, ConfigurationNode config) {

@Override
public void deposit(User user, double amount) {
economy.depositPlayer(((BukkitUser) user).getPlayer(), amount);
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
economy.depositPlayer(player, amount);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public boolean containsItem(NamespacedId key) {
@Override
public void giveItem(User user, NamespacedId key, int amount) {
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return;

ItemStack item = getItem(key);

if (item == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public <T extends SourceLeveler> T getLeveler(Class<T> levelerClass) {
@Override
public void playLevelUpSound(@NotNull User user) {
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return;
try {
player.playSound(player.getLocation(), Sound.valueOf(plugin.configString(Option.LEVELER_SOUND_TYPE))
, SoundCategory.valueOf(plugin.configString(Option.LEVELER_SOUND_CATEGORY))
Expand All @@ -79,6 +80,9 @@ public void playLevelUpSound(@NotNull User user) {

@Override
public void reloadModifiers(User user) {
plugin.getModifierManager().reloadPlayer(((BukkitUser) user).getPlayer());
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
plugin.getModifierManager().reloadPlayer(player);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ private void countErrorTimer() {

private void callRefreshEvent(User user, ManaAbility manaAbility) {
Player player = ((BukkitUser) user).getPlayer();
ManaAbilityRefreshEvent event = new ManaAbilityRefreshEvent(player, user.toApi(), manaAbility);
Bukkit.getPluginManager().callEvent(event);
if (player != null) {
ManaAbilityRefreshEvent event = new ManaAbilityRefreshEvent(player, user.toApi(), manaAbility);
Bukkit.getPluginManager().callEvent(event);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public void recoveryCustom(CustomRegenEvent event) {

if (isDisabled(ability)) return;
Player player = BukkitUser.getPlayer(event.getUser());
if (player == null) return;

User user = plugin.getUser(player);
// Gets health
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.aurelium.auraskills.common.stat.StatManager;
import dev.aurelium.auraskills.common.user.User;
import dev.aurelium.auraskills.api.bukkit.BukkitTraitHandler;
import org.bukkit.entity.Player;

public class BukkitStatManager extends StatManager {

Expand All @@ -18,11 +19,14 @@ public BukkitStatManager(AuraSkillsPlugin plugin) {
@Override
public void reloadPlayer(User user) {
// Reload traits
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return;

for (Trait trait : plugin.getTraitManager().getEnabledTraits()) {
BukkitTraitHandler traitImpl = ((BukkitTraitManager) plugin.getTraitManager()).getTraitImpl(trait);
if (traitImpl == null) continue;

traitImpl.onReload(((BukkitUser) user).getPlayer(), user.toApi(), trait);
traitImpl.onReload(player, user.toApi(), trait);
}
}

Expand All @@ -36,12 +40,14 @@ public <T> void reload(User user, T type) {
@Override
public void reloadStat(User user, Stat stat) {
if (!stat.isEnabled()) return;
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return;
// Reload traits
for (Trait trait : stat.getTraits()) {
BukkitTraitHandler traitImpl = ((BukkitTraitManager) plugin.getTraitManager()).getTraitImpl(trait);
if (traitImpl == null) continue;

traitImpl.onReload(((BukkitUser) user).getPlayer(), user.toApi(), trait);
traitImpl.onReload(player, user.toApi(), trait);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public BukkitTraitHandler getTraitImpl(Trait trait) {
@Override
public double getBaseLevel(User user, Trait trait) {
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return 0.0;

BukkitTraitHandler traitImpl = getTraitImpl(trait);
if (traitImpl != null) {
return traitImpl.getBaseLevel(player, trait);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public BukkitActionBarManager(AuraSkillsPlugin plugin, UiProvider uiProvider) {
@NotNull
public String getHp(User user) {
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return "";

return String.valueOf(Math.round(player.getHealth() * Traits.HP.optionDouble("action_bar_scaling", 1)));
}

@Override
@NotNull
public String getMaxHp(User user) {
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return "";

AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);
if (attribute != null) {
return String.valueOf(Math.round(attribute.getValue() * Traits.HP.optionDouble("action_bar_scaling", 1)));
Expand All @@ -38,6 +42,10 @@ public String getMaxHp(User user) {
@Override
@NotNull
public String getWorldName(User user) {
return ((BukkitUser) user).getPlayer().getWorld().getName();
Player player = ((BukkitUser) user).getPlayer();
if (player != null) {
return player.getWorld().getName();
}
return "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public BossBarManager getBossBarManager() {
@Override
public void sendActionBar(User user, String message) {
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return;

if (plugin.getHookManager().isRegistered(ProtocolLibHook.class)) {
ProtocolLibHook hook = plugin.getHookManager().getHook(ProtocolLibHook.class);
hook.sendActionBar(player, message);
Expand All @@ -47,6 +49,7 @@ public void sendActionBar(User user, String message) {
@Override
public void sendXpBossBar(User user, Skill skill, double currentXp, double levelXp, double xpGained, int level, boolean maxed) {
Player player = ((BukkitUser) user).getPlayer();
if (player == null) return;
bossBarManager.sendBossBar(player, skill, currentXp, levelXp, xpGained, level, maxed);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,29 @@
import dev.aurelium.auraskills.common.user.User;
import dev.aurelium.auraskills.common.util.text.TextUtil;
import net.kyori.adventure.text.Component;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.jetbrains.annotations.Nullable;

import java.util.Locale;
import java.util.UUID;
import java.util.regex.Pattern;

public class BukkitUser extends User {

// Permission pattern
private static final Pattern pattern = Pattern.compile("-?\\d+(\\.\\d+)?");

@Nullable
private final Player player;

public BukkitUser(Player player, AuraSkillsPlugin plugin) {
super(player.getUniqueId(), plugin);
public BukkitUser(UUID uuid, @Nullable Player player, AuraSkillsPlugin plugin) {
super(uuid, plugin);
this.player = player;
}

@Nullable
public static Player getPlayer(SkillsUser skillsUser) {
return ((BukkitUser) ((ApiSkillsUser) skillsUser).getUser()).getPlayer();
}
Expand All @@ -35,17 +39,22 @@ public static BukkitUser getUser(SkillsUser skillsUser) {
return (BukkitUser) ((ApiSkillsUser) skillsUser).getUser();
}

@Nullable
public Player getPlayer() {
return player;
}

@Override
public String getUsername() {
return player.getName();
String name = Bukkit.getOfflinePlayer(uuid).getName();
return name != null ? name : "?";
}

@Override
public double getPermissionMultiplier(@Nullable Skill skill) {
if (player == null) {
return 0.0;
}
double multiplier = 0.0;
for (PermissionAttachmentInfo info : player.getEffectivePermissions()) {
String permission = info.getPermission().toLowerCase(Locale.ROOT);
Expand Down Expand Up @@ -74,6 +83,8 @@ public double getPermissionMultiplier(@Nullable Skill skill) {

@Override
public void sendMessage(Component component) {
((AuraSkills) plugin).getAudiences().player(player).sendMessage(component);
if (player != null) {
((AuraSkills) plugin).getAudiences().player(player).sendMessage(component);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,8 @@ public Map<UUID, User> getUserMap() {

@Override
public User createNewUser(UUID uuid) {
Player player = plugin.getServer().getPlayer(uuid);
if (player == null) {
throw new IllegalArgumentException("Cannot create user for offline player!");
}
User user = new BukkitUser(player, plugin);
@Nullable Player player = plugin.getServer().getPlayer(uuid);
User user = new BukkitUser(uuid, player, plugin);
// Set all skills to level 1 for new players
for (LoadedSkill loadedSkill : plugin.getSkillManager().getSkills()) {
user.setSkillLevel(loadedSkill.skill(), plugin.config().getStartLevel());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public abstract class User {

protected final AuraSkillsPlugin plugin;

private final UUID uuid;
protected final UUID uuid;

private final Map<Skill, Integer> skillLevels;
private final Map<Skill, Double> skillXp;
Expand Down

0 comments on commit 28e23f1

Please sign in to comment.