Mirror von
https://github.com/Moulberry/AxiomPaperPlugin.git
synchronisiert 2024-11-17 13:50:05 +01:00
Remove serverside hotbars & views
Dieser Commit ist enthalten in:
Ursprung
26d750b258
Commit
6f77f8211b
@ -13,10 +13,5 @@ public class AxiomConstants {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static final int API_VERSION = 8;
|
public static final int API_VERSION = 8;
|
||||||
public static final NamespacedKey ACTIVE_HOTBAR_INDEX = new NamespacedKey("axiom", "active_hotbar_index");
|
|
||||||
public static final NamespacedKey HOTBAR_DATA = new NamespacedKey("axiom", "hotbar_data");
|
|
||||||
|
|
||||||
public static final NamespacedKey ACTIVE_VIEW = new NamespacedKey("axiom", "active_view");
|
|
||||||
public static final NamespacedKey VIEWS = new NamespacedKey("axiom", "views");
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -135,10 +135,7 @@ public class AxiomPaper extends JavaPlugin implements Listener {
|
|||||||
registerPacketHandler("set_world_time", new SetTimePacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
registerPacketHandler("set_world_time", new SetTimePacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
||||||
registerPacketHandler("set_world_property", new SetWorldPropertyListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
registerPacketHandler("set_world_property", new SetWorldPropertyListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
||||||
registerPacketHandler("set_block", new SetBlockPacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers); // set-single-block
|
registerPacketHandler("set_block", new SetBlockPacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers); // set-single-block
|
||||||
registerPacketHandler("set_hotbar_slot", new SetHotbarSlotPacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
|
||||||
registerPacketHandler("switch_active_hotbar", new SwitchActiveHotbarPacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
|
||||||
registerPacketHandler("teleport", new TeleportPacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
registerPacketHandler("teleport", new TeleportPacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
||||||
registerPacketHandler("set_editor_views", new SetEditorViewsPacketListener(this), msg, LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
|
||||||
registerPacketHandler("request_chunk_data", new RequestChunkDataPacketListener(this, !configuration.getBoolean("packet-handlers.request-chunk-data")), msg,
|
registerPacketHandler("request_chunk_data", new RequestChunkDataPacketListener(this, !configuration.getBoolean("packet-handlers.request-chunk-data")), msg,
|
||||||
this.configuration.getBoolean("allow-large-chunk-data-request") ? LargePayloadBehaviour.FORCE_LARGE : LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
this.configuration.getBoolean("allow-large-chunk-data-request") ? LargePayloadBehaviour.FORCE_LARGE : LargePayloadBehaviour.DEFAULT, largePayloadHandlers);
|
||||||
registerPacketHandler("request_entity_data", new RequestEntityDataPacketListener(this, !configuration.getBoolean("packet-handlers.request-entity-data")), msg,
|
registerPacketHandler("request_entity_data", new RequestEntityDataPacketListener(this, !configuration.getBoolean("packet-handlers.request-entity-data")), msg,
|
||||||
|
@ -1,130 +0,0 @@
|
|||||||
package com.moulberry.axiom;
|
|
||||||
|
|
||||||
import com.moulberry.axiom.persistence.UUIDDataType;
|
|
||||||
import net.minecraft.core.registries.Registries;
|
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
|
||||||
import net.minecraft.resources.ResourceKey;
|
|
||||||
import net.minecraft.resources.ResourceLocation;
|
|
||||||
import net.minecraft.world.level.Level;
|
|
||||||
import net.minecraft.world.phys.Vec3;
|
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.persistence.PersistentDataContainer;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public final class View {
|
|
||||||
|
|
||||||
public String name;
|
|
||||||
public final UUID uuid;
|
|
||||||
public boolean pinLevel = false;
|
|
||||||
public boolean pinLocation = false;
|
|
||||||
private ResourceKey<Level> level = null;
|
|
||||||
private Vec3 position = null;
|
|
||||||
private float yaw;
|
|
||||||
private float pitch;
|
|
||||||
|
|
||||||
public View(String name, UUID uuid) {
|
|
||||||
this.name = name;
|
|
||||||
this.uuid = uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void write(FriendlyByteBuf byteBuf) {
|
|
||||||
byteBuf.writeUtf(this.name, 64);
|
|
||||||
byteBuf.writeUUID(this.uuid);
|
|
||||||
|
|
||||||
byteBuf.writeBoolean(this.pinLevel);
|
|
||||||
if (this.pinLevel && this.level != null) {
|
|
||||||
byteBuf.writeBoolean(true);
|
|
||||||
byteBuf.writeResourceKey(this.level);
|
|
||||||
} else {
|
|
||||||
byteBuf.writeBoolean(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
byteBuf.writeBoolean(this.pinLocation);
|
|
||||||
if (this.position != null) {
|
|
||||||
byteBuf.writeBoolean(true);
|
|
||||||
byteBuf.writeDouble(this.position.x);
|
|
||||||
byteBuf.writeDouble(this.position.y);
|
|
||||||
byteBuf.writeDouble(this.position.z);
|
|
||||||
byteBuf.writeFloat(this.yaw);
|
|
||||||
byteBuf.writeFloat(this.pitch);
|
|
||||||
} else {
|
|
||||||
byteBuf.writeBoolean(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static View read(FriendlyByteBuf byteBuf) {
|
|
||||||
View view = new View(byteBuf.readUtf(64), byteBuf.readUUID());
|
|
||||||
|
|
||||||
view.pinLevel = byteBuf.readBoolean();
|
|
||||||
if (byteBuf.readBoolean()) {
|
|
||||||
view.level = byteBuf.readResourceKey(Registries.DIMENSION);
|
|
||||||
}
|
|
||||||
|
|
||||||
view.pinLocation = byteBuf.readBoolean();
|
|
||||||
if (byteBuf.readBoolean()) {
|
|
||||||
view.position = new Vec3(byteBuf.readDouble(), byteBuf.readDouble(), byteBuf.readDouble());
|
|
||||||
view.yaw = byteBuf.readFloat();
|
|
||||||
view.pitch = byteBuf.readFloat();
|
|
||||||
}
|
|
||||||
|
|
||||||
return view;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static final NamespacedKey NAME_KEY = new NamespacedKey("axiom", "view_name");
|
|
||||||
private static final NamespacedKey UUID_KEY = new NamespacedKey("axiom", "view_uuid");
|
|
||||||
private static final NamespacedKey PIN_LEVEL_KEY = new NamespacedKey("axiom", "view_pin_level");
|
|
||||||
private static final NamespacedKey LEVEL_KEY = new NamespacedKey("axiom", "view_level");
|
|
||||||
private static final NamespacedKey PIN_LOCATION_KEY = new NamespacedKey("axiom", "view_pin_location");
|
|
||||||
private static final NamespacedKey X_KEY = new NamespacedKey("axiom", "view_x");
|
|
||||||
private static final NamespacedKey Y_KEY = new NamespacedKey("axiom", "view_y");
|
|
||||||
private static final NamespacedKey Z_KEY = new NamespacedKey("axiom", "view_z");
|
|
||||||
private static final NamespacedKey YAW_KEY = new NamespacedKey("axiom", "view_yaw");
|
|
||||||
private static final NamespacedKey PITCH_KEY = new NamespacedKey("axiom", "view_pitch");
|
|
||||||
|
|
||||||
public void save(PersistentDataContainer container) {
|
|
||||||
container.set(NAME_KEY, PersistentDataType.STRING, this.name);
|
|
||||||
container.set(UUID_KEY, UUIDDataType.INSTANCE, this.uuid);
|
|
||||||
|
|
||||||
container.set(PIN_LEVEL_KEY, PersistentDataType.BOOLEAN, this.pinLevel);
|
|
||||||
if (this.pinLevel && this.level != null) {
|
|
||||||
container.set(LEVEL_KEY, PersistentDataType.STRING, this.level.location().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
container.set(PIN_LOCATION_KEY, PersistentDataType.BOOLEAN, this.pinLocation);
|
|
||||||
if (this.position != null) {
|
|
||||||
container.set(X_KEY, PersistentDataType.DOUBLE, this.position.x);
|
|
||||||
container.set(Y_KEY, PersistentDataType.DOUBLE, this.position.y);
|
|
||||||
container.set(Z_KEY, PersistentDataType.DOUBLE, this.position.z);
|
|
||||||
container.set(YAW_KEY, PersistentDataType.FLOAT, this.yaw);
|
|
||||||
container.set(PITCH_KEY, PersistentDataType.FLOAT, this.pitch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static View load(PersistentDataContainer tag) {
|
|
||||||
String name = tag.get(NAME_KEY, PersistentDataType.STRING);
|
|
||||||
UUID uuid = tag.get(UUID_KEY, UUIDDataType.INSTANCE);
|
|
||||||
|
|
||||||
View view = new View(name, uuid);
|
|
||||||
|
|
||||||
view.pinLevel = tag.getOrDefault(PIN_LEVEL_KEY, PersistentDataType.BOOLEAN, false);
|
|
||||||
if (tag.has(LEVEL_KEY)) {
|
|
||||||
String level = tag.get(LEVEL_KEY, PersistentDataType.STRING);
|
|
||||||
view.level = ResourceKey.create(Registries.DIMENSION, VersionHelper.createResourceLocation(level));
|
|
||||||
}
|
|
||||||
|
|
||||||
view.pinLocation = tag.getOrDefault(PIN_LOCATION_KEY, PersistentDataType.BOOLEAN, false);
|
|
||||||
if (tag.has(X_KEY) && tag.has(Y_KEY) && tag.has(Z_KEY)) {
|
|
||||||
double x = tag.getOrDefault(X_KEY, PersistentDataType.DOUBLE, 0.0);
|
|
||||||
double y = tag.getOrDefault(Y_KEY, PersistentDataType.DOUBLE, 0.0);
|
|
||||||
double z = tag.getOrDefault(Z_KEY, PersistentDataType.DOUBLE, 0.0);
|
|
||||||
view.position = new Vec3(x, y, z);
|
|
||||||
view.yaw = tag.getOrDefault(YAW_KEY, PersistentDataType.FLOAT, 0.0f);
|
|
||||||
view.pitch = tag.getOrDefault(PITCH_KEY, PersistentDataType.FLOAT, 0.0f);
|
|
||||||
}
|
|
||||||
|
|
||||||
return view;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -5,8 +5,6 @@ import com.moulberry.axiom.*;
|
|||||||
import com.moulberry.axiom.blueprint.ServerBlueprintManager;
|
import com.moulberry.axiom.blueprint.ServerBlueprintManager;
|
||||||
import com.moulberry.axiom.event.AxiomHandshakeEvent;
|
import com.moulberry.axiom.event.AxiomHandshakeEvent;
|
||||||
import com.moulberry.axiom.packet.PacketHandler;
|
import com.moulberry.axiom.packet.PacketHandler;
|
||||||
import com.moulberry.axiom.persistence.ItemStackDataType;
|
|
||||||
import com.moulberry.axiom.persistence.UUIDDataType;
|
|
||||||
import com.moulberry.axiom.viaversion.ViaVersionHelper;
|
import com.moulberry.axiom.viaversion.ViaVersionHelper;
|
||||||
import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry;
|
import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry;
|
||||||
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
import com.viaversion.viaversion.api.protocol.version.ProtocolVersion;
|
||||||
@ -18,18 +16,11 @@ import net.minecraft.core.IdMapper;
|
|||||||
import net.minecraft.network.FriendlyByteBuf;
|
import net.minecraft.network.FriendlyByteBuf;
|
||||||
import net.minecraft.world.level.block.state.BlockState;
|
import net.minecraft.world.level.block.state.BlockState;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.craftbukkit.v1_20_R2.CraftWorld;
|
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||||
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
|
|
||||||
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.persistence.PersistentDataContainer;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class HelloPacketListener implements PacketHandler {
|
public class HelloPacketListener implements PacketHandler {
|
||||||
|
|
||||||
@ -144,47 +135,6 @@ public class HelloPacketListener implements PacketHandler {
|
|||||||
buf.getBytes(0, enableBytes);
|
buf.getBytes(0, enableBytes);
|
||||||
player.sendPluginMessage(this.plugin, "axiom:enable", enableBytes);
|
player.sendPluginMessage(this.plugin, "axiom:enable", enableBytes);
|
||||||
|
|
||||||
// Initialize Hotbars
|
|
||||||
PersistentDataContainer container = player.getPersistentDataContainer();
|
|
||||||
if (!this.plugin.isMismatchedDataVersion(player.getUniqueId())) {
|
|
||||||
int activeHotbarIndex = container.getOrDefault(AxiomConstants.ACTIVE_HOTBAR_INDEX, PersistentDataType.BYTE, (byte) 0);
|
|
||||||
PersistentDataContainer hotbarItems = container.get(AxiomConstants.HOTBAR_DATA, PersistentDataType.TAG_CONTAINER);
|
|
||||||
if (hotbarItems != null) {
|
|
||||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
|
||||||
buf.writeByte((byte) activeHotbarIndex);
|
|
||||||
for (int i=0; i<9*9; i++) {
|
|
||||||
// Ignore selected hotbar
|
|
||||||
if (i / 9 == activeHotbarIndex) {
|
|
||||||
buf.writeItem(net.minecraft.world.item.ItemStack.EMPTY);
|
|
||||||
} else {
|
|
||||||
ItemStack stack = hotbarItems.get(new NamespacedKey("axiom", "slot_"+i), ItemStackDataType.INSTANCE);
|
|
||||||
buf.writeItem(CraftItemStack.asNMSCopy(stack));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] bytes = new byte[buf.writerIndex()];
|
|
||||||
buf.getBytes(0, bytes);
|
|
||||||
player.sendPluginMessage(this.plugin, "axiom:initialize_hotbars", bytes);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize Views
|
|
||||||
UUID activeView = container.get(AxiomConstants.ACTIVE_VIEW, UUIDDataType.INSTANCE);
|
|
||||||
if (activeView != null) {
|
|
||||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
|
||||||
buf.writeUUID(activeView);
|
|
||||||
|
|
||||||
PersistentDataContainer[] views = container.get(AxiomConstants.VIEWS, PersistentDataType.TAG_CONTAINER_ARRAY);
|
|
||||||
buf.writeVarInt(views.length);
|
|
||||||
for (PersistentDataContainer view : views) {
|
|
||||||
View.load(view).write(buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
byte[] bytes = new byte[buf.writerIndex()];
|
|
||||||
buf.getBytes(0, bytes);
|
|
||||||
player.sendPluginMessage(this.plugin, "axiom:set_editor_views", bytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Register world properties
|
// Register world properties
|
||||||
World world = player.getWorld();
|
World world = player.getWorld();
|
||||||
ServerWorldPropertiesRegistry properties = plugin.getOrCreateWorldProperties(world);
|
ServerWorldPropertiesRegistry properties = plugin.getOrCreateWorldProperties(world);
|
||||||
|
@ -1,51 +0,0 @@
|
|||||||
package com.moulberry.axiom.packet.impl;
|
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import com.moulberry.axiom.AxiomConstants;
|
|
||||||
import com.moulberry.axiom.AxiomPaper;
|
|
||||||
import com.moulberry.axiom.View;
|
|
||||||
import com.moulberry.axiom.packet.PacketHandler;
|
|
||||||
import com.moulberry.axiom.persistence.UUIDDataType;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import net.kyori.adventure.text.Component;
|
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.persistence.PersistentDataContainer;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
|
||||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.function.IntFunction;
|
|
||||||
|
|
||||||
public class SetEditorViewsPacketListener implements PacketHandler {
|
|
||||||
|
|
||||||
private final AxiomPaper plugin;
|
|
||||||
public SetEditorViewsPacketListener(AxiomPaper plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Player player, FriendlyByteBuf friendlyByteBuf) {
|
|
||||||
if (!this.plugin.canUseAxiom(player, "axiom.editor.views")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
UUID uuid = friendlyByteBuf.readUUID();
|
|
||||||
IntFunction<List<View>> listFunction = FriendlyByteBuf.limitValue(Lists::newArrayListWithCapacity, 64);
|
|
||||||
List<View> views = friendlyByteBuf.readCollection(listFunction, View::read);
|
|
||||||
|
|
||||||
PersistentDataContainer container = player.getPersistentDataContainer();
|
|
||||||
container.set(AxiomConstants.ACTIVE_VIEW, UUIDDataType.INSTANCE, uuid);
|
|
||||||
|
|
||||||
PersistentDataContainer[] containerArray = new PersistentDataContainer[views.size()];
|
|
||||||
for (int i = 0; i < views.size(); i++) {
|
|
||||||
PersistentDataContainer viewContainer = container.getAdapterContext().newPersistentDataContainer();
|
|
||||||
views.get(i).save(viewContainer);
|
|
||||||
containerArray[i] = viewContainer;
|
|
||||||
}
|
|
||||||
container.set(AxiomConstants.VIEWS, PersistentDataType.TAG_CONTAINER_ARRAY, containerArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
package com.moulberry.axiom.packet.impl;
|
|
||||||
|
|
||||||
import com.moulberry.axiom.AxiomConstants;
|
|
||||||
import com.moulberry.axiom.AxiomPaper;
|
|
||||||
import com.moulberry.axiom.packet.PacketHandler;
|
|
||||||
import com.moulberry.axiom.persistence.ItemStackDataType;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import net.kyori.adventure.text.Component;
|
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.persistence.PersistentDataContainer;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
|
||||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public class SetHotbarSlotPacketListener implements PacketHandler {
|
|
||||||
|
|
||||||
private final AxiomPaper plugin;
|
|
||||||
public SetHotbarSlotPacketListener(AxiomPaper plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Player player, FriendlyByteBuf friendlyByteBuf) {
|
|
||||||
if (!this.plugin.canUseAxiom(player, "axiom.player.hotbar") || this.plugin.isMismatchedDataVersion(player.getUniqueId())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int index = friendlyByteBuf.readByte();
|
|
||||||
if (index < 0 || index >= 9*9) return;
|
|
||||||
net.minecraft.world.item.ItemStack nmsStack = friendlyByteBuf.readItem();
|
|
||||||
|
|
||||||
PersistentDataContainer container = player.getPersistentDataContainer();
|
|
||||||
PersistentDataContainer hotbarItems = container.get(AxiomConstants.HOTBAR_DATA, PersistentDataType.TAG_CONTAINER);
|
|
||||||
if (hotbarItems == null) hotbarItems = container.getAdapterContext().newPersistentDataContainer();
|
|
||||||
hotbarItems.set(new NamespacedKey("axiom", "slot_"+index), ItemStackDataType.INSTANCE, CraftItemStack.asCraftMirror(nmsStack));
|
|
||||||
container.set(AxiomConstants.HOTBAR_DATA, PersistentDataType.TAG_CONTAINER, hotbarItems);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,66 +0,0 @@
|
|||||||
package com.moulberry.axiom.packet.impl;
|
|
||||||
|
|
||||||
import com.moulberry.axiom.AxiomConstants;
|
|
||||||
import com.moulberry.axiom.AxiomPaper;
|
|
||||||
import com.moulberry.axiom.packet.PacketHandler;
|
|
||||||
import com.moulberry.axiom.persistence.ItemStackDataType;
|
|
||||||
import io.netty.buffer.Unpooled;
|
|
||||||
import net.kyori.adventure.text.Component;
|
|
||||||
import net.minecraft.network.FriendlyByteBuf;
|
|
||||||
import org.bukkit.GameMode;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.NamespacedKey;
|
|
||||||
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.persistence.PersistentDataContainer;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
|
||||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public class SwitchActiveHotbarPacketListener implements PacketHandler {
|
|
||||||
|
|
||||||
private final AxiomPaper plugin;
|
|
||||||
public SwitchActiveHotbarPacketListener(AxiomPaper plugin) {
|
|
||||||
this.plugin = plugin;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onReceive(Player player, FriendlyByteBuf friendlyByteBuf) {
|
|
||||||
if (!this.plugin.canUseAxiom(player, "axiom.player.hotbar") || this.plugin.isMismatchedDataVersion(player.getUniqueId())) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
int oldHotbarIndex = friendlyByteBuf.readByte();
|
|
||||||
int activeHotbarIndex = friendlyByteBuf.readByte();
|
|
||||||
|
|
||||||
ItemStack[] hotbarItems = new ItemStack[9];
|
|
||||||
for (int i=0; i<9; i++) {
|
|
||||||
hotbarItems[i] = CraftItemStack.asCraftMirror(friendlyByteBuf.readItem());
|
|
||||||
}
|
|
||||||
|
|
||||||
PersistentDataContainer container = player.getPersistentDataContainer();
|
|
||||||
PersistentDataContainer containerHotbarItems = container.get(AxiomConstants.HOTBAR_DATA, PersistentDataType.TAG_CONTAINER);
|
|
||||||
if (containerHotbarItems == null) containerHotbarItems = container.getAdapterContext().newPersistentDataContainer();
|
|
||||||
|
|
||||||
for (int i=0; i<9; i++) {
|
|
||||||
if (oldHotbarIndex != activeHotbarIndex) {
|
|
||||||
int index = oldHotbarIndex*9 + i;
|
|
||||||
ItemStack stack = player.getInventory().getItem(i);
|
|
||||||
if (stack == null) {
|
|
||||||
stack = new ItemStack(Material.AIR);
|
|
||||||
} else {
|
|
||||||
stack = stack.clone();
|
|
||||||
}
|
|
||||||
containerHotbarItems.set(new NamespacedKey("axiom", "slot_"+index), ItemStackDataType.INSTANCE, stack);
|
|
||||||
}
|
|
||||||
int index = activeHotbarIndex*9 + i;
|
|
||||||
containerHotbarItems.set(new NamespacedKey("axiom", "slot_"+index), ItemStackDataType.INSTANCE, hotbarItems[i].clone());
|
|
||||||
if (player.getGameMode() == GameMode.CREATIVE) player.getInventory().setItem(i, hotbarItems[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
container.set(AxiomConstants.HOTBAR_DATA, PersistentDataType.TAG_CONTAINER, containerHotbarItems);
|
|
||||||
container.set(AxiomConstants.ACTIVE_HOTBAR_INDEX, PersistentDataType.BYTE, (byte) activeHotbarIndex);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,46 +0,0 @@
|
|||||||
package com.moulberry.axiom.persistence;
|
|
||||||
|
|
||||||
import net.minecraft.nbt.CompoundTag;
|
|
||||||
import org.bukkit.craftbukkit.v1_20_R2.inventory.CraftItemStack;
|
|
||||||
import org.bukkit.craftbukkit.v1_20_R2.persistence.CraftPersistentDataContainer;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.persistence.PersistentDataAdapterContext;
|
|
||||||
import org.bukkit.persistence.PersistentDataContainer;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
public class ItemStackDataType implements PersistentDataType<PersistentDataContainer, ItemStack> {
|
|
||||||
public static ItemStackDataType INSTANCE = new ItemStackDataType();
|
|
||||||
private ItemStackDataType() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Class<PersistentDataContainer> getPrimitiveType() {
|
|
||||||
return PersistentDataContainer.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull Class<ItemStack> getComplexType() {
|
|
||||||
return ItemStack.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull PersistentDataContainer toPrimitive(@NotNull ItemStack complex, @NotNull PersistentDataAdapterContext context) {
|
|
||||||
net.minecraft.world.item.ItemStack nmsStack = CraftItemStack.asNMSCopy(complex);
|
|
||||||
if (nmsStack == null) nmsStack = net.minecraft.world.item.ItemStack.EMPTY;
|
|
||||||
CompoundTag tag = new CompoundTag();
|
|
||||||
nmsStack.save(tag);
|
|
||||||
|
|
||||||
PersistentDataContainer container = context.newPersistentDataContainer();
|
|
||||||
((CraftPersistentDataContainer)container).putAll(tag);
|
|
||||||
return container;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NotNull ItemStack fromPrimitive(@NotNull PersistentDataContainer primitive, @NotNull PersistentDataAdapterContext context) {
|
|
||||||
CompoundTag tag = ((CraftPersistentDataContainer)primitive).toTagCompound();
|
|
||||||
net.minecraft.world.item.ItemStack nmsStack = net.minecraft.world.item.ItemStack.of(tag);
|
|
||||||
|
|
||||||
return CraftItemStack.asCraftMirror(nmsStack);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,39 +0,0 @@
|
|||||||
package com.moulberry.axiom.persistence;
|
|
||||||
|
|
||||||
import org.bukkit.persistence.PersistentDataAdapterContext;
|
|
||||||
import org.bukkit.persistence.PersistentDataType;
|
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class UUIDDataType implements PersistentDataType<byte[], UUID> {
|
|
||||||
public static UUIDDataType INSTANCE = new UUIDDataType();
|
|
||||||
private UUIDDataType() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Class<byte[]> getPrimitiveType() {
|
|
||||||
return byte[].class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Class<UUID> getComplexType() {
|
|
||||||
return UUID.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] toPrimitive(UUID complex, PersistentDataAdapterContext context) {
|
|
||||||
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
|
||||||
bb.putLong(complex.getMostSignificantBits());
|
|
||||||
bb.putLong(complex.getLeastSignificantBits());
|
|
||||||
return bb.array();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public UUID fromPrimitive(byte[] primitive, PersistentDataAdapterContext context) {
|
|
||||||
ByteBuffer bb = ByteBuffer.wrap(primitive);
|
|
||||||
long firstLong = bb.getLong();
|
|
||||||
long secondLong = bb.getLong();
|
|
||||||
return new UUID(firstLong, secondLong);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren