refactor: remove generic from actionresult
This commit is contained in:
parent
18d7322406
commit
a91d039dd2
11 changed files with 42 additions and 45 deletions
|
@ -1,27 +1,24 @@
|
||||||
package xyz.atnrch.nicko.disguise;
|
package xyz.atnrch.nicko.disguise;
|
||||||
|
|
||||||
public class ActionResult<R> {
|
public class ActionResult {
|
||||||
private final String errorKey;
|
private final String errorKey;
|
||||||
private boolean error = false;
|
private boolean error = false;
|
||||||
private R result;
|
|
||||||
|
|
||||||
public ActionResult() {
|
public static ActionResult ok() {
|
||||||
|
return new ActionResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ActionResult error(String errorMessage) {
|
||||||
|
return new ActionResult(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ActionResult() {
|
||||||
this.errorKey = null;
|
this.errorKey = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult(String errorMessage) {
|
private ActionResult(String errorMessage) {
|
||||||
this.errorKey = errorMessage;
|
this.errorKey = errorMessage;
|
||||||
this.error = true;
|
this.error = true;
|
||||||
this.result = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ActionResult(R result) {
|
|
||||||
this.errorKey = null;
|
|
||||||
this.result = result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public R getResult() {
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isError() {
|
public boolean isError() {
|
||||||
|
|
|
@ -82,11 +82,11 @@ public class AppearanceManager {
|
||||||
updatePlayer(true, false);
|
updatePlayer(true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult<Void> reset() {
|
public ActionResult reset() {
|
||||||
final String defaultName = nameStore.getStoredName(player);
|
final String defaultName = nameStore.getStoredName(player);
|
||||||
this.profile.setName(defaultName);
|
this.profile.setName(defaultName);
|
||||||
this.profile.setSkin(defaultName);
|
this.profile.setSkin(defaultName);
|
||||||
final ActionResult<Void> actionResult = updatePlayer(true, true);
|
final ActionResult actionResult = updatePlayer(true, true);
|
||||||
if (!actionResult.isError()) {
|
if (!actionResult.isError()) {
|
||||||
this.profile.setSkin(null);
|
this.profile.setSkin(null);
|
||||||
this.profile.setName(null);
|
this.profile.setName(null);
|
||||||
|
@ -94,11 +94,11 @@ public class AppearanceManager {
|
||||||
return actionResult;
|
return actionResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult<Void> updatePlayer(boolean skinChange, boolean reset) {
|
public ActionResult updatePlayer(boolean skinChange, boolean reset) {
|
||||||
final String displayName = profile.getName() == null ? player.getName() : profile.getName();
|
final String displayName = profile.getName() == null ? player.getName() : profile.getName();
|
||||||
|
|
||||||
final WrappedGameProfile gameProfile = WrappedGameProfile.fromPlayer(player).withName(displayName);
|
final WrappedGameProfile gameProfile = WrappedGameProfile.fromPlayer(player).withName(displayName);
|
||||||
final ActionResult<Void> result = updateGameProfileSkin(gameProfile, skinChange, reset);
|
final ActionResult result = updateGameProfileSkin(gameProfile, skinChange, reset);
|
||||||
final boolean wasFlying = player.isFlying();
|
final boolean wasFlying = player.isFlying();
|
||||||
if (!result.isError()) {
|
if (!result.isError()) {
|
||||||
updateMetadata();
|
updateMetadata();
|
||||||
|
@ -109,7 +109,7 @@ public class AppearanceManager {
|
||||||
player.teleport(player.getLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
|
player.teleport(player.getLocation(), PlayerTeleportEvent.TeleportCause.PLUGIN);
|
||||||
player.setFlying(wasFlying);
|
player.setFlying(wasFlying);
|
||||||
player.updateInventory();
|
player.updateInventory();
|
||||||
return new ActionResult<>();
|
return ActionResult.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateOthers() {
|
public void updateOthers() {
|
||||||
|
@ -126,7 +126,7 @@ public class AppearanceManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private ActionResult<Void> updateGameProfileSkin(WrappedGameProfile gameProfile, boolean skinChange, boolean reset) {
|
private ActionResult updateGameProfileSkin(WrappedGameProfile gameProfile, boolean skinChange, boolean reset) {
|
||||||
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
|
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
|
||||||
|
|
||||||
if (skinChange || changeOnlyName) {
|
if (skinChange || changeOnlyName) {
|
||||||
|
@ -142,17 +142,17 @@ public class AppearanceManager {
|
||||||
properties.get("textures").clear();
|
properties.get("textures").clear();
|
||||||
properties.put("textures", new WrappedSignedProperty("textures", skinResult.getValue(), skinResult.getSignature()));
|
properties.put("textures", new WrappedSignedProperty("textures", skinResult.getValue(), skinResult.getSignature()));
|
||||||
} else {
|
} else {
|
||||||
return new ActionResult<>(I18NDict.Error.MOJANG_SKIN);
|
return ActionResult.error(I18NDict.Error.MOJANG_SKIN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new ActionResult<>();
|
return ActionResult.ok();
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
return new ActionResult<>(I18NDict.Error.CACHE);
|
return ActionResult.error(I18NDict.Error.CACHE);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return new ActionResult<>(I18NDict.Error.MOJANG_NAME);
|
return ActionResult.error(I18NDict.Error.MOJANG_NAME);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return new ActionResult<>();
|
return ActionResult.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateMetadata() {
|
private void updateMetadata() {
|
||||||
|
|
|
@ -25,7 +25,7 @@ public class PlayerJoinListener implements Listener {
|
||||||
Bukkit.getScheduler().runTaskLater(instance, () -> {
|
Bukkit.getScheduler().runTaskLater(instance, () -> {
|
||||||
final AppearanceManager appearanceManager = AppearanceManager.get(player);
|
final AppearanceManager appearanceManager = AppearanceManager.get(player);
|
||||||
if (appearanceManager.hasData()) {
|
if (appearanceManager.hasData()) {
|
||||||
final ActionResult<Void> actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange());
|
final ActionResult actionResult = appearanceManager.updatePlayer(appearanceManager.needsASkinChange(), true);
|
||||||
if (!actionResult.isError()) {
|
if (!actionResult.isError()) {
|
||||||
player.sendMessage(i18n.translate(I18NDict.Event.PreviousSkin.SUCCESS));
|
player.sendMessage(i18n.translate(I18NDict.Event.PreviousSkin.SUCCESS));
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -11,7 +11,7 @@ public class PlayerQuitListener implements Listener {
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||||
final Player player = event.getPlayer();
|
final Player player = event.getPlayer();
|
||||||
final ActionResult<Void> result = NickoBukkit.getInstance().getDataStore().saveData(player);
|
final ActionResult result = NickoBukkit.getInstance().getDataStore().saveData(player);
|
||||||
if (result.isError()) {
|
if (result.isError()) {
|
||||||
NickoBukkit.getInstance().getLogger().warning("Failed to save data for " + player.getName());
|
NickoBukkit.getInstance().getLogger().warning("Failed to save data for " + player.getName());
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ public abstract class Cache {
|
||||||
|
|
||||||
public abstract CacheProvider getProvider();
|
public abstract CacheProvider getProvider();
|
||||||
|
|
||||||
public abstract ActionResult<Void> cache(UUID uuid, NickoProfile profile);
|
public abstract ActionResult cache(UUID uuid, NickoProfile profile);
|
||||||
|
|
||||||
public abstract boolean isCached(UUID uuid);
|
public abstract boolean isCached(UUID uuid);
|
||||||
|
|
||||||
|
|
|
@ -62,11 +62,11 @@ public class PlayerDataStore {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public ActionResult<Void> saveData(Player player) {
|
public ActionResult saveData(Player player) {
|
||||||
if (storage.isError()) return new ActionResult<>(I18NDict.Error.GENERIC);
|
if (storage.isError()) return ActionResult.error(I18NDict.Error.GENERIC);
|
||||||
if (!cache.isCached(player.getUniqueId())) return new ActionResult<>(I18NDict.Error.GENERIC);
|
if (!cache.isCached(player.getUniqueId())) return ActionResult.error(I18NDict.Error.GENERIC);
|
||||||
if (!cache.retrieve(player.getUniqueId()).isPresent())
|
if (!cache.retrieve(player.getUniqueId()).isPresent())
|
||||||
return new ActionResult<>(I18NDict.Error.GENERIC);
|
return ActionResult.error(I18NDict.Error.GENERIC);
|
||||||
|
|
||||||
// TODO (Ineanto, 5/20/23): Remove value from cache
|
// TODO (Ineanto, 5/20/23): Remove value from cache
|
||||||
//profiles.remove(player.getUniqueId());
|
//profiles.remove(player.getUniqueId());
|
||||||
|
|
|
@ -11,7 +11,7 @@ public abstract class Storage {
|
||||||
|
|
||||||
public abstract StorageProvider getProvider();
|
public abstract StorageProvider getProvider();
|
||||||
|
|
||||||
public abstract ActionResult<Void> store(UUID uuid, NickoProfile profile);
|
public abstract ActionResult store(UUID uuid, NickoProfile profile);
|
||||||
|
|
||||||
public abstract boolean isStored(UUID uuid);
|
public abstract boolean isStored(UUID uuid);
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class JSONStorage extends Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
|
public ActionResult store(UUID uuid, NickoProfile profile) {
|
||||||
final String profileToJson = gson.toJson(profile);
|
final String profileToJson = gson.toJson(profile);
|
||||||
final File file = new File(directory, uuid.toString() + ".json");
|
final File file = new File(directory, uuid.toString() + ".json");
|
||||||
|
|
||||||
|
@ -42,15 +42,15 @@ public class JSONStorage extends Storage {
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.warning("Could not write to file.");
|
logger.warning("Could not write to file.");
|
||||||
return new ActionResult<>(I18NDict.Error.JSON_ERROR);
|
return ActionResult.error(I18NDict.Error.JSON_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.warning("Could not create file.");
|
logger.warning("Could not create file.");
|
||||||
return new ActionResult<>(I18NDict.Error.JSON_ERROR);
|
return ActionResult.error(I18NDict.Error.JSON_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ActionResult<>();
|
return ActionResult.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -21,10 +21,10 @@ public class MapCache extends Cache {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult<Void> cache(UUID uuid, NickoProfile profile) {
|
public ActionResult cache(UUID uuid, NickoProfile profile) {
|
||||||
final HashMap<UUID, NickoProfile> profiles = provider.getMap();
|
final HashMap<UUID, NickoProfile> profiles = provider.getMap();
|
||||||
profiles.put(uuid, profile);
|
profiles.put(uuid, profile);
|
||||||
return new ActionResult<>();
|
return ActionResult.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -34,10 +34,10 @@ public class RedisCache extends Cache {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult<Void> cache(UUID uuid, NickoProfile profile) {
|
public ActionResult cache(UUID uuid, NickoProfile profile) {
|
||||||
final Jedis jedis = provider.getJedis();
|
final Jedis jedis = provider.getJedis();
|
||||||
jedis.set("nicko:" + uuid.toString(), gson.toJson(profile));
|
jedis.set("nicko:" + uuid.toString(), gson.toJson(profile));
|
||||||
return new ActionResult<>();
|
return ActionResult.ok();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -36,18 +36,18 @@ public class SQLStorage extends Storage {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ActionResult<Void> store(UUID uuid, NickoProfile profile) {
|
public ActionResult store(UUID uuid, NickoProfile profile) {
|
||||||
final Connection connection = getProvider().getConnection();
|
final Connection connection = getProvider().getConnection();
|
||||||
if (connection == null) return new ActionResult<>(I18NDict.Error.SQL_ERROR);
|
if (connection == null) return ActionResult.error(I18NDict.Error.SQL_ERROR);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
final PreparedStatement statement = isStored(uuid) ?
|
final PreparedStatement statement = isStored(uuid) ?
|
||||||
getUpdateStatement(connection, uuid, profile) : getInsertStatement(connection, uuid, profile);
|
getUpdateStatement(connection, uuid, profile) : getInsertStatement(connection, uuid, profile);
|
||||||
statement.executeUpdate();
|
statement.executeUpdate();
|
||||||
return new ActionResult<>();
|
return ActionResult.ok();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
logger.warning("Couldn't send SQL Request: " + e.getMessage());
|
logger.warning("Couldn't send SQL Request: " + e.getMessage());
|
||||||
return new ActionResult<>(I18NDict.Error.SQL_ERROR);
|
return ActionResult.error(I18NDict.Error.SQL_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue