refactor: much cleaner internals

This commit is contained in:
aro 2023-01-22 15:57:55 +01:00
parent a72b67be06
commit c7d6c57320
7 changed files with 110 additions and 169 deletions

View file

@ -2,9 +2,10 @@ package net.artelnatif.nicko.disguise;
import net.artelnatif.nicko.i18n.I18NDict;
public class ActionResult {
public class ActionResult<R> {
private final I18NDict errorMessage;
private boolean error = false;
private R result;
public ActionResult(I18NDict errorMessage) {
this.error = true;
@ -15,6 +16,14 @@ public class ActionResult {
this.errorMessage = null;
}
public void setResult(R result) {
this.result = result;
}
public R getResult() {
return result;
}
public boolean isError() {
return error;
}

View file

@ -1,13 +1,44 @@
package net.artelnatif.nicko.impl;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import org.bukkit.entity.Player;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public interface Internals {
void updateSelf(Player player);
void updateOthers(Player player);
ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset);
ActionResult<Void> updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset);
default ActionResult<MojangSkin> fetchSkinTextures(NickoProfile profile, boolean reset) {
Optional<MojangSkin> skin;
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
skin = (reset ? mojang.getSkinWithoutCaching(uuid.get()) : mojang.getSkin(uuid.get()));
if (skin.isEmpty()) {
return new ActionResult<>(I18NDict.Error.SKIN_FAIL_MOJANG);
}
final ActionResult<MojangSkin> actionResult = new ActionResult<>();
actionResult.setResult(skin.get());
return actionResult;
}
return new ActionResult<>(I18NDict.Error.NAME_FAIL_MOJANG);
} catch (ExecutionException e) {
return new ActionResult<>(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult<>(I18NDict.Error.NAME_FAIL_MOJANG);
}
}
}

View file

@ -3,11 +3,8 @@ package net.artelnatif.nicko.impl;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent;
import net.minecraft.network.protocol.game.*;
@ -24,10 +21,6 @@ import org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public class v1_17_R1 implements Internals {
@Override
public void updateSelf(Player player) {
@ -70,41 +63,28 @@ public class v1_17_R1 implements Internals {
}
@Override
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
public ActionResult<Void> updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
final String profileName = profile.getName() == null ? player.getName() : profile.getName();
Optional<MojangSkin> skin;
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final GameProfile gameProfile = new GameProfile(player.getUniqueId(), profileName);
if (skinChange || changeOnlyName) {
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
skin = (reset ? mojang.getSkinWithoutCaching(uuid.get()) : mojang.getSkin(uuid.get()));
if (skin.isPresent()) {
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult(I18NDict.Error.UNEXPECTED_ERROR);
final ActionResult<MojangSkin> skinFetch = fetchSkinTextures(profile, reset);
if (!skinFetch.isError()) {
final MojangSkin skin = skinFetch.getResult();
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.value(), skin.signature()));
updateSelf(player);
}
}
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
add.b().clear();
add.b().add(new PacketPlayOutPlayerInfo.PlayerInfoData(gameProfile,
player.getPing(),
@ -116,6 +96,6 @@ public class v1_17_R1 implements Internals {
onlineEntityPlayer.b.sendPacket(add);
});
updateOthers(player);
return new ActionResult();
return new ActionResult<>();
}
}

View file

@ -3,11 +3,8 @@ package net.artelnatif.nicko.impl;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent;
import net.minecraft.network.protocol.game.*;
@ -24,10 +21,6 @@ import org.bukkit.craftbukkit.v1_18_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public class v1_18_R1 implements Internals {
@Override
public void updateSelf(Player player) {
@ -72,41 +65,28 @@ public class v1_18_R1 implements Internals {
}
@Override
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
public ActionResult<Void> updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
final String profileName = profile.getName() == null ? player.getName() : profile.getName();
Optional<MojangSkin> skin;
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final GameProfile gameProfile = new GameProfile(player.getUniqueId(), profileName);
if (skinChange || changeOnlyName) {
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
skin = (reset ? mojang.getSkinWithoutCaching(uuid.get()) : mojang.getSkin(uuid.get()));
if (skin.isPresent()) {
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult(I18NDict.Error.UNEXPECTED_ERROR);
final ActionResult<MojangSkin> skinFetch = fetchSkinTextures(profile, reset);
if (!skinFetch.isError()) {
final MojangSkin skin = skinFetch.getResult();
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.value(), skin.signature()));
updateSelf(player);
}
}
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
add.b().clear();
add.b().add(new PacketPlayOutPlayerInfo.PlayerInfoData(gameProfile,
player.getPing(),
@ -118,6 +98,6 @@ public class v1_18_R1 implements Internals {
onlineEntityPlayer.b.a(add);
});
updateOthers(player);
return new ActionResult();
return new ActionResult<>();
}
}

View file

@ -3,11 +3,8 @@ package net.artelnatif.nicko.impl;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.core.Holder;
import net.minecraft.network.chat.IChatBaseComponent;
@ -25,10 +22,6 @@ import org.bukkit.craftbukkit.v1_18_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public class v1_18_R2 implements Internals {
@Override
public void updateSelf(Player player) {
@ -72,41 +65,28 @@ public class v1_18_R2 implements Internals {
}
@Override
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
public ActionResult<Void> updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
final String profileName = profile.getName() == null ? player.getName() : profile.getName();
Optional<MojangSkin> skin;
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final GameProfile gameProfile = new GameProfile(player.getUniqueId(), profileName);
if (skinChange || changeOnlyName) {
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
skin = (reset ? mojang.getSkinWithoutCaching(uuid.get()) : mojang.getSkin(uuid.get()));
if (skin.isPresent()) {
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult(I18NDict.Error.UNEXPECTED_ERROR);
final ActionResult<MojangSkin> skinFetch = fetchSkinTextures(profile, reset);
if (!skinFetch.isError()) {
final MojangSkin skin = skinFetch.getResult();
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.value(), skin.signature()));
updateSelf(player);
}
}
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
add.b().clear();
add.b().add(new PacketPlayOutPlayerInfo.PlayerInfoData(gameProfile,
player.getPing(),
@ -118,6 +98,6 @@ public class v1_18_R2 implements Internals {
onlineEntityPlayer.b.a(add);
});
updateOthers(player);
return new ActionResult();
return new ActionResult<>();
}
}
}

View file

@ -3,11 +3,8 @@ package net.artelnatif.nicko.impl;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.IChatBaseComponent;
import net.minecraft.network.protocol.game.*;
@ -24,9 +21,7 @@ import org.bukkit.craftbukkit.v1_19_R1.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
public class v1_19_R1 implements Internals {
@Override
@ -70,44 +65,29 @@ public class v1_19_R1 implements Internals {
}
@Override
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
public ActionResult<Void> updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
final String profileName = profile.getName() == null ? player.getName() : profile.getName();
Optional<MojangSkin> skin;
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
// "It's a Surprise Tool That Will Help Us Later!"
final ProfilePublicKey.a key = remove.b().get(0).e();
final CraftPlayer craftPlayer = (CraftPlayer) player;
final EntityPlayer entityPlayer = craftPlayer.getHandle();
final GameProfile gameProfile = new GameProfile(player.getUniqueId(), profileName);
if (skinChange || changeOnlyName) {
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
skin = (reset ? mojang.getSkinWithoutCaching(uuid.get()) : mojang.getSkin(uuid.get()));
if (skin.isPresent()) {
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
final ActionResult<MojangSkin> skinFetch = fetchSkinTextures(profile, reset);
if (!skinFetch.isError()) {
final MojangSkin skin = skinFetch.getResult();
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.value(), skin.signature()));
updateSelf(player);
}
}
final PacketPlayOutPlayerInfo add = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.a);
final PacketPlayOutPlayerInfo remove = new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.e, entityPlayer);
// "It's a Surprise Tool That Will Help Us Later!"
final ProfilePublicKey.a key = remove.b().get(0).e();
add.b().clear();
add.b().add(new PacketPlayOutPlayerInfo.PlayerInfoData(gameProfile,
@ -122,6 +102,6 @@ public class v1_19_R1 implements Internals {
onlineEntityPlayer.b.a(add);
});
updateOthers(player);
return new ActionResult();
return new ActionResult<>();
}
}

View file

@ -6,8 +6,6 @@ import com.mojang.authlib.properties.PropertyMap;
import net.artelnatif.nicko.NickoBukkit;
import net.artelnatif.nicko.disguise.ActionResult;
import net.artelnatif.nicko.disguise.NickoProfile;
import net.artelnatif.nicko.i18n.I18NDict;
import net.artelnatif.nicko.mojang.MojangAPI;
import net.artelnatif.nicko.mojang.MojangSkin;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.RemoteChatSession;
@ -25,10 +23,8 @@ import org.bukkit.craftbukkit.v1_19_R2.entity.CraftPlayer;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.ExecutionException;
public class v1_19_R2 implements Internals {
@Override
@ -73,37 +69,21 @@ public class v1_19_R2 implements Internals {
}
@Override
public ActionResult updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
public ActionResult<Void> updateProfile(Player player, NickoProfile profile, boolean skinChange, boolean reset) {
final boolean changeOnlyName = profile.getSkin() != null && !profile.getSkin().equalsIgnoreCase(player.getName());
final String profileName = profile.getName() == null ? player.getName() : profile.getName();
Optional<MojangSkin> skin;
final ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle();
final GameProfile gameProfile = new GameProfile(player.getUniqueId(), profileName);
final ClientboundPlayerInfoRemovePacket remove = new ClientboundPlayerInfoRemovePacket(List.of(player.getUniqueId()));
if (skinChange || changeOnlyName) {
try {
final MojangAPI mojang = NickoBukkit.getInstance().getMojangAPI();
final Optional<String> uuid = mojang.getUUID(profile.getSkin());
if (uuid.isPresent()) {
skin = (reset ? mojang.getSkinWithoutCaching(uuid.get()) : mojang.getSkin(uuid.get()));
if (skin.isPresent()) {
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.get().value(), skin.get().signature()));
updateSelf(player);
} else {
return new ActionResult(I18NDict.Error.SKIN_FAIL_MOJANG);
}
} else {
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
}
} catch (ExecutionException e) {
return new ActionResult(I18NDict.Error.SKIN_FAIL_CACHE);
} catch (IOException e) {
return new ActionResult(I18NDict.Error.NAME_FAIL_MOJANG);
final ActionResult<MojangSkin> skinFetch = fetchSkinTextures(profile, reset);
if (!skinFetch.isError()) {
final MojangSkin skin = skinFetch.getResult();
final PropertyMap properties = gameProfile.getProperties();
properties.removeAll("textures");
properties.put("textures", new Property("textures", skin.value(), skin.signature()));
updateSelf(player);
}
}
@ -113,6 +93,7 @@ public class v1_19_R2 implements Internals {
ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY,
ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LISTED),
Collections.singletonList(serverPlayer));
final ClientboundPlayerInfoRemovePacket remove = new ClientboundPlayerInfoRemovePacket(List.of(player.getUniqueId()));
if (serverPlayer.getChatSession() == null) {
NickoBukkit.getInstance().getLogger().warning("Chat Session of " + serverPlayer.displayName + " is undefined." +
@ -142,7 +123,7 @@ public class v1_19_R2 implements Internals {
onlinePlayer.connection.send(init);
});
updateOthers(player);
return new ActionResult();
return new ActionResult<>();
}
private void spoofPlayerInfoPacket(Object object, Object newValue) {