diff --git a/src/main/java/de/bixilon/minosoft/data/PlayerPropertyData.java b/src/main/java/de/bixilon/minosoft/data/PlayerPropertyData.java deleted file mode 100644 index b641a5b5e..000000000 --- a/src/main/java/de/bixilon/minosoft/data/PlayerPropertyData.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.data; - -public class PlayerPropertyData { - private final String name; - private final String value; - private final String signature; - - public PlayerPropertyData(String name, String value, String signature) { - this.name = name; - this.value = value; - this.signature = signature; - } - - public String getName() { - return this.name; - } - - public String getValue() { - return this.value; - } - - public String getSignature() { - return this.signature; - } - - @Override - public String toString() { - return String.format("%s=%s", getName(), getValue()); - } -} diff --git a/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt b/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt index 446d6598e..bed42d6e1 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt +++ b/src/main/java/de/bixilon/minosoft/data/text/BaseComponent.kt @@ -18,8 +18,6 @@ import com.google.gson.JsonObject import de.bixilon.minosoft.data.locale.minecraft.Translator import de.bixilon.minosoft.data.text.RGBColor.Companion.asColor import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition -import de.bixilon.minosoft.util.KUtil.nullCast -import glm_.vec2.Vec2i import javafx.collections.ObservableList import javafx.scene.Node import java.text.CharacterIterator diff --git a/src/main/java/de/bixilon/minosoft/data/text/ClickEvent.java b/src/main/java/de/bixilon/minosoft/data/text/ClickEvent.java deleted file mode 100644 index a83b14b9d..000000000 --- a/src/main/java/de/bixilon/minosoft/data/text/ClickEvent.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.data.text; - -import com.google.gson.JsonObject; -import com.google.gson.JsonPrimitive; - -public class ClickEvent { - private final ClickEventActions action; - private final Object value; - - public ClickEvent(JsonObject json) { - this.action = ClickEventActions.valueOf(json.get("action").getAsString().toUpperCase()); - JsonPrimitive primitive = json.get("value").getAsJsonPrimitive(); - if (primitive.isNumber()) { - this.value = primitive.getAsNumber(); - } else { - this.value = primitive.getAsString(); - } - } - - public ClickEvent(ClickEventActions action, Object value) { - this.action = action; - this.value = value; - } - - public ClickEventActions getAction() { - return this.action; - } - - public Object getValue() { - return this.value; - } - - public enum ClickEventActions { - OPEN_URL, - RUN_COMMAND, - SUGGEST_COMMAND, - CHANGE_PAGE - } -} diff --git a/src/main/java/de/bixilon/minosoft/data/text/HoverEvent.java b/src/main/java/de/bixilon/minosoft/data/text/HoverEvent.java deleted file mode 100644 index e17920930..000000000 --- a/src/main/java/de/bixilon/minosoft/data/text/HoverEvent.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 Moritz Zwerger - * - * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with this program. If not, see . - * - * This software is not affiliated with Mojang AB, the original developer of Minecraft. - */ - -package de.bixilon.minosoft.data.text; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; -import com.google.gson.JsonPrimitive; -import de.bixilon.minosoft.data.mappings.ResourceLocation; -import de.bixilon.minosoft.util.Util; - -import java.util.UUID; - -public class HoverEvent { - private final HoverEventActions action; - private final Object value; // TextComponent, NBT, Entity, Achievement Id - - public HoverEvent(JsonObject json) { - this.action = HoverEventActions.valueOf(json.get("action").getAsString().toUpperCase()); - JsonElement data = null; - if (json.has("value")) { - data = json.get("value"); - } - if (json.has("contents")) { - data = json.get("contents"); - } - json.get("value"); - this.value = switch (this.action) { // ToDo - case SHOW_TEXT -> ChatComponent.Companion.of(data); - case SHOW_ENTITY -> EntityHoverData.deserialize(data); - default -> null; - }; - } - - public HoverEvent(HoverEventActions action, Object value) { - this.action = action; - if (!(value instanceof ChatComponent) && !(value instanceof EntityHoverData)) { - throw new IllegalArgumentException(String.format("%s is not a valid value hier", value.getClass().getSimpleName())); - } - this.value = value; - } - - public Object getValue() { - return this.value; - } - - public enum HoverEventActions { - SHOW_TEXT, - SHOW_ITEM, - SHOW_ENTITY, - SHOW_ACHIEVEMENT - } - - public static final class EntityHoverData { - private final UUID uuid; - private final ResourceLocation resourceLocation; - private final ChatComponent name; - - public EntityHoverData(UUID uuid, ResourceLocation resourceLocation, ChatComponent name) { - this.uuid = uuid; - this.resourceLocation = resourceLocation; - this.name = name; - } - - public static EntityHoverData deserialize(JsonElement data) { - JsonObject json; - if (data instanceof JsonPrimitive) { - json = JsonParser.parseString(data.getAsString()).getAsJsonObject(); - } else { - json = (JsonObject) data; - } - if (json.has("text")) { - // 1.14.3.... lol - json = JsonParser.parseString(json.get("text").getAsString()).getAsJsonObject(); - } - ResourceLocation type = null; - if (json.has("type")) { - type = new ResourceLocation(json.get("type").getAsString()); - } - return new EntityHoverData(Util.getUUIDFromString(json.get("id").getAsString()), type, ChatComponent.Companion.of(json.get("name"))); - } - - public UUID uuid() { - return this.uuid; - } - - public ResourceLocation resourceLocation() { - return this.resourceLocation; - } - - public ChatComponent name() { - return this.name; - } - } -} diff --git a/src/main/java/de/bixilon/minosoft/data/text/MultiChatComponent.kt b/src/main/java/de/bixilon/minosoft/data/text/MultiChatComponent.kt index d06eb658b..e868f333c 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/MultiChatComponent.kt +++ b/src/main/java/de/bixilon/minosoft/data/text/MultiChatComponent.kt @@ -12,6 +12,9 @@ */ package de.bixilon.minosoft.data.text +import de.bixilon.minosoft.data.text.events.ClickEvent +import de.bixilon.minosoft.data.text.events.HoverEvent + class MultiChatComponent( message: String = "", color: RGBColor? = null, diff --git a/src/main/java/de/bixilon/minosoft/data/text/events/ClickEvent.kt b/src/main/java/de/bixilon/minosoft/data/text/events/ClickEvent.kt new file mode 100644 index 000000000..2c2d9ea73 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/text/events/ClickEvent.kt @@ -0,0 +1,50 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.data.text.events + +import com.google.gson.JsonObject +import de.bixilon.minosoft.util.KUtil +import de.bixilon.minosoft.util.enum.ValuesEnum + +class ClickEvent { + val action: ClickEventActions + val value: Any + + constructor(json: JsonObject) { + action = ClickEventActions[json["action"].asString.lowercase()] + val primitive = json["value"].asJsonPrimitive + value = if (primitive.isNumber) { + primitive.asNumber + } else { + primitive.asString + } + } + + constructor(action: ClickEventActions, value: Any) { + this.action = action + this.value = value + } + + enum class ClickEventActions { + OPEN_URL, + RUN_COMMAND, + SUGGEST_COMMAND, + CHANGE_PAGE, + ; + + companion object : ValuesEnum { + override val VALUES: Array = values() + override val NAME_MAP: Map = KUtil.getEnumValues(VALUES) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/text/events/HoverEvent.kt b/src/main/java/de/bixilon/minosoft/data/text/events/HoverEvent.kt new file mode 100644 index 000000000..416da5e64 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/text/events/HoverEvent.kt @@ -0,0 +1,60 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.data.text.events + +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import de.bixilon.minosoft.data.text.ChatComponent +import de.bixilon.minosoft.data.text.events.data.EntityHoverData +import de.bixilon.minosoft.util.KUtil +import de.bixilon.minosoft.util.enum.ValuesEnum +import java.util.* + +class HoverEvent { + val action: HoverEventActions + val value: Any + + constructor(json: JsonObject) { + action = HoverEventActions.valueOf(json["action"].asString.uppercase(Locale.getDefault())) + var data: JsonElement = json + json["value"]?.let { + data = it + } + json["contents"]?.let { + data = it + } + this.value = when (action) { + HoverEventActions.SHOW_TEXT -> ChatComponent.of(data) + HoverEventActions.SHOW_ENTITY -> EntityHoverData.deserialize(data) + else -> TODO("Don't know what todo with $action: $data") + } + } + + constructor(action: HoverEventActions, value: Any) { + this.action = action + this.value = value + } + + enum class HoverEventActions { + SHOW_TEXT, + SHOW_ITEM, + SHOW_ENTITY, + SHOW_ACHIEVEMENT, + ; + + companion object : ValuesEnum { + override val VALUES: Array = values() + override val NAME_MAP: Map = KUtil.getEnumValues(VALUES) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/text/events/data/EntityHoverData.kt b/src/main/java/de/bixilon/minosoft/data/text/events/data/EntityHoverData.kt new file mode 100644 index 000000000..ecc7cedb8 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/text/events/data/EntityHoverData.kt @@ -0,0 +1,50 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ +package de.bixilon.minosoft.data.text.events.data + +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.google.gson.JsonPrimitive +import de.bixilon.minosoft.data.mappings.ResourceLocation +import de.bixilon.minosoft.data.text.ChatComponent +import de.bixilon.minosoft.util.KUtil.asResourceLocation +import de.bixilon.minosoft.util.KUtil.asUUID +import java.util.* + +class EntityHoverData( + val uuid: UUID, + val resourceLocation: ResourceLocation?, + val name: ChatComponent, +) { + + companion object { + fun deserialize(data: JsonElement): EntityHoverData { + var json = if (data is JsonPrimitive) { + JsonParser.parseString(data.getAsString()).asJsonObject + } else { + data as JsonObject + } + json["text"]?.let { + // 1.14.3.... lol + json = JsonParser.parseString(json["text"].asString).asJsonObject + } + var type: ResourceLocation? = null + json["type"]?.asString?.let { + type = it.asResourceLocation() + } + + return EntityHoverData(json["id"].asString.asUUID(), type, ChatComponent.of(json["name"])) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerEntitySpawnS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerEntitySpawnS2CP.kt index fee8358f4..b40617022 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerEntitySpawnS2CP.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerEntitySpawnS2CP.kt @@ -13,11 +13,11 @@ package de.bixilon.minosoft.protocol.packets.s2c.play import de.bixilon.minosoft.config.StaticConfiguration -import de.bixilon.minosoft.data.PlayerPropertyData import de.bixilon.minosoft.data.entities.EntityRotation import de.bixilon.minosoft.data.entities.entities.player.PlayerEntity import de.bixilon.minosoft.data.entities.entities.player.RemotePlayerEntity import de.bixilon.minosoft.data.entities.meta.EntityMetaData +import de.bixilon.minosoft.data.player.PlayerProperty import de.bixilon.minosoft.modding.event.events.EntitySpawnEvent import de.bixilon.minosoft.protocol.network.connection.PlayConnection import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket @@ -39,13 +39,14 @@ class PlayerEntitySpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() { entityId = buffer.readVarInt() var name = "TBA" - val properties: MutableSet = mutableSetOf() + val properties: MutableMap = mutableMapOf() if (buffer.versionId < ProtocolVersions.V_14W21A) { name = buffer.readString() entityUUID = Util.getUUIDFromString(buffer.readString()) val length = buffer.readVarInt() for (i in 0 until length) { - properties.add(PlayerPropertyData(buffer.readString(), buffer.readString(), buffer.readString())) + val property = PlayerProperty(buffer.readString(), buffer.readString(), buffer.readString()) + properties[property.key] = property } } else { entityUUID = buffer.readUUID() @@ -73,7 +74,7 @@ class PlayerEntitySpawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() { position = position, rotation = EntityRotation(yaw.toFloat(), pitch.toFloat(), 0.0f), name = name, - // ToDo: properties = properties, + properties = properties, ) if (metaData != null) {