forked from SteamWar/SteamWar
Add RInteraction
This commit is contained in:
@@ -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 <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<Object> updatePacketSink = o -> server.updateEntity(this, o);
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
@Getter(AccessLevel.PRIVATE)
|
||||||
|
protected BiConsumer<Player, REntityServer.EntityAction> 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<Object> packetSink) {
|
||||||
|
super.postSpawn(packetSink);
|
||||||
|
sendPacket(packetSink,
|
||||||
|
this::getInteractionWidthData,
|
||||||
|
this::getInteractionHeightData,
|
||||||
|
this::getResponsiveData
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
protected final void sendPacket(Consumer<Object> packetSink, BiConsumer<Boolean, BiConsumer<Object, Object>>... dataSinkSinks) {
|
||||||
|
List<Object> keyValueData = new ArrayList<>();
|
||||||
|
boolean ignoreDefault = packetSink == updatePacketSink;
|
||||||
|
for (BiConsumer<Boolean, BiConsumer<Object, Object>> 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<Object, Object> 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<Object, Object> 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<Object, Object> dataSink) {
|
||||||
|
if (ignoreDefault || !responsive) {
|
||||||
|
dataSink.accept(responsiveWatcher, responsive);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user