forked from SteamWar/SteamWar
Add RInteraction
This commit is contained in:
@@ -19,8 +19,8 @@
|
|||||||
|
|
||||||
package de.steamwar.entity;
|
package de.steamwar.entity;
|
||||||
|
|
||||||
import de.steamwar.Reflection;
|
|
||||||
import com.comphenix.tinyprotocol.TinyProtocol;
|
import com.comphenix.tinyprotocol.TinyProtocol;
|
||||||
|
import de.steamwar.Reflection;
|
||||||
import de.steamwar.core.Core;
|
import de.steamwar.core.Core;
|
||||||
import de.steamwar.core.FlatteningWrapper;
|
import de.steamwar.core.FlatteningWrapper;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@@ -52,8 +52,9 @@ public class REntityServer implements Listener {
|
|||||||
private static final Class<?> useEntityEnumAction = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundInteractPacket$Action");
|
private static final Class<?> useEntityEnumAction = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundInteractPacket$Action");
|
||||||
private static final Reflection.Field<?> useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0);
|
private static final Reflection.Field<?> useEntityAction = Reflection.getField(useEntity, useEntityEnumAction, 0);
|
||||||
private static final Function<Object, Integer> getEntityAction;
|
private static final Function<Object, Integer> getEntityAction;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
if(Core.getVersion() > 15) {
|
if (Core.getVersion() > 15) {
|
||||||
Class<?> useEntityEnumActionType = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundInteractPacket$ActionType");
|
Class<?> useEntityEnumActionType = Reflection.getClass("net.minecraft.network.protocol.game.ServerboundInteractPacket$ActionType");
|
||||||
Reflection.Method useEntityGetAction = Reflection.getTypedMethod(useEntityEnumAction, null, useEntityEnumActionType);
|
Reflection.Method useEntityGetAction = Reflection.getTypedMethod(useEntityEnumAction, null, useEntityEnumActionType);
|
||||||
getEntityAction = value -> ((Enum<?>) useEntityGetAction.invoke(value)).ordinal();
|
getEntityAction = value -> ((Enum<?>) useEntityGetAction.invoke(value)).ordinal();
|
||||||
@@ -71,6 +72,9 @@ public class REntityServer implements Listener {
|
|||||||
private EntityActionListener callback = null;
|
private EntityActionListener callback = null;
|
||||||
private final Set<Player> playersThatClicked = Collections.synchronizedSet(new HashSet<>());
|
private final Set<Player> playersThatClicked = Collections.synchronizedSet(new HashSet<>());
|
||||||
|
|
||||||
|
private static final String DE_STEAMWAR_ENTITY_RINTERACTION = "de.steamwar.entity.RInteraction";
|
||||||
|
private Reflection.Field<BiConsumer> interactionField = Reflection.getField(DE_STEAMWAR_ENTITY_RINTERACTION, "interaction", BiConsumer.class);
|
||||||
|
|
||||||
private final BiFunction<Player, Object, Object> filter = (player, packet) -> {
|
private final BiFunction<Player, Object, Object> filter = (player, packet) -> {
|
||||||
REntity entity = entityMap.get(useEntityTarget.get(packet));
|
REntity entity = entityMap.get(useEntityTarget.get(packet));
|
||||||
if (entity == null)
|
if (entity == null)
|
||||||
@@ -83,6 +87,9 @@ public class REntityServer implements Listener {
|
|||||||
EntityAction action = getEntityAction.apply(useEntityAction.get(packet)) == 1 ? EntityAction.ATTACK : EntityAction.INTERACT;
|
EntityAction action = getEntityAction.apply(useEntityAction.get(packet)) == 1 ? EntityAction.ATTACK : EntityAction.INTERACT;
|
||||||
Bukkit.getScheduler().runTask(Core.getInstance(), () -> {
|
Bukkit.getScheduler().runTask(Core.getInstance(), () -> {
|
||||||
playersThatClicked.remove(player);
|
playersThatClicked.remove(player);
|
||||||
|
if (entity.getClass().getTypeName().equals(DE_STEAMWAR_ENTITY_RINTERACTION)) {
|
||||||
|
interactionField.get(entity).accept(player, action);
|
||||||
|
}
|
||||||
callback.onAction(player, entity, action);
|
callback.onAction(player, entity, action);
|
||||||
});
|
});
|
||||||
return null;
|
return null;
|
||||||
@@ -96,7 +103,7 @@ public class REntityServer implements Listener {
|
|||||||
boolean uninitialized = this.callback == null;
|
boolean uninitialized = this.callback == null;
|
||||||
this.callback = callback;
|
this.callback = callback;
|
||||||
|
|
||||||
if(uninitialized)
|
if (uninitialized)
|
||||||
TinyProtocol.instance.addFilter(useEntity, filter);
|
TinyProtocol.instance.addFilter(useEntity, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,13 +128,16 @@ public class REntityServer implements Listener {
|
|||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
TinyProtocol.instance.removeFilter(useEntity, filter);
|
TinyProtocol.instance.removeFilter(useEntity, filter);
|
||||||
for(Player player : lastLocation.keySet().toArray(new Player[0])) {
|
for (Player player : lastLocation.keySet().toArray(new Player[0])) {
|
||||||
removePlayer(player);
|
removePlayer(player);
|
||||||
}
|
}
|
||||||
HandlerList.unregisterAll(this);
|
HandlerList.unregisterAll(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addEntity(REntity entity) {
|
void addEntity(REntity entity) {
|
||||||
|
if (callback == null && entity.getClass().getTypeName().equals(DE_STEAMWAR_ENTITY_RINTERACTION)) {
|
||||||
|
setCallback((player, entity1, action) -> {});
|
||||||
|
}
|
||||||
entityMap.put(entity.entityId, entity);
|
entityMap.put(entity.entityId, entity);
|
||||||
addEntityToChunk(entity);
|
addEntityToChunk(entity);
|
||||||
entity.list(packet -> updateEntity(entity, packet));
|
entity.list(packet -> updateEntity(entity, packet));
|
||||||
@@ -137,10 +147,10 @@ public class REntityServer implements Listener {
|
|||||||
void preEntityMove(REntity entity, double toX, double toZ) {
|
void preEntityMove(REntity entity, double toX, double toZ) {
|
||||||
long fromId = entityToId(entity);
|
long fromId = entityToId(entity);
|
||||||
long toId = posToId(toX, toZ);
|
long toId = posToId(toX, toZ);
|
||||||
if(fromId == toId)
|
if (fromId == toId)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(!entity.isHidden())
|
if (!entity.isHidden())
|
||||||
onMissing(players.get(fromId), players.get(toId), entity::despawn);
|
onMissing(players.get(fromId), players.get(toId), entity::despawn);
|
||||||
onMissing(players.get(fromId), players.get(toId), entity::delist);
|
onMissing(players.get(fromId), players.get(toId), entity::delist);
|
||||||
removeEntityFromChunk(entity);
|
removeEntityFromChunk(entity);
|
||||||
@@ -149,20 +159,20 @@ public class REntityServer implements Listener {
|
|||||||
void postEntityMove(REntity entity, double fromX, double fromZ) {
|
void postEntityMove(REntity entity, double fromX, double fromZ) {
|
||||||
long fromId = posToId(fromX, fromZ);
|
long fromId = posToId(fromX, fromZ);
|
||||||
long toId = entityToId(entity);
|
long toId = entityToId(entity);
|
||||||
if(fromId == toId)
|
if (fromId == toId)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
addEntityToChunk(entity);
|
addEntityToChunk(entity);
|
||||||
onMissing(players.get(toId), players.get(fromId), entity::list);
|
onMissing(players.get(toId), players.get(fromId), entity::list);
|
||||||
if(!entity.isHidden())
|
if (!entity.isHidden())
|
||||||
onMissing(players.get(toId), players.get(fromId), entity::spawn);
|
onMissing(players.get(toId), players.get(fromId), entity::spawn);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateEntity(REntity entity, Object packet) {
|
void updateEntity(REntity entity, Object packet) {
|
||||||
if(entity.isHidden())
|
if (entity.isHidden())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for(Player player : players.getOrDefault(entityToId(entity), emptyPlayers)) {
|
for (Player player : players.getOrDefault(entityToId(entity), emptyPlayers)) {
|
||||||
TinyProtocol.instance.sendPacket(player, packet);
|
TinyProtocol.instance.sendPacket(player, packet);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -191,7 +201,7 @@ public class REntityServer implements Listener {
|
|||||||
HashSet<REntity> entitiesInChunk = entities.get(id);
|
HashSet<REntity> entitiesInChunk = entities.get(id);
|
||||||
if (entitiesInChunk == null) return;
|
if (entitiesInChunk == null) return;
|
||||||
entitiesInChunk.remove(entity);
|
entitiesInChunk.remove(entity);
|
||||||
if(entitiesInChunk.isEmpty())
|
if (entitiesInChunk.isEmpty())
|
||||||
entities.remove(id);
|
entities.remove(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,28 +210,28 @@ public class REntityServer implements Listener {
|
|||||||
Player player = e.getPlayer();
|
Player player = e.getPlayer();
|
||||||
Location from = lastLocation.get(player);
|
Location from = lastLocation.get(player);
|
||||||
Location to = e.getTo();
|
Location to = e.getTo();
|
||||||
if(from == null || to == null)
|
if (from == null || to == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int fromX = posToChunk(from.getX());
|
int fromX = posToChunk(from.getX());
|
||||||
int fromZ = posToChunk(from.getZ());
|
int fromZ = posToChunk(from.getZ());
|
||||||
int toX = posToChunk(to.getX());
|
int toX = posToChunk(to.getX());
|
||||||
int toZ = posToChunk(to.getZ());
|
int toZ = posToChunk(to.getZ());
|
||||||
if(fromX == toX && fromZ == toZ)
|
if (fromX == toX && fromZ == toZ)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
lastLocation.put(player, to);
|
lastLocation.put(player, to);
|
||||||
|
|
||||||
int toViewDistance = viewRadius(player);
|
int toViewDistance = viewRadius(player);
|
||||||
forChunkInView(player, from, (x, z) -> {
|
forChunkInView(player, from, (x, z) -> {
|
||||||
if(Math.abs(x - toX) > toViewDistance || Math.abs(z - toZ) > toViewDistance) {
|
if (Math.abs(x - toX) > toViewDistance || Math.abs(z - toZ) > toViewDistance) {
|
||||||
removePlayerFromChunk(player, x, z);
|
removePlayerFromChunk(player, x, z);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
int fromViewDistance = this.viewDistance.put(player, toViewDistance);
|
int fromViewDistance = this.viewDistance.put(player, toViewDistance);
|
||||||
forChunkInView(player, to, (x, z) -> {
|
forChunkInView(player, to, (x, z) -> {
|
||||||
if(Math.abs(x - fromX) > fromViewDistance || Math.abs(z - fromZ) > fromViewDistance) {
|
if (Math.abs(x - fromX) > fromViewDistance || Math.abs(z - fromZ) > fromViewDistance) {
|
||||||
addPlayerToChunk(player, x, z);
|
addPlayerToChunk(player, x, z);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -231,25 +241,25 @@ public class REntityServer implements Listener {
|
|||||||
public void onQuit(PlayerQuitEvent e) {
|
public void onQuit(PlayerQuitEvent e) {
|
||||||
Player player = e.getPlayer();
|
Player player = e.getPlayer();
|
||||||
Location location = lastLocation.remove(player);
|
Location location = lastLocation.remove(player);
|
||||||
if(location == null)
|
if (location == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
forChunkInView(player, location, (x, z) -> {
|
forChunkInView(player, location, (x, z) -> {
|
||||||
long id = chunkToId(x, z);
|
long id = chunkToId(x, z);
|
||||||
Set<Player> playersInChunk = players.get(id);
|
Set<Player> playersInChunk = players.get(id);
|
||||||
playersInChunk.remove(player);
|
playersInChunk.remove(player);
|
||||||
if(playersInChunk.isEmpty())
|
if (playersInChunk.isEmpty())
|
||||||
players.remove(id);
|
players.remove(id);
|
||||||
});
|
});
|
||||||
viewDistance.remove(player);
|
viewDistance.remove(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onMissing(Set<Player> of, Set<Player> in, Consumer<Consumer<Object>> packetProvider) {
|
private void onMissing(Set<Player> of, Set<Player> in, Consumer<Consumer<Object>> packetProvider) {
|
||||||
if(of == null)
|
if (of == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
for(Player player : of) {
|
for (Player player : of) {
|
||||||
if(in == null || !in.contains(player)) {
|
if (in == null || !in.contains(player)) {
|
||||||
packetProvider.accept(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
packetProvider.accept(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -260,8 +270,8 @@ public class REntityServer implements Listener {
|
|||||||
int chunkZ = posToChunk(location.getZ());
|
int chunkZ = posToChunk(location.getZ());
|
||||||
int viewDistance = this.viewDistance.get(player);
|
int viewDistance = this.viewDistance.get(player);
|
||||||
|
|
||||||
for(int x = chunkX - viewDistance; x <= chunkX + viewDistance; x++) {
|
for (int x = chunkX - viewDistance; x <= chunkX + viewDistance; x++) {
|
||||||
for(int z = chunkZ - viewDistance; z <= chunkZ + viewDistance; z++) {
|
for (int z = chunkZ - viewDistance; z <= chunkZ + viewDistance; z++) {
|
||||||
func.accept(x, z);
|
func.accept(x, z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -270,9 +280,9 @@ public class REntityServer implements Listener {
|
|||||||
private void addPlayerToChunk(Player player, int x, int z) {
|
private void addPlayerToChunk(Player player, int x, int z) {
|
||||||
long id = chunkToId(x, z);
|
long id = chunkToId(x, z);
|
||||||
players.computeIfAbsent(id, i -> new HashSet<>()).add(player);
|
players.computeIfAbsent(id, i -> new HashSet<>()).add(player);
|
||||||
for(REntity entity : entities.getOrDefault(id, emptyEntities)) {
|
for (REntity entity : entities.getOrDefault(id, emptyEntities)) {
|
||||||
entity.list(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
entity.list(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
||||||
if(!entity.isHidden())
|
if (!entity.isHidden())
|
||||||
entity.spawn(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
entity.spawn(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -282,11 +292,11 @@ public class REntityServer implements Listener {
|
|||||||
|
|
||||||
Set<Player> playersInChunk = players.get(id);
|
Set<Player> playersInChunk = players.get(id);
|
||||||
playersInChunk.remove(player);
|
playersInChunk.remove(player);
|
||||||
if(playersInChunk.isEmpty())
|
if (playersInChunk.isEmpty())
|
||||||
players.remove(id);
|
players.remove(id);
|
||||||
|
|
||||||
for(REntity entity : entities.getOrDefault(id, emptyEntities)) {
|
for (REntity entity : entities.getOrDefault(id, emptyEntities)) {
|
||||||
if(!entity.isHidden())
|
if (!entity.isHidden())
|
||||||
entity.despawn(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
entity.despawn(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
||||||
entity.delist(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
entity.delist(packet -> TinyProtocol.instance.sendPacket(player, packet));
|
||||||
}
|
}
|
||||||
@@ -299,7 +309,7 @@ public class REntityServer implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int posToChunk(double coord) {
|
private int posToChunk(double coord) {
|
||||||
return (int)(coord / 16) - (coord < 0 ? 1 : 0);
|
return (int) (coord / 16) - (coord < 0 ? 1 : 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int viewRadius(Player player) {
|
private int viewRadius(Player player) {
|
||||||
|
|||||||
@@ -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