code: improve PacketTitle a bit

This commit is contained in:
Bixilon 2021-03-30 18:31:18 +02:00
parent bb5da427c5
commit fb88d0271d
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 110 additions and 129 deletions

View File

@ -16,6 +16,7 @@ package de.bixilon.minosoft.data.mappings
import de.bixilon.minosoft.Minosoft
import de.bixilon.minosoft.data.entities.meta.EntityMetaData
import de.bixilon.minosoft.data.inventory.InventorySlots
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketTitle
import de.bixilon.minosoft.util.json.ResourceLocationJsonMap.toResourceLocationMap
object DefaultRegistries {
@ -26,8 +27,11 @@ object DefaultRegistries {
val HAND_EQUIPMENT_SLOTS_REGISTRY = PerVersionRegistry(InventorySlots.EquipmentSlots)
val ARMOR_EQUIPMENT_SLOTS_REGISTRY = PerVersionRegistry(InventorySlots.EquipmentSlots)
val ARMOR_STAND_EQUIPMENT_SLOTS_REGISTRY = PerVersionRegistry(InventorySlots.EquipmentSlots)
val ENTITY_META_DATA_DATA_TYPES_REGISTRY = PerVersionRegistry(EntityMetaData.EntityMetaDataDataTypes)
val TITLE_ACTIONS_REGISTRY = PerVersionRegistry(PacketTitle.TitleActions)
fun load() {
check(!initialized) { "Already initialized!" }
@ -38,8 +42,11 @@ object DefaultRegistries {
HAND_EQUIPMENT_SLOTS_REGISTRY.initialize(enumJson[ResourceLocation("hand_equipment_slots")]!!)
ARMOR_EQUIPMENT_SLOTS_REGISTRY.initialize(enumJson[ResourceLocation("armor_equipment_slots")]!!)
ARMOR_STAND_EQUIPMENT_SLOTS_REGISTRY.initialize(enumJson[ResourceLocation("armor_stand_equipment_slots")]!!)
ENTITY_META_DATA_DATA_TYPES_REGISTRY.initialize(enumJson[ResourceLocation("entity_meta_data_data_types")]!!)
TITLE_ACTIONS_REGISTRY.initialize(enumJson[ResourceLocation("title_actions")]!!)
initialized = true
}

View File

@ -40,6 +40,7 @@ import de.bixilon.minosoft.data.mappings.statistics.Statistic
import de.bixilon.minosoft.gui.rendering.chunk.VoxelShape
import de.bixilon.minosoft.gui.rendering.chunk.models.AABB
import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModel
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketTitle
import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
import de.bixilon.minosoft.util.collections.Clearable
import de.bixilon.minosoft.util.json.ResourceLocationJsonMap.toResourceLocationMap
@ -66,6 +67,8 @@ class VersionMapping(var version: Version?) {
val entityMetaDataDataDataTypesRegistry: EnumRegistry<EntityMetaData.EntityMetaDataDataTypes> = EnumRegistry(values = EntityMetaData.EntityMetaDataDataTypes)
val titleActionsRegistry: EnumRegistry<PacketTitle.TitleActions> = EnumRegistry(values = PacketTitle.TitleActions)
val creativeModeTabRegistry: FakeEnumRegistry<CreativeModeTab> = FakeEnumRegistry()
val biomePrecipitationRegistry: FakeEnumRegistry<BiomePrecipitation> = FakeEnumRegistry()
@ -110,6 +113,8 @@ class VersionMapping(var version: Version?) {
armorStandEquipmentSlotRegistry.setParent(value?.armorStandEquipmentSlotRegistry)
entityMetaDataDataDataTypesRegistry.setParent(value?.entityMetaDataDataDataTypesRegistry)
creativeModeTabRegistry.setParent(value?.creativeModeTabRegistry)
titleActionsRegistry.setParent(value?.titleActionsRegistry)
}
fun getBlockState(blockState: Int): BlockState? {
@ -154,6 +159,8 @@ class VersionMapping(var version: Version?) {
loadEnumRegistry(pixlyzerData["entity_meta_data_data_types"], entityMetaDataDataDataTypesRegistry, DefaultRegistries.ENTITY_META_DATA_DATA_TYPES_REGISTRY)
loadEnumRegistry(pixlyzerData["title_actions"], titleActionsRegistry, DefaultRegistries.TITLE_ACTIONS_REGISTRY)
// id stuff
biomeCategoryRegistry.initialize(pixlyzerData["biome_categories"]?.asJsonObject, this, BiomeCategory.Companion)
biomePrecipitationRegistry.initialize(pixlyzerData["biome_precipations"]?.asJsonObject, this, BiomePrecipitation.Companion)

View File

@ -1,129 +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 <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play;
import de.bixilon.minosoft.data.VersionValueMap;
import de.bixilon.minosoft.data.text.ChatComponent;
import de.bixilon.minosoft.modding.event.events.TitleChangeEvent;
import de.bixilon.minosoft.protocol.network.Connection;
import de.bixilon.minosoft.protocol.packets.ClientboundPacket;
import de.bixilon.minosoft.protocol.protocol.InByteBuffer;
import de.bixilon.minosoft.util.logging.Log;
import java.util.Map;
import static de.bixilon.minosoft.protocol.protocol.ProtocolVersions.*;
public class PacketTitle extends ClientboundPacket {
TitleActions action;
// fields depend on action
ChatComponent text;
ChatComponent subText;
int fadeInTime;
int stayTime;
int fadeOutTime;
@Override
public boolean read(InByteBuffer buffer) {
this.action = TitleActions.byId(buffer.readVarInt(), buffer.getVersionId());
switch (this.action) {
case SET_TITLE -> this.text = buffer.readChatComponent();
case SET_SUBTITLE -> this.subText = buffer.readChatComponent();
case SET_TIMES_AND_DISPLAY -> {
this.fadeInTime = buffer.readInt();
this.stayTime = buffer.readInt();
this.fadeOutTime = buffer.readInt();
}
}
return true;
}
@Override
public void handle(Connection connection) {
if (connection.fireEvent(new TitleChangeEvent(connection, this))) {
return;
}
}
@Override
public void log() {
switch (this.action) {
case SET_TITLE -> Log.protocol(String.format("[IN] Received title (action=%s, text=%s)", this.action, this.text.getANSIColoredMessage()));
case SET_SUBTITLE -> Log.protocol(String.format("[IN] Received title (action=%s, subText=%s)", this.action, this.subText.getANSIColoredMessage()));
case SET_TIMES_AND_DISPLAY -> Log.protocol(String.format("[IN] Received title (action=%s, fadeInTime=%d, stayTime=%d, fadeOutTime=%d)", this.action, this.fadeInTime, this.stayTime, this.fadeOutTime));
case HIDE, RESET -> Log.protocol(String.format("[IN] Received title (action=%s)", this.action));
}
}
public int getFadeInTime() {
return this.fadeInTime;
}
public int getFadeOutTime() {
return this.fadeOutTime;
}
public int getStayTime() {
return this.stayTime;
}
public ChatComponent getSubText() {
return this.subText;
}
public ChatComponent getText() {
return this.text;
}
public TitleActions getAction() {
return this.action;
}
public enum TitleActions {
SET_TITLE(0),
SET_SUBTITLE(1),
SET_ACTION_BAR(Map.of(V_16W32B, 2)),
SET_TIMES_AND_DISPLAY(Map.of(V_14W20B, 2, V_16W32B, 3)),
HIDE(Map.of(V_14W20B, 3, V_16W32B, 4)),
RESET(Map.of(V_14W20B, 4, V_16W32B, 5));
private final VersionValueMap<Integer> valueMap;
TitleActions(Map<Integer, Integer> values) {
this.valueMap = new VersionValueMap<>(values);
}
TitleActions(int id) {
this.valueMap = new VersionValueMap<>(Map.of(LOWEST_VERSION_SUPPORTED, id));
}
public static TitleActions byId(int id, int versionId) {
for (TitleActions action : values()) {
if (action.getId(versionId) == id) {
return action;
}
}
return null;
}
public int getId(int versionId) {
Integer ret = this.valueMap.get(versionId);
if (ret == null) {
return -2;
}
return ret;
}
}
}

View File

@ -0,0 +1,79 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.protocol.packets.clientbound.play
import de.bixilon.minosoft.data.text.ChatComponent
import de.bixilon.minosoft.modding.event.events.TitleChangeEvent
import de.bixilon.minosoft.protocol.network.Connection
import de.bixilon.minosoft.protocol.packets.ClientboundPacket
import de.bixilon.minosoft.protocol.packets.clientbound.play.PacketTitle.TitleActions
import de.bixilon.minosoft.protocol.protocol.InByteBuffer
import de.bixilon.minosoft.util.KUtil
import de.bixilon.minosoft.util.enum.ValuesEnum
import de.bixilon.minosoft.util.logging.Log
class PacketTitle : ClientboundPacket() {
lateinit var action: TitleActions
// fields depend on action
var text: ChatComponent? = null
var subText: ChatComponent? = null
var fadeInTime = 0
var stayTime = 0
var fadeOutTime = 0
override fun read(buffer: InByteBuffer): Boolean {
action = buffer.connection.mapping.titleActionsRegistry.get(buffer.readVarInt())!!
when (action) {
TitleActions.SET_TITLE -> this.text = buffer.readChatComponent()
TitleActions.SET_SUBTITLE -> this.subText = buffer.readChatComponent()
TitleActions.SET_TIMES_AND_DISPLAY -> {
this.fadeInTime = buffer.readInt()
this.stayTime = buffer.readInt()
this.fadeOutTime = buffer.readInt()
}
else -> {
}
}
return true
}
override fun handle(connection: Connection) {
if (connection.fireEvent(TitleChangeEvent(connection, this))) {
return
}
}
override fun log() {
when (this.action) {
TitleActions.SET_TITLE -> Log.protocol(String.format("[IN] Received title (action=%s, text=%s)", this.action, this.text?.ansiColoredMessage))
TitleActions.SET_SUBTITLE -> Log.protocol(String.format("[IN] Received title (action=%s, subText=%s)", this.action, this.subText?.ansiColoredMessage))
TitleActions.SET_TIMES_AND_DISPLAY -> Log.protocol(String.format("[IN] Received title (action=%s, fadeInTime=%d, stayTime=%d, fadeOutTime=%d)", this.action, this.fadeInTime, this.stayTime, this.fadeOutTime))
TitleActions.HIDE, TitleActions.RESET -> Log.protocol(String.format("[IN] Received title (action=%s)", this.action))
}
}
enum class TitleActions {
SET_TITLE,
SET_SUBTITLE,
SET_ACTION_BAR,
SET_TIMES_AND_DISPLAY,
HIDE,
RESET,
;
companion object : ValuesEnum<TitleActions> {
override val VALUES = values()
override val NAME_MAP = KUtil.getEnumValues(VALUES)
}
}
}

View File

@ -197,5 +197,22 @@
"OPT_VAR_INT",
"POSE"
]
},
"minecraft:title_actions": {
"18": [
"SET_TITLE",
"SET_SUBTITLE",
"SET_TIMES_AND_DISPLAY",
"HIDE",
"RESET"
],
"302": [
"SET_TITLE",
"SET_SUBTITLE",
"SET_ACTION_BAR",
"SET_TIMES_AND_DISPLAY",
"HIDE",
"RESET"
]
}
}