From 83d868eba2bcc5c6e6ad7cc3d1c7eef2caaa530b Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Mon, 16 Mar 2026 22:16:10 +0100 Subject: [PATCH] Add RInteraction --- .../src/de/steamwar/entity/RInteraction.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 SpigotCore/SpigotCore_Main/src/de/steamwar/entity/RInteraction.java diff --git a/SpigotCore/SpigotCore_Main/src/de/steamwar/entity/RInteraction.java b/SpigotCore/SpigotCore_Main/src/de/steamwar/entity/RInteraction.java new file mode 100644 index 00000000..530aca91 --- /dev/null +++ b/SpigotCore/SpigotCore_Main/src/de/steamwar/entity/RInteraction.java @@ -0,0 +1,119 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2026 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero 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 Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.entity; + +import de.steamwar.core.BountifulWrapper; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.Setter; +import org.bukkit.Location; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; +import java.util.function.Consumer; + +/** + * !! This class cannot be used in Versions lower than or equal to 1.19.4 !! + */ +@Getter +public class RInteraction extends REntity { + + protected final Consumer updatePacketSink = o -> server.updateEntity(this, o); + + @Setter + @Getter(AccessLevel.PRIVATE) + protected BiConsumer interaction; + + private float interactionWidth = 1.0f; + private float interactionHeight = 1.0f; + private boolean responsive = false; + + public RInteraction(REntityServer server, Location location) { + super(server, EntityType.INTERACTION, location); + server.addEntity(this); + } + + @Override + protected void postSpawn(Consumer packetSink) { + super.postSpawn(packetSink); + sendPacket(packetSink, + this::getInteractionWidthData, + this::getInteractionHeightData, + this::getResponsiveData + ); + } + + @SafeVarargs + protected final void sendPacket(Consumer packetSink, BiConsumer>... dataSinkSinks) { + List keyValueData = new ArrayList<>(); + boolean ignoreDefault = packetSink == updatePacketSink; + for (BiConsumer> dataSinkSink : dataSinkSinks) { + dataSinkSink.accept(ignoreDefault, (dataWatcher, value) -> { + keyValueData.add(dataWatcher); + keyValueData.add(value); + }); + } + if (!keyValueData.isEmpty()) { + packetSink.accept(getDataWatcherPacket(keyValueData.toArray())); + } + } + + public void setInteractionWidth(float interactionWidth) { + this.interactionWidth = interactionWidth; + sendPacket(updatePacketSink, this::getInteractionWidthData); + } + + private static final Object interactionWidthWatcher = BountifulWrapper.impl.getDataWatcherObject(8, Float.class); + + private void getInteractionWidthData(boolean ignoreDefault, BiConsumer dataSink) { + if (ignoreDefault || interactionWidth != 1.0) { + dataSink.accept(interactionWidthWatcher, interactionWidth); + } + } + + public void setInteractionHeight(float interactionHeight) { + this.interactionHeight = interactionHeight; + sendPacket(updatePacketSink, this::getInteractionHeightData); + } + + private static final Object interactionHeightWatcher = BountifulWrapper.impl.getDataWatcherObject(9, Float.class); + + private void getInteractionHeightData(boolean ignoreDefault, BiConsumer dataSink) { + if (ignoreDefault || interactionHeight != 1.0) { + dataSink.accept(interactionHeightWatcher, interactionHeight); + } + } + + public void setResponsive(boolean responsive) { + this.responsive = responsive; + sendPacket(updatePacketSink, this::getResponsiveData); + } + + private static final Object responsiveWatcher = BountifulWrapper.impl.getDataWatcherObject(10, Boolean.class); + + private void getResponsiveData(boolean ignoreDefault, BiConsumer dataSink) { + if (ignoreDefault || !responsive) { + dataSink.accept(responsiveWatcher, responsive); + } + } +}