fix: persistence, i18n

This commit is contained in:
ineanto 2023-08-21 17:52:15 +02:00
parent 618af08024
commit 8d87cddbea
8 changed files with 44 additions and 25 deletions

View file

@ -44,7 +44,7 @@ public class AnvilManager {
getNameAnvil().open(player); getNameAnvil().open(player);
} }
public AnvilGUI.Builder getNameThenSkinAnvil() { private AnvilGUI.Builder getNameThenSkinAnvil() {
return new AnvilGUI.Builder() return new AnvilGUI.Builder()
.plugin(NickoBukkit.getInstance()) .plugin(NickoBukkit.getInstance())
.itemLeft(getLeftItem(false)) .itemLeft(getLeftItem(false))
@ -65,7 +65,7 @@ public class AnvilManager {
.text("New name..."); .text("New name...");
} }
public AnvilGUI.Builder getNameAnvil() { private AnvilGUI.Builder getNameAnvil() {
return new AnvilGUI.Builder() return new AnvilGUI.Builder()
.plugin(NickoBukkit.getInstance()) .plugin(NickoBukkit.getInstance())
.itemLeft(getLeftItem(false)) .itemLeft(getLeftItem(false))

View file

@ -21,7 +21,6 @@ import xyz.atnrch.nicko.wrapper.*;
import java.io.IOException; import java.io.IOException;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
public class AppearanceManager { public class AppearanceManager {
@ -29,35 +28,29 @@ public class AppearanceManager {
private final PlayerDataStore dataStore = instance.getDataStore(); private final PlayerDataStore dataStore = instance.getDataStore();
private final PlayerNameStore nameStore = instance.getNameStore(); private final PlayerNameStore nameStore = instance.getNameStore();
private final NickoProfile profile;
private final Player player; private final Player player;
private final UUID uuid;
public AppearanceManager(Player player) { public AppearanceManager(Player player) {
this.uuid = player.getUniqueId();
this.player = player; this.player = player;
final Optional<NickoProfile> optionalProfile = dataStore.getData(player.getUniqueId());
this.profile = optionalProfile.orElse(NickoProfile.EMPTY_PROFILE.clone());
if (!optionalProfile.isPresent()) {
instance.getLogger().warning("Unable to appearance data for: " + player.getUniqueId() + ".");
}
} }
public ActionResult reset() { public ActionResult reset() {
final NickoProfile profile = getNickoProfile();
final String defaultName = nameStore.getStoredName(player); final String defaultName = nameStore.getStoredName(player);
profile.setName(defaultName); profile.setName(defaultName);
profile.setSkin(defaultName); profile.setSkin(defaultName);
dataStore.getCache().cache(player.getUniqueId(), profile);
final ActionResult actionResult = updatePlayer(true, true); final ActionResult actionResult = updatePlayer(true, true);
if (!actionResult.isError()) { if (!actionResult.isError()) {
profile.setSkin(null); profile.setSkin(null);
profile.setName(null); profile.setName(null);
dataStore.getCache().cache(uuid, profile); dataStore.getCache().cache(player.getUniqueId(), profile);
} }
return actionResult; return actionResult;
} }
public ActionResult updatePlayer(boolean skinChange, boolean reset) { public ActionResult updatePlayer(boolean skinChange, boolean reset) {
final NickoProfile profile = getNickoProfile();
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 result = updateGameProfileSkin(gameProfile, skinChange, reset); final ActionResult result = updateGameProfileSkin(gameProfile, skinChange, reset);
@ -70,6 +63,11 @@ public class AppearanceManager {
return result; return result;
} }
private NickoProfile getNickoProfile() {
final Optional<NickoProfile> optionalProfile = dataStore.getData(player.getUniqueId());
return optionalProfile.orElse(NickoProfile.EMPTY_PROFILE.clone());
}
public void updateOthers() { public void updateOthers() {
final WrapperPlayServerEntityDestroy destroy = new WrapperPlayServerEntityDestroy(); final WrapperPlayServerEntityDestroy destroy = new WrapperPlayServerEntityDestroy();
final WrapperPlayServerNamedEntitySpawn spawn = new WrapperPlayServerNamedEntitySpawn(); final WrapperPlayServerNamedEntitySpawn spawn = new WrapperPlayServerNamedEntitySpawn();
@ -85,9 +83,10 @@ public class AppearanceManager {
private ActionResult 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 NickoProfile profile = getNickoProfile();
final boolean changeOnlyName = profile.getSkin() != null && profile.getSkin().equals(player.getName());
if (skinChange || changeOnlyName) { if (skinChange || !changeOnlyName) {
Optional<MojangSkin> skin; Optional<MojangSkin> skin;
try { try {
final MojangAPI mojangAPI = NickoBukkit.getInstance().getMojangAPI(); final MojangAPI mojangAPI = NickoBukkit.getInstance().getMojangAPI();

View file

@ -9,7 +9,6 @@ import xyz.atnrch.nicko.appearance.ActionResult;
import xyz.atnrch.nicko.appearance.AppearanceManager; import xyz.atnrch.nicko.appearance.AppearanceManager;
import xyz.atnrch.nicko.i18n.I18N; import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.mojang.MojangUtils; import xyz.atnrch.nicko.mojang.MojangUtils;
import xyz.atnrch.nicko.profile.AppearanceData;
import xyz.atnrch.nicko.profile.NickoProfile; import xyz.atnrch.nicko.profile.NickoProfile;
import xyz.atnrch.nicko.storage.PlayerDataStore; import xyz.atnrch.nicko.storage.PlayerDataStore;
@ -57,10 +56,10 @@ public class NickoDebugCmd {
if (optionalProfile.isPresent()) { if (optionalProfile.isPresent()) {
final NickoProfile profile = optionalProfile.get(); final NickoProfile profile = optionalProfile.get();
final PlayerDataStore dataStore = NickoBukkit.getInstance().getDataStore(); final PlayerDataStore dataStore = NickoBukkit.getInstance().getDataStore();
final AppearanceManager appearanceManager = new AppearanceManager(target);
profile.setName(name); profile.setName(name);
profile.setSkin(skin); profile.setSkin(skin);
dataStore.updateCache(target.getUniqueId(), profile); dataStore.updateCache(target.getUniqueId(), profile);
final AppearanceManager appearanceManager = new AppearanceManager(target);
final ActionResult result = appearanceManager.updatePlayer(true, false); final ActionResult result = appearanceManager.updatePlayer(true, false);
if (!result.isError()) { if (!result.isError()) {
target.sendMessage(prefix + "§aWhoosh!"); target.sendMessage(prefix + "§aWhoosh!");

View file

@ -26,9 +26,11 @@ public class HomeGUI {
dynamicStructure[2] = dynamicStructure[2].replace("A", "#"); dynamicStructure[2] = dynamicStructure[2].replace("A", "#");
} }
final ExitItem exitItem = new ExitItem(player);
this.gui = Gui.normal() this.gui = Gui.normal()
.setStructure(dynamicStructure) .setStructure(dynamicStructure)
.addIngredient('E', new ExitItem()) .addIngredient('E', exitItem.get())
.addIngredient('R', new ResetItem()) .addIngredient('R', new ResetItem())
.addIngredient('N', new ChangeNameItem()) .addIngredient('N', new ChangeNameItem())
.addIngredient('B', new ChangeBothItem()) .addIngredient('B', new ChangeBothItem())

View file

@ -22,7 +22,7 @@ public class PlayerInformationItem extends AsyncItem {
final Optional<NickoProfile> optionalProfile = dataStore.getData(uuid); final Optional<NickoProfile> optionalProfile = dataStore.getData(uuid);
optionalProfile.ifPresent(profile -> { optionalProfile.ifPresent(profile -> {
if (!profile.hasData()) { if (profile.hasData()) {
skull.addLoreLines( skull.addLoreLines(
"§cNicked: §a✔", "§cNicked: §a✔",
"§cName: §6" + profile.getName(), "§cName: §6" + profile.getName(),

View file

@ -1,17 +1,34 @@
package xyz.atnrch.nicko.gui.items.home; package xyz.atnrch.nicko.gui.items.home;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.ClickType;
import xyz.atnrch.nicko.i18n.I18N;
import xyz.atnrch.nicko.i18n.I18NDict;
import xyz.atnrch.nicko.i18n.ItemTranslation;
import xyz.xenondevs.invui.item.builder.ItemBuilder; import xyz.xenondevs.invui.item.builder.ItemBuilder;
import xyz.xenondevs.invui.item.impl.SimpleItem; import xyz.xenondevs.invui.item.impl.SuppliedItem;
public class ExitItem extends SimpleItem { public class ExitItem {
public ExitItem() { private final I18N i18n;
super(new ItemBuilder(Material.OAK_DOOR).setDisplayName("Exit"), click -> {
public ExitItem(Player player) {
this.i18n = new I18N(player);
}
public SuppliedItem get() {
return new SuppliedItem(() -> {
final ItemBuilder builder = new ItemBuilder(Material.OAK_DOOR);
final ItemTranslation translation = i18n.translateItem(I18NDict.GUI.EXIT);
builder.setDisplayName(translation.getName());
return builder;
}, click -> {
click.getEvent().getView().close();
final ClickType clickType = click.getClickType(); final ClickType clickType = click.getClickType();
if (clickType.isLeftClick() || clickType.isRightClick()) { if (clickType.isLeftClick() || clickType.isRightClick()) {
click.getEvent().getView().close(); click.getEvent().getView().close();
} }
return true;
}); });
} }
} }

View file

@ -28,7 +28,8 @@ event:
invalidate_entry: "§6{0} §fhas been invalidated." invalidate_entry: "§6{0} §fhas been invalidated."
gui: gui:
exit: "Exit" exit:
name: "Exit"
go_back: go_back:
name: "Back" name: "Back"
home: home:

View file

@ -28,7 +28,8 @@ event:
invalidate_entry: "§6{0} §fa été invalidé." invalidate_entry: "§6{0} §fa été invalidé."
gui: gui:
exit: "Quitter" exit:
name: "Quitter"
go_back: go_back:
name: "Retour" name: "Retour"
home: home: