Skip to content

Commit

Permalink
Chore: Cleanup. Fix nullability issues, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Skullians committed Dec 23, 2024
1 parent 12ec0b4 commit 97ae698
Show file tree
Hide file tree
Showing 56 changed files with 573 additions and 315 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/qodana.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Qodana
on:
workflow_dispatch:
pull_request:
push:
branches:
- main

jobs:
qodana:
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
checks: write
steps:
- uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0
- name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2024.3
env:
QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ Server/
run/
assets/data.sqlite3
/tasks
qodana.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@ public CompletableFuture<Faction> getFaction(String name) {
if (ex != null || faction == null) return;

factionCache.put(faction.getName(), faction);
for (SkyUser player : faction.getAllMembers()) {

}
for (SkyUser player : faction.getAllMembers()) {
if (!player.isOnline()) return;
factionCache.put(faction.getName(), faction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ public void playSound(SkyUser player, String identifier, float pitch, float volu
public abstract void playSound(SkyLocation location, String identifier, float pitch, float volume);

public Sound getSound(String identifier, float pitch, float volume) {
Sound sound = Sound.sound(Key.key(identifier), Sound.Source.MASTER, volume, pitch);
if (sound == null) {
SLogger.warn("Attempted to play a sound of {} when it returned null!", identifier);
return null;
} else return sound;
return Sound.sound(Key.key(identifier), Sound.Source.MASTER, volume, pitch);
}

public abstract void playMusic(SkyUser def, SkyUser att);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.skullian.skyfactions.common.command;

import net.skullian.skyfactions.common.user.SkyUser;

import java.util.List;

public abstract class CommandTemplate {
Expand All @@ -14,4 +16,8 @@ public abstract class CommandTemplate {

public abstract List<String> permission();

public boolean hasPermission(SkyUser player, boolean sendDeny) {
return CommandsUtility.hasPerm(player, permission(), sendDeny);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,30 @@ public class LinkCommand extends CommandTemplate {
public void perform(
SkyUser player
) {
if (!CommandsUtility.hasPerm(player, List.of("skyfactions.command.link", "skyfactions.discord"), true)) return;
if (!hasPermission(player, true)) return;
String locale = SkyApi.getInstance().getPlayerAPI().getLocale(player.getUniqueId());
DiscordModule module = (DiscordModule) SkyModules.DISCORD.getModule();

SkyApi.getInstance().getPlayerAPI().getPlayerData(player.getUniqueId()).whenComplete((data, ex) -> {
if (ex != null) {
ErrorUtil.handleError(player, "link your Discord", "SQL_GET_DISCORD", ex);
} else if (data.getDISCORD_ID() == null) {
module.createLinkCode(player).whenComplete((code, err) -> {
if (err != null) {
err.printStackTrace();
return;
SkyApi.getInstance().getPlayerAPI().getPlayerData(player.getUniqueId())
.thenApplyAsync(data -> {
if (data == null) return null;
else if (!data.getDISCORD_ID().equalsIgnoreCase("none")) {

String retrievedUser = module.getUsernameByID(data.getDISCORD_ID());
Messages.DISCORD_ALREADY_LINKED.send(player, locale, "discord_name", retrievedUser);
}

return module.createLinkCode(player);
})
.thenAcceptAsync(code -> {
if (code == null) return;

Messages.DISCORD_LINK_PROMPT.send(player, locale, "code", code);
})
.exceptionally(ex -> {
ErrorUtil.handleError(player, "link your Discord", "SQL_GET_DISCORD", ex);
return null;
});
} else {
String retrievedUser = module.getUsernameByID(data.getDISCORD_ID());
Messages.DISCORD_ALREADY_LINKED.send(player, locale, "discord_name", retrievedUser);
}
});
}

@Override
Expand All @@ -64,6 +67,6 @@ public String getSyntax() {

@Override
public List<String> permission() {
return List.of("skyfactions.command.link");
return List.of("skyfactions.command.link", "skyfactions.discord");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void handleUnlink(
Messages.DISCORD_UNLINK_SUCCESS.send(player, locale);
}
});

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.skullian.skyfactions.common.api.SkyApi;
import net.skullian.skyfactions.common.config.types.*;
import net.skullian.skyfactions.paper.event.defence.DefencePlacementHandler;

import java.io.File;
import java.util.HashMap;
Expand All @@ -11,6 +12,7 @@ public class ConfigFileHandler {

private Map<ConfigTypes, ConfigHandler> configs = new HashMap<>();

@SuppressWarnings("ResultOfMethodCallIgnored")
public void loadFiles() {
String configPath = SkyApi.getInstance().getFileAPI().getConfigFolderPath();
new File(configPath, "/schematics").mkdirs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Objects;

public class ConfigHandler {

Expand All @@ -26,7 +27,7 @@ public ConfigHandler(String name) {
this.file = new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath(), this.name);

try {
this.config = YamlDocument.create(this.file, ConfigHandler.class.getClassLoader().getResourceAsStream(this.name),
this.config = YamlDocument.create(this.file, Objects.requireNonNull(ConfigHandler.class.getClassLoader().getResourceAsStream(this.name)),
GeneralSettings.DEFAULT, LoaderSettings.builder().setAutoUpdate(true).build(), DumperSettings.DEFAULT, UpdaterSettings.builder().setVersioning(new BasicVersioning("CONFIG_VERSION")).build());
} catch (IOException error) {
SLogger.setup("----------------- CONFIGURATION EXCEPTION -----------------", true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,29 +265,29 @@ public enum Messages {
NOTIFICATION_FACTION_RANK_UPDATED_TITLE("Notifications.FACTION_RANK_UPDATED_TITLE"),
NOTIFICATION_FACTION_RANK_UPDATED_DESCRIPTION("Notifications.FACTION_RANK_UPDATED_DESCRIPTION");

public static Map<String, YamlDocument> configs = new HashMap<>();
public static final Map<String, YamlDocument> configs = new HashMap<>();
private final String path;

Messages(String path) {
this.path = path;
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void load(boolean setup) {
try {
new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath(), "/language").mkdirs();
if (setup) SLogger.setup("Saving default language <#05eb2f>[English]<#4294ed>.", false);
else SLogger.info("Saving default language <#05eb2f>[English]<#4294ed>.");

YamlDocument doc = YamlDocument.create(new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath() + "/language/en/en.yml"), Messages.class.getClassLoader().getResourceAsStream("language/en/en.yml"),
YamlDocument doc = YamlDocument.create(new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath() + "/language/en/en.yml"), Objects.requireNonNull(Messages.class.getClassLoader().getResourceAsStream("language/en/en.yml")),
GeneralSettings.DEFAULT, LoaderSettings.builder().setAutoUpdate(true).build(), DumperSettings.DEFAULT, UpdaterSettings.builder().setVersioning(new BasicVersioning("CONFIG_VERSION")).build());

configs.put("en", doc);


File folder = new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath(), "/language");
if (!folder.exists() || !folder.isDirectory()) throw new Exception("Could not find the language folder. Please report this error ASAP.");

for (File dir : folder.listFiles()) {
for (File dir : Objects.requireNonNull(folder.listFiles())) {

if (dir.isDirectory()) {
if (setup) SLogger.setup("Registering Language: <#05eb2f>[{}]<#4294ed>", false, dir.getName());
Expand All @@ -300,6 +300,7 @@ public static void load(boolean setup) {
);

registerGUIs(dir, dir.getName());
SkyApi.getInstance().getDefenceFactory().register(dir, dir.getName());
}
}
} catch (Exception exception) {
Expand All @@ -308,7 +309,7 @@ public static void load(boolean setup) {
SLogger.setup("Please check that config for any configuration mistakes.", true);
SLogger.setup("Plugin will now disable.", true);
SLogger.setup("----------------- CONFIGURATION EXCEPTION -----------------", true);
exception.printStackTrace();
SLogger.fatal(exception);
SkyApi.disablePlugin();
}

Expand All @@ -317,7 +318,7 @@ public static void load(boolean setup) {
private static void registerGUIs(File dir, String locale) throws IOException {
Map<String, YamlDocument> docs = new HashMap<>();
for (GUIEnums enumEntry : GUIEnums.values()) {
YamlDocument doc = YamlDocument.create(new File(dir, "guis/" + enumEntry.getPath() + ".yml"), Messages.class.getClassLoader().getResourceAsStream(String.format("language/%s/%s.yml", locale, "guis/" + enumEntry.getPath())),
YamlDocument doc = YamlDocument.create(new File(dir, "guis/" + enumEntry.getPath() + ".yml"), Objects.requireNonNull(Messages.class.getClassLoader().getResourceAsStream(String.format("language/%s/%s.yml", locale, "guis/" + enumEntry.getPath()))),
GeneralSettings.DEFAULT, LoaderSettings.builder().setAutoUpdate(true).build(), DumperSettings.DEFAULT, UpdaterSettings.builder().setVersioning(new BasicVersioning("CONFIG_VERSION")).build());

docs.put(enumEntry.getPath(), doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

public class DatabaseExecutionListener implements ExecuteListener {
@Override
@SuppressWarnings("ConstantConditions")
public void exception(ExecuteContext ctx) {
ErrorUtil.handleDatabaseError(ctx.exception());
if (ctx.exception() != null) ErrorUtil.handleDatabaseError(ctx.exception());
SkyApi.getInstance().getDatabaseManager().closed = SkyApi.getInstance().getDatabaseManager().getDataSource().isClosed();

new RuntimeException("Database is closed! Cannot allow player to join without risking dupes and unexpected functionalities. Kicking all online players.").printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public CompletableFuture<Void> cacheOnce() {

String factionName = cachedFaction.getKey();
Faction faction = SkyApi.getInstance().getFactionAPI().getCachedFaction(factionName);
if (faction == null) continue;

int gemsModification = cachedFaction.getValue().getGems();
int runesModification = cachedFaction.getValue().getRunes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,37 @@ public abstract class DefenceFactory {

private final List<String> cachedMaterials = new ArrayList<>();

@SuppressWarnings("ResultOfMethodCallIgnored")
public void registerDefaultDefences() {
new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath() + "/language/en/defences").mkdirs();

try {
URI defencesFolder = DefenceFactory.class.getResource("/language/en/defences").toURI();
FileSystem fileSystem = FileSystems.newFileSystem(defencesFolder, new HashMap());

for (Path yamlPath : fileSystem.getRootDirectories()) {
Stream<Path> stream = Files.list(yamlPath.resolve("/language/en/defences"));
stream.forEach(path -> {
try {
String fullPathName = path.getFileName().toString();
String defenceName = fullPathName.substring(0, fullPathName.length() - 4);

YamlDocument.create(new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath() + "/language/en/defences/", defenceName + ".yml"), Objects.requireNonNull(DefenceFactory.class.getClassLoader().getResourceAsStream(String.format("language/en/defences/%s.yml", defenceName))),
GeneralSettings.DEFAULT, LoaderSettings.builder().setAutoUpdate(true).build(), DumperSettings.DEFAULT, UpdaterSettings.builder().setVersioning(new BasicVersioning("CONFIG_VERSION")).build());
URI defencesFolder = Objects.requireNonNull(DefenceFactory.class.getResource("/language/en/defences")).toURI();
try (FileSystem fileSystem = FileSystems.newFileSystem(defencesFolder, new HashMap<>())) {
for (Path yamlPath : fileSystem.getRootDirectories()) {
try (Stream<Path> stream = Files.list(yamlPath.resolve("/language/en/defences"))) {
stream.forEach(path -> {
try {
String fullPathName = path.getFileName().toString();
String defenceName = fullPathName.substring(0, fullPathName.length() - 4);

YamlDocument.create(new File(SkyApi.getInstance().getFileAPI().getConfigFolderPath() + "/language/en/defences/", defenceName + ".yml"), Objects.requireNonNull(DefenceFactory.class.getClassLoader().getResourceAsStream(String.format("language/en/defences/%s.yml", defenceName))),
GeneralSettings.DEFAULT, LoaderSettings.builder().setAutoUpdate(true).build(), DumperSettings.DEFAULT, UpdaterSettings.builder().setVersioning(new BasicVersioning("CONFIG_VERSION")).build());
} catch (IOException error) {
handleError(error);
}
});
} catch (IOException error) {
handleError(error);
}
});
}
}
} catch (URISyntaxException | IOException error) {
handleError(error);
}
}

@SuppressWarnings("ResultOfMethodCallIgnored")
public void register(File directory, String locale) {
try {
File dir = new File(directory + "/defences");
Expand All @@ -79,6 +84,8 @@ public void register(File directory, String locale) {

DefenceStruct struct = createStruct(config, defenceName);
dMap.put(struct.getIDENTIFIER(), struct);

defences.put(locale, dMap);
}
} catch (IOException error) {
handleError(error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void onClick(SkyClickType clickType, SkyUser player) {
}
DefenceData data = (DefenceData) getOPTIONALS()[0];
Defence defence = SkyApi.getInstance().getDefenceAPI().getDefenceFromData(data);
if (defence == null) return;

data.setTARGET_HOSTILES(!data.isTARGET_HOSTILES());
defence.setData(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public void onClick(SkyClickType clickType, SkyUser player) {
}
DefenceData data = (DefenceData) getOPTIONALS()[0];
Defence defence = SkyApi.getInstance().getDefenceAPI().getDefenceFromData(data);
if (defence == null) return;

data.setTARGET_PASSIVE(!data.isTARGET_PASSIVE());
defence.setData(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void onClick(SkyClickType clickType, SkyUser player) {
DefenceData defenceData = (DefenceData) getOPTIONALS()[1];

Defence defence = SkyApi.getInstance().getDefenceAPI().getDefenceFromData(defenceData);
if (defence == null) return;
defence.disable();

defence.remove().whenComplete((ignored, ex) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class SkyModuleManager {
private static final Map<String, SkyModule> enabledModules = new HashMap<>();

public static void registerModule(Class<?> module) {
modules.add(module.getClass().getName());
modules.add(module.getName());
}

public static void onEnable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.skullian.skyfactions.common.util.SkyLocation;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -47,8 +48,9 @@ public UUID getUniqueId() {

public abstract void teleport(SkyLocation location);

@SuppressWarnings("")
public PlayerData getPlayerData() {
return this.data.get();
return this.data.orElse(null);
}

public CompletableFuture<Integer> getGems() {
Expand Down Expand Up @@ -105,12 +107,12 @@ public CompletableFuture<List<InviteData>> getIncomingInvites() {
}

public List<InviteData> getCachedInvites() {
return this.incomingInvites.get();
return this.incomingInvites.orElse(new ArrayList<>());
}

public void onCacheComplete(int runesAddition, int gemsAddition) {
this.runes = Optional.of(this.runes.isPresent() ? this.runes.get() : 0 + runesAddition);
this.gems = Optional.of(this.gems.isPresent() ? this.gems.get() : 0 + gemsAddition);
this.runes = Optional.of(this.runes.orElse(runesAddition));
this.gems = Optional.of(this.gems.orElse(gemsAddition));
}

@Nullable public abstract CompletableFuture<JoinRequestData> getActiveJoinRequest();
Expand Down
Loading

0 comments on commit 97ae698

Please sign in to comment.