feat: reset disguise

This commit is contained in:
aroooo 2022-11-04 16:18:56 +01:00
parent 9e64dc74f0
commit 54671b62e5
14 changed files with 159 additions and 27 deletions

8
README.md Normal file
View file

@ -0,0 +1,8 @@
# Nicko
## The most complete disguising plugin in Minecraft.
### Commands
> /nicko
#### Permissions

View file

@ -88,6 +88,7 @@ public class NickoBukkit extends JavaPlugin {
public void onDisable() {
if (!dataStore.getStorage().isError()) {
getLogger().info("Closing persistence...");
dataStore.removeAllNames();
if (!dataStore.getStorage().getProvider().close()) {
getLogger().warning("Failed to close persistence!");
}

View file

@ -27,13 +27,13 @@ public class NickoCheckSubCmd extends NickoSubCmd {
}
final StringJoiner builder = new StringJoiner("\n");
builder.add("§c" + NickoBukkit.getInstance().getNickoConfig().getPrefix() + "§f- §6Check for:§f§o" + targetName);
builder.add("§c" + NickoBukkit.getInstance().getNickoConfig().getPrefix() + "§6Check for: §f§o" + targetName);
if (!appearanceManager.hasData()) {
builder.add("§cThis player has not data.");
} else {
builder.add("§7- §fNicked: " + (appearanceManager.isNicked() ? "§a✔" : "§c❌"));
builder.add("§7- §fNickname: §6" + (appearanceManager.getName().equals(targetName) ? "N/A" : appearanceManager.getName()));
builder.add("§7- §fSkin: §6" + (appearanceManager.getSkin().equals(targetName) ? "N/A" : appearanceManager.getSkin()));
builder.add("§7- §fNicked: " + (appearanceManager.hasData() ? "§a✔" : "§c❌"));
builder.add("§7- §fName: §6" + appearanceManager.getName());
builder.add("§7- §fSkin: §6" + appearanceManager.getSkin());
}
sender.sendMessage(builder.toString());

View file

@ -35,10 +35,6 @@ public class AppearanceManager {
return !profile.isEmpty();
}
public boolean isNicked() {
return hasData() && !profile.getSkin().equals(player.getName()) || !profile.getName().equals(player.getName());
}
public void setSkin(String skin) {
profile.setSkin(skin);
}
@ -65,6 +61,16 @@ public class AppearanceManager {
updatePlayer(true);
}
public UpdateResult reset() {
final String defaultName = instance.getDataStore().getStoredName(player);
this.profile.setName(defaultName);
this.profile.setSkin(defaultName);
final UpdateResult updateResult = updatePlayer(true);
this.profile.setSkin(null);
this.profile.setName(null);
return updateResult;
}
public UpdateResult updatePlayer(boolean skinChange) {
return NickoBukkit.getInstance().getInternals().updateProfile(player, profile, skinChange);
}

View file

@ -15,6 +15,7 @@ public class PlayerJoinListener implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
final Player player = event.getPlayer();
NickoBukkit.getInstance().getDataStore().storeName(player);
Bukkit.getScheduler().runTaskLater(NickoBukkit.getInstance(), () -> {
final AppearanceManager appearanceManager = AppearanceManager.get(player);

View file

@ -5,23 +5,29 @@ import de.studiocode.invui.gui.builder.GUIBuilder;
import de.studiocode.invui.gui.builder.guitype.GUIType;
import de.studiocode.invui.gui.structure.Structure;
import de.studiocode.invui.window.impl.single.SimpleWindow;
import net.artelnatif.nicko.gui.items.common.BackItem;
import org.bukkit.entity.Player;
public class AdminPanelGUI {
private final Player player;
private final GUI gui;
private String[] structureIngredients = new String[]
{"# # # # # # # # #",
"# % % M C R % % #",
"E # # # # # # # #"};
public AdminPanelGUI(Player player) {
final Structure structure = new Structure("# # # # # # # # #",
"# % % M C R % % #",
"B # # # # # # # #");
structure.addIngredient('B', new BackItem(new MainGUI(player).getGUI()));
this.gui = new GUIBuilder<>(GUIType.NORMAL)
.setStructure(new Structure(structureIngredients))
.setStructure(structure)
.build();
this.player = player;
}
public GUI getGUI() {
return gui;
}
public void open() {
new SimpleWindow(player, "Nicko", gui).show();
}

View file

@ -12,9 +12,9 @@ public class MainGUI {
private final Player player;
private final GUI gui;
private final String[] structureIngredients = new String[]
{"# # # # # # # # #",
{"# # # # # # # # #",
"# % % % % % % % #",
"# % # # B # # % #",
"# % # R B # # % #",
"# % # N A S # % #",
"# % % % % % % % #",
"E # # # # # # # #"};
@ -31,10 +31,16 @@ public class MainGUI {
.addIngredient('S', new ChangeSkinItem())
.addIngredient('A', new AdminPanelAccessItem())
.addIngredient('B', new ChangeNameAndSkinItem())
.addIngredient('R', new ResetItem())
// TODO: 11/3/22 Add possibility to reset either skin or name or both
.build();
this.player = player;
}
public GUI getGUI() {
return gui;
}
public void open() {
new SimpleWindow(player, "Nicko", gui).show();
}

View file

@ -0,0 +1,34 @@
package net.artelnatif.nicko.gui.items.common;
import de.studiocode.invui.gui.GUI;
import de.studiocode.invui.item.ItemProvider;
import de.studiocode.invui.item.builder.ItemBuilder;
import de.studiocode.invui.item.impl.BaseItem;
import de.studiocode.invui.window.impl.single.SimpleWindow;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
public class BackItem extends BaseItem {
private final GUI gui;
public BackItem(GUI gui) {
this.gui = gui;
}
@Override
public ItemProvider getItemProvider() {
final ItemBuilder builder = new ItemBuilder(Material.ARROW);
builder.setDisplayName("Go back");
builder.addLoreLines("§7Return to the previous window.");
return builder;
}
@Override
public void handleClick(@NotNull ClickType clickType, @NotNull Player player, @NotNull InventoryClickEvent event) {
event.getView().close();
new SimpleWindow(player, "Nicko", gui).show();
}
}

View file

@ -14,7 +14,7 @@ public class AdminPanelAccessItem extends BaseItem {
@Override
public ItemProvider getItemProvider() {
final ItemBuilder builder = new ItemBuilder(Material.PISTON);
builder.setDisplayName("§cAdministration panel");
builder.setDisplayName("§cAdministration panel...");
builder.addLoreLines("§7Access the administration panel.");
return builder;
}

View file

@ -0,0 +1,42 @@
package net.artelnatif.nicko.gui.items.main;
import de.studiocode.invui.item.ItemProvider;
import de.studiocode.invui.item.builder.ItemBuilder;
import de.studiocode.invui.item.impl.BaseItem;
import net.artelnatif.nicko.disguise.AppearanceManager;
import net.artelnatif.nicko.i18n.I18N;
import net.artelnatif.nicko.i18n.I18NDict;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.jetbrains.annotations.NotNull;
public class ResetItem extends BaseItem {
@Override
public ItemProvider getItemProvider() {
final ItemBuilder builder = new ItemBuilder(Material.TNT);
builder.setDisplayName("§6Reset");
builder.addLoreLines("§7Removes your disguise.");
return builder;
}
@Override
public void handleClick(@NotNull ClickType clickType, @NotNull Player player, @NotNull InventoryClickEvent event) {
if (clickType.isLeftClick() || clickType.isRightClick()) {
final AppearanceManager appearanceManager = AppearanceManager.get(player);
if (!appearanceManager.hasData()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_NOTACTIVE.getKey()));
event.getView().close();
return;
}
if (!appearanceManager.reset().isError()) {
player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_SUCCESS.getKey()));
} else {
player.sendMessage(I18N.translate(player, I18NDict.Event.UNDISGUISE_FAIL.getKey()));
}
}
}
}

View file

@ -2,6 +2,9 @@ package net.artelnatif.nicko.i18n;
public class I18NDict {
public enum Event {
UNDISGUISE_SUCCESS("undisguise.success"),
UNDISGUISE_FAIL("undisguise.fail"),
UNDISGUISE_NOTACTIVE("undisguise.notactive"),
DISGUISE_SUCCESS("disguise.success"),
DISGUISE_FAIL("disguise.fail"),
PREVIOUS_SKIN_APPLIED("previous_skin_applied.success"),

View file

@ -14,27 +14,46 @@ import java.util.UUID;
public class PlayerDataStore {
private final Storage storage;
private final HashMap<UUID, NickoProfile> profiles = new HashMap<>();
private final HashMap<UUID, String> names = new HashMap<>();
public PlayerDataStore(NickoBukkit instance) {
this.storage = instance.getNickoConfig().isLocalStorage() ? new JSONStorage() : new SQLStorage(instance);
}
public static final HashMap<UUID, NickoProfile> PROFILES = new HashMap<>();
public void storeName(Player player) {
if(!isNameStored(player)) {
names.put(player.getUniqueId(), player.getName());
}
}
public String getStoredName(Player player) {
return names.get(player.getUniqueId());
}
private boolean isNameStored(Player player) {
return names.containsKey(player.getUniqueId());
}
public void removeAllNames() {
names.clear();
}
public Optional<NickoProfile> getData(UUID uuid) {
if (storage.isError()) {
return Optional.empty();
}
if (PROFILES.containsKey(uuid)) {
return Optional.of(PROFILES.get(uuid));
if (profiles.containsKey(uuid)) {
return Optional.of(profiles.get(uuid));
} else if (storage.isStored(uuid)) {
final Optional<NickoProfile> retrievedProfile = storage.retrieve(uuid);
retrievedProfile.ifPresent(profile -> PROFILES.put(uuid, profile));
retrievedProfile.ifPresent(profile -> profiles.put(uuid, profile));
return retrievedProfile;
} else {
final NickoProfile newProfile = NickoProfile.EMPTY_PROFILE;
PROFILES.put(uuid, newProfile);
profiles.put(uuid, newProfile);
return Optional.of(newProfile);
}
}
@ -61,8 +80,8 @@ public class PlayerDataStore {
return;
}
storage.store(player.getUniqueId(), PROFILES.get(player.getUniqueId()));
PROFILES.remove(player.getUniqueId());
storage.store(player.getUniqueId(), profiles.get(player.getUniqueId()));
profiles.remove(player.getUniqueId());
}
public Storage getStorage() {

View file

@ -5,5 +5,8 @@ error.generic=§cAn unknown error occured. Please contact the developer.
error.couldnt_get_name_from_mojang=§cFailed to get username from Mojang. Does the user exists?
error.couldnt_get_skin_from_mojang=§cFailed to get skin from Mojang.
error.couldnt_get_skin_from_cache=§cFailed to get skin from cache.
event.disguise.success=§aDisguise applied.
event.disguise.success=§aDisguise applied !
event.disguise.fail=§cUnable to apply your disguise. §7§o({0})
event.undisguise.success=§aDisguise removed.
event.undisguise.fail=§cUnable to remove your disguise. It will be set back to default on the next login. Sorry!
event.undisguise.notactive=§cYou do not have an active disguise.

View file

@ -5,5 +5,8 @@ error.generic=§cUne erreur inconnue est survenue. Veuillez contacter le dévelo
error.couldnt_get_name_from_mojang=§cImpossible de récupérer le nom d'utilisateur depuis Mojang. Cet utilisateur existe-il?
error.couldnt_get_skin_from_mojang=§cImpossible de récupérer le skin depuis Mojang.
error.couldnt_get_skin_from_cache=§cImpossible de récupérer le skin depuis le cache.
event.disguise.success=§aDéguisement appliqué avec succès.
event.disguise.success=§aDéguisement appliqué !
event.disguise.fail=§cImpossible d'appliquer votre déguisement. §7§o({0})
event.undisguise.success=§aDéguisement retiré.
event.undisguise.fail=§cImpossible de retier votre déguisement. Il sera remis par défaut à votre prochaine reconnexion. Désolé !
event.undisguise.notactive=§cVous n'avez pas de déguisement.