geforkt von Mirrors/AxiomPaperPlugin
Commits vergleichen
74 Commits
multiversi
...
master
Autor | SHA1 | Datum | |
---|---|---|---|
12a94a7796 | |||
|
bac3f8e5a3 | ||
|
47505405ca | ||
|
5358afd619 | ||
b3a36ebda4 | |||
|
1319a3d7c2 | ||
|
9ba82ce6a7 | ||
|
a3d3d16e7c | ||
|
77b58a5dcf | ||
|
db9b7dce07 | ||
|
a8fdb871d4 | ||
|
ff46b9330c | ||
|
dbcfd0bb2b | ||
|
b694a0515b | ||
|
8cd2930fa2 | ||
|
8631549a4a | ||
|
8fd71e59ba | ||
|
2acc42b125 | ||
|
d83cc8ca06 | ||
|
8106e24d74 | ||
|
bb57b07323 | ||
|
13df370a42 | ||
|
313dd873d9 | ||
|
b2d67e4f91 | ||
|
c027e56fc4 | ||
|
0c05b3a46f | ||
|
f38226c9fb | ||
|
b6afa50b04 | ||
|
bf43e7c0b9 | ||
|
a8b4ee2a1d | ||
|
d4e950f996 | ||
|
970678f8b1 | ||
|
8a17605969 | ||
|
89e04ffb39 | ||
|
1ce77ea5e0 | ||
|
8c6fd853ab | ||
|
5b61aebd32 | ||
|
54564c188b | ||
|
83b60d3c6f | ||
|
63d04d3656 | ||
|
e5c8acff02 | ||
|
ca7ef8266d | ||
|
9382958959 | ||
|
dcf70936e2 | ||
|
342aa8036c | ||
|
1140204157 | ||
|
93c618a150 | ||
|
eb1a1cd9cd | ||
|
a76aaba3d0 | ||
|
55e85a3d0e | ||
|
5c8e845b62 | ||
|
fa3f904f22 | ||
772f4f82d6 | |||
db7d852705 | |||
|
d7523cba6a | ||
|
6de6ff0d28 | ||
|
ccbbd5fd39 | ||
|
305a125e27 | ||
|
c8f7c55138 | ||
|
7c8f5000c5 | ||
|
3f613db359 | ||
|
21ac713806 | ||
|
e68cbdbde2 | ||
|
f6cbe97890 | ||
|
5001765e4b | ||
|
48e10626f8 | ||
372d3663d0 | |||
|
1cead96565 | ||
|
8b107c3a9a | ||
|
59cf7b49b9 | ||
|
f88d558f88 | ||
|
1e25490d27 | ||
|
95ac82cf73 | ||
|
aff54bcd34 |
@ -3,3 +3,6 @@
|
||||
Serverside component for Axiom
|
||||
|
||||
(todo: better readme)
|
||||
|
||||
## Download
|
||||
https://modrinth.com/plugin/axiom-paper-plugin/
|
||||
|
@ -1,14 +1,14 @@
|
||||
plugins {
|
||||
`java-library`
|
||||
id("io.papermc.paperweight.userdev") version "1.5.5"
|
||||
id("xyz.jpenilla.run-paper") version "2.1.0" // Adds runServer and runMojangMappedServer tasks for testing
|
||||
id("io.papermc.paperweight.userdev") version "1.5.11"
|
||||
id("xyz.jpenilla.run-paper") version "2.2.2" // Adds runServer and runMojangMappedServer tasks for testing
|
||||
|
||||
// Shades and relocates dependencies into our plugin jar. See https://imperceptiblethoughts.com/shadow/introduction/
|
||||
id("com.github.johnrengelman.shadow") version "8.1.1"
|
||||
}
|
||||
|
||||
group = "com.moulberry.axiom"
|
||||
version = "1.2.1"
|
||||
version = "1.5.8"
|
||||
description = "Serverside component for Axiom on Paper"
|
||||
|
||||
java {
|
||||
@ -18,13 +18,15 @@ java {
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven("https://maven.enginehub.org/repo/")
|
||||
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
|
||||
maven("https://jitpack.io")
|
||||
maven("https://repo.papermc.io/repository/maven-public/")
|
||||
maven("https://repo.viaversion.com")
|
||||
maven("https://maven.enginehub.org/repo/")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
paperweight.paperDevBundle("1.20.1-R0.1-SNAPSHOT")
|
||||
implementation("xyz.jpenilla:reflection-remapper:0.1.0-SNAPSHOT")
|
||||
|
||||
// Zstd Compression Library
|
||||
implementation("com.github.luben:zstd-jni:1.5.5-4")
|
||||
@ -36,9 +38,6 @@ dependencies {
|
||||
implementation(platform("com.intellectualsites.bom:bom-newest:1.37"))
|
||||
compileOnly("com.intellectualsites.plotsquared:plotsquared-core")
|
||||
compileOnly("com.intellectualsites.plotsquared:plotsquared-bukkit") { isTransitive = false }
|
||||
|
||||
// ViaVersion support
|
||||
compileOnly("com.viaversion:viaversion-api:4.7.0")
|
||||
}
|
||||
|
||||
tasks {
|
||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,6 +1,6 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
|
||||
networkTimeout=10000
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
|
@ -1,22 +1,22 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import org.bukkit.NamespacedKey;
|
||||
|
||||
public class AxiomConstants {
|
||||
|
||||
public static final long MIN_POSITION_LONG = 0b1000000000000000000000000010000000000000000000000000100000000000L;
|
||||
|
||||
public static final int API_VERSION = 5;
|
||||
|
||||
public static final String PERMISSION = "axiom.*";
|
||||
|
||||
public static final NamespacedKey ACTIVE_HOTBAR_INDEX = axiomKey("active_hotbar_index");
|
||||
public static final NamespacedKey HOTBAR_DATA = axiomKey("hotbar_data");
|
||||
|
||||
public static final NamespacedKey ACTIVE_VIEW = axiomKey("active_view");
|
||||
public static final NamespacedKey VIEWS = axiomKey("views");
|
||||
|
||||
public static NamespacedKey axiomKey(String key) {
|
||||
return new NamespacedKey("axiom", key);
|
||||
public static final long MIN_POSITION_LONG = BlockPos.asLong(-33554432, -2048, -33554432);
|
||||
static {
|
||||
if (MIN_POSITION_LONG != 0b1000000000000000000000000010000000000000000000000000100000000000L) {
|
||||
throw new Error("BlockPos representation changed!");
|
||||
}
|
||||
}
|
||||
|
||||
public static final int API_VERSION = 7;
|
||||
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");
|
||||
|
||||
}
|
||||
|
@ -1,129 +1,349 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import com.moulberry.axiom.integration.PaperFailMoveListener;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import com.moulberry.axiom.buffer.CompressedBlockEntity;
|
||||
import com.moulberry.axiom.event.AxiomCreateWorldPropertiesEvent;
|
||||
import com.moulberry.axiom.event.AxiomModifyWorldEvent;
|
||||
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
|
||||
import com.moulberry.axiom.packet.*;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.papermc.paper.event.player.PlayerFailMoveEvent;
|
||||
import io.papermc.paper.event.world.WorldGameRuleChangeEvent;
|
||||
import io.papermc.paper.network.ChannelInitializeListener;
|
||||
import io.papermc.paper.network.ChannelInitializeListenerHolder;
|
||||
import net.kyori.adventure.key.Key;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.IdMapper;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.ConnectionProtocol;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.server.level.*;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.server.network.ServerPlayerConnection;
|
||||
import net.minecraft.world.level.ChunkPos;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.minecraft.network.protocol.PacketFlow;
|
||||
import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameRule;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.configuration.Configuration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.plugin.messaging.Messenger;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class AxiomPaper extends JavaPlugin implements Listener {
|
||||
|
||||
private static AxiomPaper instance;
|
||||
public static Plugin getPlugin() {
|
||||
return instance;
|
||||
}
|
||||
public static AxiomPaper PLUGIN; // tsk tsk tsk
|
||||
|
||||
private static final Map<String, AxiomPacketListener> listeners = new ConcurrentHashMap<>();
|
||||
public static AxiomPacketListener getListener(String channel) {
|
||||
return listeners.get(channel);
|
||||
}
|
||||
public final Set<UUID> activeAxiomPlayers = Collections.newSetFromMap(new ConcurrentHashMap<>());
|
||||
public final Map<UUID, RateLimiter> playerBlockBufferRateLimiters = new ConcurrentHashMap<>();
|
||||
public final Map<UUID, Restrictions> playerRestrictions = new ConcurrentHashMap<>();
|
||||
public Configuration configuration;
|
||||
|
||||
private static final Class<Player> CraftPlayer = Reflection.getClass("org.bukkit.craftbukkit.entity.CraftPlayer");
|
||||
private static final Reflection.Method<Player, ServerPlayer> playerGetHandle = Reflection.getTypedMethod(CraftPlayer, "getHandle", ServerPlayer.class);
|
||||
public static ServerPlayer convert(Player player) {
|
||||
return playerGetHandle.invoke(player);
|
||||
}
|
||||
|
||||
private static final Class<World> CraftWorld = Reflection.getClass("org.bukkit.craftbukkit.CraftWorld");
|
||||
private static final Reflection.Method<World, ServerLevel> worldGetHandle = Reflection.getTypedMethod(CraftWorld, "getHandle", ServerLevel.class);
|
||||
public static ServerLevel convert(World world) {
|
||||
return worldGetHandle.invoke(world);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<Level, LevelChunk> getChunk = Reflection.getTypedMethod(Level.class, LevelChunk.class, int.class, int.class);
|
||||
public static LevelChunk getChunk(Level level, int cx, int cz) {
|
||||
return getChunk.invoke(level, cx, cz);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<BlockPos, BlockPos> of = Reflection.getTypedMethod(BlockPos.class, BlockPos.class, long.class);
|
||||
public static BlockPos convert(long packed) {
|
||||
return of.invoke(null, packed);
|
||||
}
|
||||
|
||||
private static final Reflection.Field<ServerPlayer, ServerGamePacketListenerImpl> serverPlayerConnection = Reflection.getField(ServerPlayer.class, ServerGamePacketListenerImpl.class);
|
||||
public static ServerGamePacketListenerImpl getConnection(ServerPlayer player) {
|
||||
return serverPlayerConnection.get(player);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<ServerLevel, ServerChunkCache> getChunkSource = Reflection.getTypedMethod(ServerLevel.class, ServerChunkCache.class);
|
||||
public static ServerChunkCache getChunkSource(ServerLevel level) {
|
||||
return getChunkSource.invoke(level);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<ServerChunkCache, ThreadedLevelLightEngine> getLightEngine = Reflection.getTypedMethod(ServerChunkCache.class, ThreadedLevelLightEngine.class);
|
||||
public static ThreadedLevelLightEngine getLightEngine(ServerChunkCache chunkSource) {
|
||||
return getLightEngine.invoke(chunkSource);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<ServerPlayerConnection, Void> send = Reflection.getMethod(ServerPlayerConnection.class, Packet.class);
|
||||
public static void sendPacket(ServerPlayer player, Packet<?> packet) {
|
||||
send.invoke(AxiomPaper.getConnection(player), packet);
|
||||
}
|
||||
|
||||
private static final Reflection.Field<ServerChunkCache, ChunkMap> chunkMap = Reflection.getField(ServerChunkCache.class, ChunkMap.class);
|
||||
private static final Reflection.Method<ChunkMap, List> getPlayers = Reflection.getTypedMethod(ChunkMap.class, List.class, ChunkPos.class, boolean.class);
|
||||
public static List<ServerPlayer> getPlayersSeeingChunk(ServerLevel level, ChunkPos pos) {
|
||||
ChunkMap map = chunkMap.get(AxiomPaper.getChunkSource(level));
|
||||
return (List<ServerPlayer>)getPlayers.invoke(map, pos, false);
|
||||
}
|
||||
public IdMapper<BlockState> allowedBlockRegistry = null;
|
||||
private boolean logLargeBlockBufferChanges = false;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
instance = this;
|
||||
PLUGIN = this;
|
||||
|
||||
try {
|
||||
PlayerFailMoveEvent.class.getName(); //Try load class
|
||||
Bukkit.getPluginManager().registerEvents(new PaperFailMoveListener(), this);
|
||||
} catch (NoClassDefFoundError e) {
|
||||
getLogger().log(java.util.logging.Level.INFO, "Axiom players may move too quickly according to the server. Use a current Paper version or increase the 'moved-too-quickly-multiplier' in spigot.yml.");
|
||||
this.saveDefaultConfig();
|
||||
configuration = this.getConfig();
|
||||
|
||||
Set<String> validResolutions = Set.of("kick", "warn", "ignore");
|
||||
if (!validResolutions.contains(configuration.getString("incompatible-data-version"))) {
|
||||
this.getLogger().warning("Invalid value for incompatible-data-version, expected 'kick', 'warn' or 'ignore'");
|
||||
}
|
||||
if (!validResolutions.contains(configuration.getString("unsupported-axiom-version"))) {
|
||||
this.getLogger().warning("Invalid value for unsupported-axiom-version, expected 'kick', 'warn' or 'ignore'");
|
||||
}
|
||||
|
||||
for(OutChannel channel : OutChannel.values()) {
|
||||
Bukkit.getMessenger().registerOutgoingPluginChannel(this, channel.getChannel());
|
||||
this.logLargeBlockBufferChanges = this.configuration.getBoolean("log-large-block-buffer-changes");
|
||||
|
||||
List<String> disallowedBlocks = this.configuration.getStringList("disallowed-blocks");
|
||||
this.allowedBlockRegistry = DisallowedBlocks.createAllowedBlockRegistry(disallowedBlocks);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(this, this);
|
||||
// Bukkit.getPluginManager().registerEvents(new WorldPropertiesExample(), this);
|
||||
CompressedBlockEntity.initialize(this);
|
||||
|
||||
Messenger msg = Bukkit.getMessenger();
|
||||
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:enable");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:initialize_hotbars");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:set_editor_views");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:response_chunk_data");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:register_world_properties");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:set_world_property");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:ack_world_properties");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:restrictions");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:marker_data");
|
||||
msg.registerOutgoingPluginChannel(this, "axiom:marker_nbt_response");
|
||||
|
||||
if (configuration.getBoolean("packet-handlers.hello")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:hello", new HelloPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.set-gamemode")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:set_gamemode", new SetGamemodePacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.set-fly-speed")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:set_fly_speed", new SetFlySpeedPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.set-world-time")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:set_world_time", new SetTimePacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.set-world-property")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:set_world_property", new SetWorldPropertyListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.set-single-block")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:set_block", new SetBlockPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.set-hotbar-slot")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:set_hotbar_slot", new SetHotbarSlotPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.switch-active-hotbar")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:switch_active_hotbar", new SwitchActiveHotbarPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.teleport")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:teleport", new TeleportPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.set-editor-views")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:set_editor_views", new SetEditorViewsPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.request-chunk-data")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:request_chunk_data", new RequestChunkDataPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.spawn-entity")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:spawn_entity", new SpawnEntityPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.manipulate-entity")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:manipulate_entity", new ManipulateEntityPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.delete-entity")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:delete_entity", new DeleteEntityPacketListener(this));
|
||||
}
|
||||
if (configuration.getBoolean("packet-handlers.marker-nbt-request")) {
|
||||
msg.registerIncomingPluginChannel(this, "axiom:marker_nbt_request", new MarkerNbtRequestPacketListener(this));
|
||||
}
|
||||
|
||||
registerInChannel("hello", new HelloPacketListener(new AxiomPlayerManager()));
|
||||
registerInChannel("set_gamemode", new SetGamemodePacketListener());
|
||||
registerInChannel("set_fly_speed", new SetFlySpeedPacketListener());
|
||||
registerInChannel("set_block", new SetBlockPacketListener());
|
||||
registerInChannel("set_hotbar_slot", new SetHotbarSlotPacketListener());
|
||||
registerInChannel("switch_active_hotbar", new SwitchActiveHotbarPacketListener());
|
||||
registerInChannel("teleport", new TeleportPacketListener());
|
||||
registerInChannel("set_editor_views", new SetEditorViewsPacketListener());
|
||||
registerInChannel("request_block_entity", new RequestBlockEntityPacketListener());
|
||||
registerInChannel("set_buffer", new SetBlockBufferPacketListener());
|
||||
if (configuration.getBoolean("packet-handlers.set-buffer")) {
|
||||
SetBlockBufferPacketListener setBlockBufferPacketListener = new SetBlockBufferPacketListener(this);
|
||||
|
||||
ChannelInitializeListenerHolder.addListener(Key.key("axiom:handle_big_payload"), new ChannelInitializeListener() {
|
||||
@Override
|
||||
public void afterInitChannel(@NonNull Channel channel) {
|
||||
var packets = ConnectionProtocol.PLAY.getPacketsByIds(PacketFlow.SERVERBOUND);
|
||||
int payloadId = -1;
|
||||
for (Map.Entry<Integer, Class<? extends Packet<?>>> entry : packets.entrySet()) {
|
||||
if (entry.getValue() == ServerboundCustomPayloadPacket.class) {
|
||||
payloadId = entry.getKey();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (payloadId < 0) {
|
||||
throw new RuntimeException("Failed to find ServerboundCustomPayloadPacket id");
|
||||
}
|
||||
|
||||
private void registerInChannel(String channel, AxiomPacketListener handler) {
|
||||
listeners.put(channel, handler);
|
||||
Bukkit.getMessenger().registerIncomingPluginChannel(this, "axiom:" + channel, (ch, player, message) -> {
|
||||
ByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
if (!player.hasPermission(AxiomConstants.PERMISSION)) {
|
||||
handler.onMissingPerm(player, buf);
|
||||
return;
|
||||
Connection connection = (Connection) channel.pipeline().get("packet_handler");
|
||||
channel.pipeline().addBefore("decoder", "axiom-big-payload-handler",
|
||||
new AxiomBigPayloadHandler(payloadId, connection, setBlockBufferPacketListener));
|
||||
}
|
||||
|
||||
handler.onMessage(player, buf);
|
||||
});
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
|
||||
HashSet<UUID> stillActiveAxiomPlayers = new HashSet<>();
|
||||
|
||||
int rateLimit = this.configuration.getInt("block-buffer-rate-limit");
|
||||
if (rateLimit > 0) {
|
||||
// Reduce by 20% just to prevent synchronization/timing issues
|
||||
rateLimit = rateLimit * 8/10;
|
||||
if (rateLimit <= 0) rateLimit = 1;
|
||||
}
|
||||
|
||||
for (Player player : Bukkit.getServer().getOnlinePlayers()) {
|
||||
if (activeAxiomPlayers.contains(player.getUniqueId())) {
|
||||
if (!player.hasPermission("axiom.*")) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeBoolean(false);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.sendPluginMessage(this, "axiom:enable", bytes);
|
||||
} else {
|
||||
UUID uuid = player.getUniqueId();
|
||||
stillActiveAxiomPlayers.add(uuid);
|
||||
|
||||
boolean send = false;
|
||||
|
||||
Restrictions restrictions = playerRestrictions.get(uuid);
|
||||
if (restrictions == null) {
|
||||
restrictions = new Restrictions();
|
||||
playerRestrictions.put(uuid, restrictions);
|
||||
send = true;
|
||||
}
|
||||
|
||||
BlockPos boundsMin = null;
|
||||
BlockPos boundsMax = null;
|
||||
|
||||
if (!player.hasPermission("axiom.allow_copying_other_plots")) {
|
||||
if (PlotSquaredIntegration.isPlotWorld(player.getWorld())) {
|
||||
PlotSquaredIntegration.PlotBounds editable = PlotSquaredIntegration.getCurrentEditablePlot(player);
|
||||
if (editable != null) {
|
||||
restrictions.lastPlotBounds = editable;
|
||||
boundsMin = editable.min();
|
||||
boundsMax = editable.max();
|
||||
} else if (restrictions.lastPlotBounds != null && restrictions.lastPlotBounds.worldName().equals(player.getWorld().getName())) {
|
||||
boundsMin = restrictions.lastPlotBounds.min();
|
||||
boundsMax = restrictions.lastPlotBounds.max();
|
||||
} else {
|
||||
boundsMin = BlockPos.ZERO;
|
||||
boundsMax = BlockPos.ZERO;
|
||||
}
|
||||
}
|
||||
|
||||
int min = Integer.MIN_VALUE;
|
||||
int max = Integer.MAX_VALUE;
|
||||
if (boundsMin != null && boundsMax != null &&
|
||||
boundsMin.getX() == min && boundsMin.getY() == min && boundsMin.getZ() == min &&
|
||||
boundsMax.getX() == max && boundsMax.getY() == max && boundsMax.getZ() == max) {
|
||||
boundsMin = null;
|
||||
boundsMax = null;
|
||||
}
|
||||
}
|
||||
|
||||
boolean allowImportingBlocks = player.hasPermission("axiom.can_import_blocks");
|
||||
|
||||
if (restrictions.maxSectionsPerSecond != rateLimit ||
|
||||
restrictions.canImportBlocks != allowImportingBlocks ||
|
||||
!Objects.equals(restrictions.boundsMin, boundsMin) ||
|
||||
!Objects.equals(restrictions.boundsMax, boundsMax)) {
|
||||
restrictions.maxSectionsPerSecond = rateLimit;
|
||||
restrictions.canImportBlocks = allowImportingBlocks;
|
||||
restrictions.boundsMin = boundsMin;
|
||||
restrictions.boundsMax = boundsMax;
|
||||
send = true;
|
||||
}
|
||||
|
||||
if (send) {
|
||||
restrictions.send(this, player);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
activeAxiomPlayers.retainAll(stillActiveAxiomPlayers);
|
||||
playerBlockBufferRateLimiters.keySet().retainAll(stillActiveAxiomPlayers);
|
||||
playerRestrictions.keySet().retainAll(stillActiveAxiomPlayers);
|
||||
}, 20, 20);
|
||||
|
||||
boolean sendMarkers = configuration.getBoolean("send-markers");
|
||||
int maxChunkRelightsPerTick = configuration.getInt("max-chunk-relights-per-tick");
|
||||
int maxChunkSendsPerTick = configuration.getInt("max-chunk-sends-per-tick");
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, () -> {
|
||||
WorldExtension.tick(MinecraftServer.getServer(), sendMarkers, maxChunkRelightsPerTick, maxChunkSendsPerTick);
|
||||
}, 1, 1);
|
||||
}
|
||||
|
||||
public boolean logLargeBlockBufferChanges() {
|
||||
return this.logLargeBlockBufferChanges;
|
||||
}
|
||||
|
||||
public boolean canUseAxiom(Player player) {
|
||||
return player.hasPermission("axiom.*") && activeAxiomPlayers.contains(player.getUniqueId());
|
||||
}
|
||||
|
||||
public @Nullable RateLimiter getBlockBufferRateLimiter(UUID uuid) {
|
||||
return this.playerBlockBufferRateLimiters.get(uuid);
|
||||
}
|
||||
|
||||
private final WeakHashMap<World, ServerWorldPropertiesRegistry> worldProperties = new WeakHashMap<>();
|
||||
|
||||
public @Nullable ServerWorldPropertiesRegistry getWorldPropertiesIfPresent(World world) {
|
||||
return worldProperties.get(world);
|
||||
}
|
||||
|
||||
public @Nullable ServerWorldPropertiesRegistry getOrCreateWorldProperties(World world) {
|
||||
if (worldProperties.containsKey(world)) {
|
||||
return worldProperties.get(world);
|
||||
} else {
|
||||
ServerWorldPropertiesRegistry properties = createWorldProperties(world);
|
||||
worldProperties.put(world, properties);
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean canModifyWorld(Player player, World world) {
|
||||
String whitelist = this.configuration.getString("whitelist-world-regex");
|
||||
if (whitelist != null && !world.getName().matches(whitelist)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String blacklist = this.configuration.getString("blacklist-world-regex");
|
||||
if (blacklist != null && world.getName().matches(blacklist)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(player, world);
|
||||
Bukkit.getPluginManager().callEvent(modifyWorldEvent);
|
||||
return !modifyWorldEvent.isCancelled();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onFailMove(PlayerFailMoveEvent event) {
|
||||
if (!this.activeAxiomPlayers.contains(event.getPlayer().getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
if (event.getFailReason() == PlayerFailMoveEvent.FailReason.MOVED_TOO_QUICKLY) {
|
||||
event.setAllowed(true); // Support for arcball camera
|
||||
} else if (event.getPlayer().isFlying()) {
|
||||
event.setAllowed(true); // Support for noclip
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onChangedWorld(PlayerChangedWorldEvent event) {
|
||||
if (!this.activeAxiomPlayers.contains(event.getPlayer().getUniqueId())) {
|
||||
return;
|
||||
}
|
||||
|
||||
World world = event.getPlayer().getWorld();
|
||||
|
||||
ServerWorldPropertiesRegistry properties = getOrCreateWorldProperties(world);
|
||||
|
||||
if (properties == null) {
|
||||
event.getPlayer().sendPluginMessage(this, "axiom:register_world_properties", new byte[]{0});
|
||||
} else {
|
||||
properties.registerFor(this, event.getPlayer());
|
||||
}
|
||||
|
||||
WorldExtension.onPlayerJoin(world, event.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onGameRuleChanged(WorldGameRuleChangeEvent event) {
|
||||
if (event.getGameRule() == GameRule.DO_WEATHER_CYCLE) {
|
||||
ServerWorldPropertiesRegistry.PAUSE_WEATHER.setValue(event.getWorld(), !Boolean.parseBoolean(event.getValue()));
|
||||
}
|
||||
}
|
||||
|
||||
private ServerWorldPropertiesRegistry createWorldProperties(World world) {
|
||||
ServerWorldPropertiesRegistry registry = new ServerWorldPropertiesRegistry(world);
|
||||
|
||||
AxiomCreateWorldPropertiesEvent createEvent = new AxiomCreateWorldPropertiesEvent(world, registry);
|
||||
Bukkit.getPluginManager().callEvent(createEvent);
|
||||
if (createEvent.isCancelled()) return null;
|
||||
|
||||
return registry;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,38 +0,0 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class AxiomPlayerManager implements Listener {
|
||||
|
||||
private final Set<Player> axiomPlayers = Collections.newSetFromMap(new ConcurrentHashMap<>());
|
||||
|
||||
public AxiomPlayerManager() {
|
||||
Bukkit.getPluginManager().registerEvents(this, AxiomPaper.getPlugin());
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(AxiomPaper.getPlugin(), () -> axiomPlayers.removeIf(player -> {
|
||||
if (!player.hasPermission(AxiomConstants.PERMISSION)) {
|
||||
OutChannel.ENABLE.send(player, new byte[] { 0 });
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}), 20, 20);
|
||||
}
|
||||
|
||||
public void add(Player player) {
|
||||
axiomPlayers.add(player);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
axiomPlayers.remove(event.getPlayer());
|
||||
}
|
||||
|
||||
}
|
87
src/main/java/com/moulberry/axiom/DisallowedBlocks.java
Normale Datei
87
src/main/java/com/moulberry/axiom/DisallowedBlocks.java
Normale Datei
@ -0,0 +1,87 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import com.mojang.brigadier.StringReader;
|
||||
import com.moulberry.axiom.buffer.BlockBuffer;
|
||||
import net.minecraft.commands.arguments.blocks.BlockStateParser;
|
||||
import net.minecraft.core.IdMapper;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.properties.Property;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class DisallowedBlocks {
|
||||
|
||||
public static IdMapper<BlockState> createAllowedBlockRegistry(List<String> disallowedBlocks) {
|
||||
List<Predicate<BlockState>> disallowedPredicates = new ArrayList<>();
|
||||
|
||||
for (String disallowedBlock : disallowedBlocks) {
|
||||
try {
|
||||
var parsed = BlockStateParser.parseForTesting(BuiltInRegistries.BLOCK.asLookup(), new StringReader(disallowedBlock), false);
|
||||
|
||||
parsed.left().ifPresent(result -> {
|
||||
disallowedPredicates.add(blockState -> {
|
||||
if (!blockState.is(result.blockState().getBlock())) {
|
||||
return false;
|
||||
} else {
|
||||
for (Property<?> property : result.properties().keySet()) {
|
||||
if (blockState.getValue(property) != result.blockState().getValue(property)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
parsed.right().ifPresent(result -> {
|
||||
disallowedPredicates.add(blockState -> {
|
||||
if (!blockState.is(result.tag())) {
|
||||
return false;
|
||||
} else {
|
||||
for(Map.Entry<String, String> entry : result.vagueProperties().entrySet()) {
|
||||
Property<?> property = blockState.getBlock().getStateDefinition().getProperty(entry.getKey());
|
||||
if (property == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Comparable<?> comparable = property.getValue(entry.getValue()).orElse(null);
|
||||
if (comparable == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (blockState.getValue(property) != comparable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
});
|
||||
});
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
|
||||
IdMapper<BlockState> allowedBlockRegistry = new IdMapper<>();
|
||||
|
||||
// Create allowedBlockRegistry
|
||||
blocks:
|
||||
for (BlockState blockState : Block.BLOCK_STATE_REGISTRY) {
|
||||
for (Predicate<BlockState> disallowedPredicate : disallowedPredicates) {
|
||||
if (disallowedPredicate.test(blockState)) {
|
||||
allowedBlockRegistry.add(BlockBuffer.EMPTY_STATE);
|
||||
continue blocks;
|
||||
}
|
||||
}
|
||||
|
||||
allowedBlockRegistry.add(blockState);
|
||||
}
|
||||
allowedBlockRegistry.addMapping(BlockBuffer.EMPTY_STATE, Block.BLOCK_STATE_REGISTRY.getId(BlockBuffer.EMPTY_STATE));
|
||||
return allowedBlockRegistry;
|
||||
}
|
||||
|
||||
}
|
63
src/main/java/com/moulberry/axiom/NbtSanitization.java
Normale Datei
63
src/main/java/com/moulberry/axiom/NbtSanitization.java
Normale Datei
@ -0,0 +1,63 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public class NbtSanitization {
|
||||
|
||||
private static final Set<String> ALLOWED_KEYS = Set.of(
|
||||
"id", // entity id
|
||||
// generic
|
||||
"Pos",
|
||||
"Rotation",
|
||||
"Invulnerable",
|
||||
"CustomName",
|
||||
"CustomNameVisible",
|
||||
"Silent",
|
||||
"NoGravity",
|
||||
"Glowing",
|
||||
"Tags",
|
||||
"Passengers",
|
||||
// marker
|
||||
"data",
|
||||
// display entity
|
||||
"transformation",
|
||||
"interpolation_duration",
|
||||
"start_interpolation",
|
||||
"teleport_duration",
|
||||
"billboard",
|
||||
"view_range",
|
||||
"shadow_radius",
|
||||
"shadow_strength",
|
||||
"width",
|
||||
"height",
|
||||
"glow_color_override",
|
||||
"brightness",
|
||||
"line_width",
|
||||
"text_opacity",
|
||||
"background",
|
||||
"shadow",
|
||||
"see_through",
|
||||
"default_background",
|
||||
"alignment",
|
||||
"text",
|
||||
"block_state",
|
||||
"item",
|
||||
"item_display"
|
||||
);
|
||||
|
||||
public static void sanitizeEntity(CompoundTag entityRoot) {
|
||||
entityRoot.getAllKeys().retainAll(ALLOWED_KEYS);
|
||||
|
||||
if (entityRoot.contains("Passengers", Tag.TAG_LIST)) {
|
||||
ListTag listTag = entityRoot.getList("Passengers", Tag.TAG_COMPOUND);
|
||||
for (Tag tag : listTag) {
|
||||
sanitizeEntity((CompoundTag) tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public enum OutChannel {
|
||||
ENABLE,
|
||||
INITIALIZE_HOTBARS,
|
||||
SET_EDITOR_VIEWS,
|
||||
BLOCK_ENTITIES;
|
||||
|
||||
private final String channel;
|
||||
|
||||
OutChannel() {
|
||||
channel = "axiom:" + name().toLowerCase();
|
||||
}
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public void send(Player player, ByteBuf buf) {
|
||||
// trustin https://stackoverflow.com/a/19309312 CC BY-SA 3.0
|
||||
byte[] array = new byte[buf.readableBytes()];
|
||||
buf.getBytes(buf.readerIndex(), array);
|
||||
send(player, array);
|
||||
}
|
||||
|
||||
public void send(Player player, byte[] array) {
|
||||
player.sendPluginMessage(AxiomPaper.getPlugin(), channel, array);
|
||||
}
|
||||
}
|
@ -1,169 +0,0 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Reflection {
|
||||
private Reflection() {}
|
||||
|
||||
public interface Field<T, R> {
|
||||
R get(T target);
|
||||
void set(T target, R value);
|
||||
}
|
||||
|
||||
public static <T, R> Field<T, R> getField(Class<T> target, Class<R> fieldType, Class<?>... parameters) {
|
||||
return getField(target, fieldType, 0, parameters);
|
||||
}
|
||||
|
||||
public static <T, R> Field<T, R> getField(Class<T> target, Class<R> fieldType, int index, Class<?>... parameters) {
|
||||
for (final java.lang.reflect.Field field : target.getDeclaredFields()) {
|
||||
if(matching(field, fieldType, parameters) && index-- <= 0) {
|
||||
field.setAccessible(true);
|
||||
|
||||
return new Field<>() {
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public R get(T target) {
|
||||
try {
|
||||
return (R) field.get(target);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Cannot access reflection.", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void set(T target, R value) {
|
||||
try {
|
||||
field.set(target, value);
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalArgumentException("Cannot access reflection.", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (target.getSuperclass() != null) {
|
||||
try {
|
||||
return getField((Class<T>) target.getSuperclass(), fieldType, index, parameters);
|
||||
} catch (ClassCastException e) {}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Cannot find field with type " + fieldType);
|
||||
}
|
||||
|
||||
private static <T> boolean matching(java.lang.reflect.Field field, Class<T> fieldType, Class<?>... parameters) {
|
||||
if(!fieldType.isAssignableFrom(field.getType()))
|
||||
return false;
|
||||
|
||||
if(parameters.length > 0) {
|
||||
Type[] arguments = ((ParameterizedType)field.getGenericType()).getActualTypeArguments();
|
||||
|
||||
for(int i = 0; i < parameters.length; i++) {
|
||||
if(arguments[i] instanceof ParameterizedType ? ((ParameterizedType) arguments[i]).getRawType() != parameters[i] : arguments[i] != parameters[i])
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public interface Method<T, R> {
|
||||
R invoke(T target, Object... arguments);
|
||||
}
|
||||
|
||||
public static <T> Method<T, Void> getMethod(Class<T> clazz, Class<?>... params) {
|
||||
return getTypedMethod(clazz, null, Void.TYPE, 0, params);
|
||||
}
|
||||
|
||||
public static <T> Method<T, Void> getMethod(Class<T> clazz, int index, Class<?>... params) {
|
||||
return getTypedMethod(clazz, null, Void.TYPE, index, params);
|
||||
}
|
||||
|
||||
public static <T> Method<T, Void> getMethod(Class<T> clazz, String methodName, Class<?>... params) {
|
||||
return getTypedMethod(clazz, methodName, Void.TYPE, 0, params);
|
||||
}
|
||||
|
||||
public static <T, R> Method<T, R> getTypedMethod(Class<T> clazz, Class<R> returnType, Class<?>... params) {
|
||||
return getTypedMethod(clazz, null, returnType, 0, params);
|
||||
}
|
||||
|
||||
public static <T, R> Method<T, R> getTypedMethod(Class<T> clazz, Class<R> returnType, int index, Class<?>... params) {
|
||||
return getTypedMethod(clazz, null, returnType, index, params);
|
||||
}
|
||||
|
||||
public static <T, R> Method<T, R> getTypedMethod(Class<T> clazz, String methodName, Class<R> returnType, Class<?>... params) {
|
||||
return getTypedMethod(clazz, methodName, returnType, 0, params);
|
||||
}
|
||||
|
||||
private static <T, R> Method<T, R> getTypedMethod(Class<T> clazz, String methodName, Class<R> returnType, int index, Class<?>... params) {
|
||||
for (final java.lang.reflect.Method method : clazz.getDeclaredMethods()) {
|
||||
if ((methodName == null || method.getName().equals(methodName))
|
||||
&& (returnType == null || method.getReturnType().equals(returnType))
|
||||
&& Arrays.equals(method.getParameterTypes(), params)
|
||||
&& index-- <= 0) {
|
||||
method.setAccessible(true);
|
||||
|
||||
return (target, arguments) -> {
|
||||
try {
|
||||
return (R) method.invoke(target, arguments);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException("Cannot invoke method " + method, e);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (clazz.getSuperclass() != null) {
|
||||
try {
|
||||
return getTypedMethod((Class<T>) clazz.getSuperclass(), methodName, returnType, index, params);
|
||||
} catch (ClassCastException e) {}
|
||||
}
|
||||
|
||||
throw new IllegalStateException(String.format("Unable to find method %s (%s).", methodName, Arrays.asList(params)));
|
||||
}
|
||||
|
||||
|
||||
public interface Constructor<T> {
|
||||
T newInstance(Object... arguments);
|
||||
}
|
||||
|
||||
public static <T> Constructor<T> getConstructor(Class<T> clazz, Class<?>... params) {
|
||||
try {
|
||||
java.lang.reflect.Constructor<T> constructor = clazz.getDeclaredConstructor(params);
|
||||
constructor.setAccessible(true);
|
||||
|
||||
return arguments -> {
|
||||
try {
|
||||
return constructor.newInstance(arguments);
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
|
||||
throw new IllegalArgumentException("Cannot invoke constructor " + constructor, e);
|
||||
}
|
||||
};
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new IllegalStateException("Cannot find matching constructor");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static final String ORG_BUKKIT_CRAFTBUKKIT = Bukkit.getServer().getClass().getPackage().getName();
|
||||
public static final int VERSION; // Format: 2 digit minor version, 2 digit CraftBukkit revision: eg. 2001 for v1_20_R1
|
||||
static {
|
||||
String[] version = ORG_BUKKIT_CRAFTBUKKIT.substring(ORG_BUKKIT_CRAFTBUKKIT.lastIndexOf('.')).split("_");
|
||||
VERSION = Integer.parseInt(version[1]) * 100 + Integer.parseInt(version[2].substring(1));
|
||||
}
|
||||
|
||||
public static <T> Class<T> getClass(String name) {
|
||||
if(name.startsWith("org.bukkit.craftbukkit"))
|
||||
name = ORG_BUKKIT_CRAFTBUKKIT + name.substring(22);
|
||||
|
||||
try {
|
||||
return (Class<T>) Class.forName(name);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalArgumentException("Cannot find " + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
64
src/main/java/com/moulberry/axiom/Restrictions.java
Normale Datei
64
src/main/java/com/moulberry/axiom/Restrictions.java
Normale Datei
@ -0,0 +1,64 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Restrictions {
|
||||
|
||||
public boolean canImportBlocks = true;
|
||||
public boolean canUseEditor = true;
|
||||
public boolean canEditDisplayEntities = true;
|
||||
public int maxSectionsPerSecond = 0;
|
||||
public BlockPos boundsMin = null;
|
||||
public BlockPos boundsMax = null;
|
||||
|
||||
public PlotSquaredIntegration.PlotBounds lastPlotBounds = null;
|
||||
|
||||
public void send(AxiomPaper plugin, Player player) {
|
||||
if (player.getListeningPluginChannels().contains("axiom:restrictions")) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeBoolean(this.canImportBlocks);
|
||||
buf.writeBoolean(this.canUseEditor);
|
||||
buf.writeBoolean(this.canEditDisplayEntities);
|
||||
|
||||
buf.writeVarInt(this.maxSectionsPerSecond);
|
||||
|
||||
if (this.boundsMin == null || this.boundsMax == null) {
|
||||
buf.writeBoolean(false);
|
||||
} else {
|
||||
buf.writeBoolean(true);
|
||||
int minX = this.boundsMin.getX();
|
||||
int minY = this.boundsMin.getY();
|
||||
int minZ = this.boundsMin.getZ();
|
||||
int maxX = this.boundsMax.getX();
|
||||
int maxY = this.boundsMax.getY();
|
||||
int maxZ = this.boundsMax.getZ();
|
||||
|
||||
if (minX < -33554431) minX = -33554431;
|
||||
if (minX > 33554431) minX = 33554431;
|
||||
if (minY < -2047) minY = -2047;
|
||||
if (minY > 2047) minY = 2047;
|
||||
if (minZ < -33554431) minZ = -33554431;
|
||||
if (minZ > 33554431) minZ = 33554431;
|
||||
|
||||
if (maxX < -33554431) maxX = -33554431;
|
||||
if (maxX > 33554431) maxX = 33554431;
|
||||
if (maxY < -2047) maxY = -2047;
|
||||
if (maxY > 2047) maxY = 2047;
|
||||
if (maxZ < -33554431) maxZ = -33554431;
|
||||
if (maxZ > 33554431) maxZ = 33554431;
|
||||
|
||||
buf.writeBlockPos(new BlockPos(minX, minY, minZ));
|
||||
buf.writeBlockPos(new BlockPos(maxX, maxY, maxZ));
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.sendPluginMessage(plugin, "axiom:restrictions", bytes);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,12 +1,13 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.persistence.UUIDDataType;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
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.World;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
import org.bukkit.persistence.PersistentDataType;
|
||||
|
||||
@ -14,76 +15,73 @@ import java.util.UUID;
|
||||
|
||||
public final class View {
|
||||
|
||||
private final String name;
|
||||
private final UUID uuid;
|
||||
private boolean pinLevel = false;
|
||||
private boolean pinLocation = false;
|
||||
private World level = null;
|
||||
private Location location = null;
|
||||
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(ByteBuf buf) {
|
||||
MojBuf.writeUtf(buf, this.name, 64);
|
||||
MojBuf.writeUUID(buf, this.uuid);
|
||||
public void write(FriendlyByteBuf byteBuf) {
|
||||
byteBuf.writeUtf(this.name, 64);
|
||||
byteBuf.writeUUID(this.uuid);
|
||||
|
||||
buf.writeBoolean(this.pinLevel);
|
||||
byteBuf.writeBoolean(this.pinLevel);
|
||||
if (this.pinLevel && this.level != null) {
|
||||
buf.writeBoolean(true);
|
||||
MojBuf.writeKey(buf, this.level.getKey());
|
||||
byteBuf.writeBoolean(true);
|
||||
byteBuf.writeResourceKey(this.level);
|
||||
} else {
|
||||
buf.writeBoolean(false);
|
||||
byteBuf.writeBoolean(false);
|
||||
}
|
||||
|
||||
buf.writeBoolean(this.pinLocation);
|
||||
if (this.location != null) {
|
||||
buf.writeBoolean(true);
|
||||
buf.writeDouble(this.location.getX());
|
||||
buf.writeDouble(this.location.getY());
|
||||
buf.writeDouble(this.location.getZ());
|
||||
buf.writeFloat(this.location.getYaw());
|
||||
buf.writeFloat(this.location.getPitch());
|
||||
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 {
|
||||
buf.writeBoolean(false);
|
||||
byteBuf.writeBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static View read(ByteBuf buf) {
|
||||
View view = new View(MojBuf.readUtf(buf, 64), MojBuf.readUUID(buf));
|
||||
public static View read(FriendlyByteBuf byteBuf) {
|
||||
View view = new View(byteBuf.readUtf(64), byteBuf.readUUID());
|
||||
|
||||
view.pinLevel = buf.readBoolean();
|
||||
if (buf.readBoolean()) {
|
||||
view.level = Bukkit.getWorld(MojBuf.readKey(buf));
|
||||
view.pinLevel = byteBuf.readBoolean();
|
||||
if (byteBuf.readBoolean()) {
|
||||
view.level = byteBuf.readResourceKey(Registries.DIMENSION);
|
||||
}
|
||||
|
||||
view.pinLocation = buf.readBoolean();
|
||||
if (buf.readBoolean()) {
|
||||
view.location = new Location(view.level,
|
||||
buf.readDouble(),
|
||||
buf.readDouble(),
|
||||
buf.readDouble(),
|
||||
buf.readFloat(),
|
||||
buf.readFloat()
|
||||
);
|
||||
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 = AxiomConstants.axiomKey("view_name");
|
||||
private static final NamespacedKey UUID_KEY = AxiomConstants.axiomKey("view_uuid");
|
||||
private static final NamespacedKey PIN_LEVEL_KEY = AxiomConstants.axiomKey("view_pin_level");
|
||||
private static final NamespacedKey LEVEL_KEY = AxiomConstants.axiomKey("view_level");
|
||||
private static final NamespacedKey PIN_LOCATION_KEY = AxiomConstants.axiomKey("view_pin_location");
|
||||
private static final NamespacedKey X_KEY = AxiomConstants.axiomKey("view_x");
|
||||
private static final NamespacedKey Y_KEY = AxiomConstants.axiomKey("view_y");
|
||||
private static final NamespacedKey Z_KEY = AxiomConstants.axiomKey("view_z");
|
||||
private static final NamespacedKey YAW_KEY = AxiomConstants.axiomKey("view_yaw");
|
||||
private static final NamespacedKey PITCH_KEY = AxiomConstants.axiomKey("view_pitch");
|
||||
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);
|
||||
@ -91,16 +89,16 @@ public final class View {
|
||||
|
||||
container.set(PIN_LEVEL_KEY, PersistentDataType.BOOLEAN, this.pinLevel);
|
||||
if (this.pinLevel && this.level != null) {
|
||||
container.set(LEVEL_KEY, PersistentDataType.STRING, this.level.getKey().asString());
|
||||
container.set(LEVEL_KEY, PersistentDataType.STRING, this.level.location().toString());
|
||||
}
|
||||
|
||||
container.set(PIN_LOCATION_KEY, PersistentDataType.BOOLEAN, this.pinLocation);
|
||||
if (this.location != null) {
|
||||
container.set(X_KEY, PersistentDataType.DOUBLE, this.location.getX());
|
||||
container.set(Y_KEY, PersistentDataType.DOUBLE, this.location.getY());
|
||||
container.set(Z_KEY, PersistentDataType.DOUBLE, this.location.getZ());
|
||||
container.set(YAW_KEY, PersistentDataType.FLOAT, this.location.getYaw());
|
||||
container.set(PITCH_KEY, PersistentDataType.FLOAT, this.location.getPitch());
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -113,18 +111,17 @@ public final class View {
|
||||
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 = Bukkit.getWorld(NamespacedKey.fromString(level));
|
||||
view.level = ResourceKey.create(Registries.DIMENSION, new ResourceLocation(level));
|
||||
}
|
||||
|
||||
view.pinLocation = tag.getOrDefault(PIN_LOCATION_KEY, PersistentDataType.BOOLEAN, false);
|
||||
if (tag.has(X_KEY) && tag.has(Y_KEY) && tag.has(Z_KEY)) {
|
||||
view.location = new Location(view.level,
|
||||
tag.getOrDefault(X_KEY, PersistentDataType.DOUBLE, 0.0),
|
||||
tag.getOrDefault(Y_KEY, PersistentDataType.DOUBLE, 0.0),
|
||||
tag.getOrDefault(Z_KEY, PersistentDataType.DOUBLE, 0.0),
|
||||
tag.getOrDefault(YAW_KEY, PersistentDataType.FLOAT, 0.0f),
|
||||
tag.getOrDefault(PITCH_KEY, PersistentDataType.FLOAT, 0.0f)
|
||||
);
|
||||
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;
|
||||
|
176
src/main/java/com/moulberry/axiom/WorldExtension.java
Normale Datei
176
src/main/java/com/moulberry/axiom/WorldExtension.java
Normale Datei
@ -0,0 +1,176 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import com.moulberry.axiom.marker.MarkerData;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import it.unimi.dsi.fastutil.longs.LongIterator;
|
||||
import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
||||
import it.unimi.dsi.fastutil.longs.LongSet;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ChunkMap;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.Marker;
|
||||
import net.minecraft.world.level.ChunkPos;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class WorldExtension {
|
||||
|
||||
private static final Map<ResourceKey<Level>, WorldExtension> extensions = new HashMap<>();
|
||||
|
||||
public static WorldExtension get(ServerLevel serverLevel) {
|
||||
WorldExtension extension = extensions.computeIfAbsent(serverLevel.dimension(), k -> new WorldExtension());
|
||||
extension.level = serverLevel;
|
||||
return extension;
|
||||
}
|
||||
|
||||
public static void onPlayerJoin(World world, Player player) {
|
||||
ServerLevel level = ((CraftWorld)world).getHandle();
|
||||
get(level).onPlayerJoin(player);
|
||||
}
|
||||
|
||||
public static void tick(MinecraftServer server, boolean sendMarkers, int maxChunkRelightsPerTick, int maxChunkSendsPerTick) {
|
||||
extensions.keySet().retainAll(server.levelKeys());
|
||||
|
||||
for (ServerLevel level : server.getAllLevels()) {
|
||||
get(level).tick(sendMarkers, maxChunkRelightsPerTick, maxChunkSendsPerTick);
|
||||
}
|
||||
}
|
||||
|
||||
private ServerLevel level;
|
||||
|
||||
private final LongSet pendingChunksToSend = new LongOpenHashSet();
|
||||
private final LongSet pendingChunksToLight = new LongOpenHashSet();
|
||||
private final Map<UUID, MarkerData> previousMarkerData = new HashMap<>();
|
||||
|
||||
public void sendChunk(int cx, int cz) {
|
||||
this.pendingChunksToSend.add(ChunkPos.asLong(cx, cz));
|
||||
}
|
||||
|
||||
public void lightChunk(int cx, int cz) {
|
||||
this.pendingChunksToLight.add(ChunkPos.asLong(cx, cz));
|
||||
}
|
||||
|
||||
public void onPlayerJoin(Player player) {
|
||||
if (!this.previousMarkerData.isEmpty()) {
|
||||
List<MarkerData> markerData = new ArrayList<>(this.previousMarkerData.values());
|
||||
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeCollection(markerData, MarkerData::write);
|
||||
buf.writeCollection(Set.of(), FriendlyByteBuf::writeUUID);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
|
||||
player.sendPluginMessage(AxiomPaper.PLUGIN, "axiom:marker_data", bytes);
|
||||
}
|
||||
}
|
||||
|
||||
public void tick(boolean sendMarkers, int maxChunkRelightsPerTick, int maxChunkSendsPerTick) {
|
||||
if (sendMarkers) {
|
||||
this.tickMarkers();
|
||||
}
|
||||
this.tickChunkRelight(maxChunkRelightsPerTick, maxChunkSendsPerTick);
|
||||
}
|
||||
|
||||
private void tickMarkers() {
|
||||
List<MarkerData> changedData = new ArrayList<>();
|
||||
|
||||
Set<UUID> allMarkers = new HashSet<>();
|
||||
|
||||
for (Entity entity : this.level.getEntities().getAll()) {
|
||||
if (entity instanceof Marker marker) {
|
||||
MarkerData currentData = MarkerData.createFrom(marker);
|
||||
|
||||
MarkerData previousData = this.previousMarkerData.get(marker.getUUID());
|
||||
if (!Objects.equals(currentData, previousData)) {
|
||||
this.previousMarkerData.put(marker.getUUID(), currentData);
|
||||
changedData.add(currentData);
|
||||
}
|
||||
|
||||
allMarkers.add(marker.getUUID());
|
||||
}
|
||||
}
|
||||
|
||||
Set<UUID> oldUuids = new HashSet<>(this.previousMarkerData.keySet());
|
||||
oldUuids.removeAll(allMarkers);
|
||||
this.previousMarkerData.keySet().removeAll(oldUuids);
|
||||
|
||||
if (!changedData.isEmpty() || !oldUuids.isEmpty()) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeCollection(changedData, MarkerData::write);
|
||||
buf.writeCollection(oldUuids, FriendlyByteBuf::writeUUID);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
|
||||
for (ServerPlayer player : this.level.players()) {
|
||||
if (AxiomPaper.PLUGIN.activeAxiomPlayers.contains(player.getUUID())) {
|
||||
player.getBukkitEntity().sendPluginMessage(AxiomPaper.PLUGIN, "axiom:marker_data", bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void tickChunkRelight(int maxChunkRelightsPerTick, int maxChunkSendsPerTick) {
|
||||
ChunkMap chunkMap = this.level.getChunkSource().chunkMap;
|
||||
|
||||
boolean sendAll = maxChunkSendsPerTick <= 0;
|
||||
|
||||
// Send chunks
|
||||
LongIterator longIterator = this.pendingChunksToSend.longIterator();
|
||||
while (longIterator.hasNext()) {
|
||||
ChunkPos chunkPos = new ChunkPos(longIterator.nextLong());
|
||||
List<ServerPlayer> players = chunkMap.getPlayers(chunkPos, false);
|
||||
if (players.isEmpty()) continue;
|
||||
|
||||
LevelChunk chunk = this.level.getChunk(chunkPos.x, chunkPos.z);
|
||||
var packet = new ClientboundLevelChunkWithLightPacket(chunk, this.level.getLightEngine(), null, null, false);
|
||||
for (ServerPlayer player : players) {
|
||||
player.connection.send(packet);
|
||||
}
|
||||
|
||||
if (!sendAll) {
|
||||
longIterator.remove();
|
||||
|
||||
maxChunkSendsPerTick -= 1;
|
||||
if (maxChunkSendsPerTick <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (sendAll) {
|
||||
this.pendingChunksToSend.clear();
|
||||
}
|
||||
|
||||
// Relight chunks
|
||||
Set<ChunkPos> chunkSet = new HashSet<>();
|
||||
longIterator = this.pendingChunksToLight.longIterator();
|
||||
if (maxChunkRelightsPerTick <= 0) {
|
||||
while (longIterator.hasNext()) {
|
||||
chunkSet.add(new ChunkPos(longIterator.nextLong()));
|
||||
}
|
||||
this.pendingChunksToLight.clear();
|
||||
} else {
|
||||
while (longIterator.hasNext()) {
|
||||
chunkSet.add(new ChunkPos(longIterator.nextLong()));
|
||||
longIterator.remove();
|
||||
|
||||
maxChunkRelightsPerTick -= 1;
|
||||
if (maxChunkRelightsPerTick <= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.level.getChunkSource().getLightEngine().relight(chunkSet, pos -> {}, count -> {});
|
||||
}
|
||||
|
||||
}
|
66
src/main/java/com/moulberry/axiom/WorldPropertiesExample.java
Normale Datei
66
src/main/java/com/moulberry/axiom/WorldPropertiesExample.java
Normale Datei
@ -0,0 +1,66 @@
|
||||
package com.moulberry.axiom;
|
||||
|
||||
import com.moulberry.axiom.event.AxiomCreateWorldPropertiesEvent;
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyCategory;
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
|
||||
import com.moulberry.axiom.world_properties.server.PropertyUpdateResult;
|
||||
import com.moulberry.axiom.world_properties.server.ServerWorldProperty;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class WorldPropertiesExample implements Listener {
|
||||
|
||||
private static final ServerWorldProperty<Boolean> CHECKBOX = new ServerWorldProperty<>(
|
||||
new NamespacedKey("axiom", "checkbox"),
|
||||
"Checkbox",
|
||||
false, WorldPropertyWidgetType.CHECKBOX, world -> false,
|
||||
(player, world, bool) -> {
|
||||
world.sendMessage(Component.text("Checkbox: " + bool)); // Do something with input
|
||||
return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client
|
||||
}
|
||||
);
|
||||
|
||||
private static final ServerWorldProperty<Integer> SLIDER = new ServerWorldProperty<>(
|
||||
new NamespacedKey("axiom", "slider"),
|
||||
"Slider",
|
||||
false, new WorldPropertyWidgetType.Slider(0, 8),
|
||||
world -> 4,
|
||||
(player, world, integer) -> {
|
||||
world.sendMessage(Component.text("Slider: " + integer)); // Do something with input
|
||||
return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client
|
||||
}
|
||||
);
|
||||
|
||||
private static final ServerWorldProperty<String> TEXTBOX = new ServerWorldProperty<>(
|
||||
new NamespacedKey("axiom", "textbox"),
|
||||
"Textbox",
|
||||
false, WorldPropertyWidgetType.TEXTBOX,
|
||||
world -> "Hello",
|
||||
(player, world, string) -> {
|
||||
world.sendMessage(Component.text("Textbox: " + string)); // Do something with input
|
||||
return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client
|
||||
}
|
||||
);
|
||||
|
||||
private static final ServerWorldProperty<Void> BUTTON = new ServerWorldProperty<>(
|
||||
new NamespacedKey("axiom", "button"),
|
||||
"Button",
|
||||
false, WorldPropertyWidgetType.BUTTON,
|
||||
world -> null,
|
||||
(player, world, unit) -> {
|
||||
world.sendMessage(Component.text("Button pressed")); // Do something with input
|
||||
return PropertyUpdateResult.UPDATE_AND_SYNC; // sync with client
|
||||
}
|
||||
);
|
||||
|
||||
@EventHandler
|
||||
public void onCreateWorldProperties(AxiomCreateWorldPropertiesEvent event) {
|
||||
WorldPropertyCategory category = new WorldPropertyCategory("Examples", false);
|
||||
event.addCategory(category, List.of(CHECKBOX, SLIDER, TEXTBOX, BUTTON));
|
||||
}
|
||||
|
||||
}
|
@ -1,26 +1,96 @@
|
||||
package com.moulberry.axiom.buffer;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.bukkit.block.Biome;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ByteMap;
|
||||
import it.unimi.dsi.fastutil.objects.Object2ByteOpenHashMap;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class BiomeBuffer {
|
||||
|
||||
private final Position2ByteMap map;
|
||||
private final Biome[] palette;
|
||||
private final ResourceKey<Biome>[] palette;
|
||||
private final Object2ByteMap<ResourceKey<Biome>> paletteReverse;
|
||||
private int paletteSize = 0;
|
||||
|
||||
public BiomeBuffer(ByteBuf buf) {
|
||||
palette = new Biome[buf.readByte()];
|
||||
for (int i = 0; i < palette.length; i++) {
|
||||
palette[i] = Biome.valueOf(MojBuf.readKey(buf).getKey().toUpperCase());
|
||||
public BiomeBuffer() {
|
||||
this.map = new Position2ByteMap();
|
||||
this.palette = new ResourceKey[255];
|
||||
this.paletteReverse = new Object2ByteOpenHashMap<>();
|
||||
}
|
||||
|
||||
map = Position2ByteMap.load(buf);
|
||||
private BiomeBuffer(Position2ByteMap map, ResourceKey<Biome>[] palette, Object2ByteMap<ResourceKey<Biome>> paletteReverse) {
|
||||
this.map = map;
|
||||
this.palette = palette;
|
||||
this.paletteReverse = paletteReverse;
|
||||
this.paletteSize = this.paletteReverse.size();
|
||||
}
|
||||
|
||||
public void forEachEntry(PositionConsumer<Biome> consumer) {
|
||||
public int size() {
|
||||
return this.map.size();
|
||||
}
|
||||
|
||||
public void save(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeByte(this.paletteSize);
|
||||
for (int i = 0; i < this.paletteSize; i++) {
|
||||
friendlyByteBuf.writeResourceKey(this.palette[i]);
|
||||
}
|
||||
this.map.save(friendlyByteBuf);
|
||||
}
|
||||
|
||||
public static BiomeBuffer load(FriendlyByteBuf friendlyByteBuf, @Nullable RateLimiter rateLimiter, AtomicBoolean reachedRateLimit) {
|
||||
int paletteSize = friendlyByteBuf.readByte();
|
||||
ResourceKey<Biome>[] palette = new ResourceKey[255];
|
||||
Object2ByteMap<ResourceKey<Biome>> paletteReverse = new Object2ByteOpenHashMap<>();
|
||||
for (int i = 0; i < paletteSize; i++) {
|
||||
ResourceKey<Biome> key = friendlyByteBuf.readResourceKey(Registries.BIOME);
|
||||
palette[i] = key;
|
||||
paletteReverse.put(key, (byte)(i+1));
|
||||
}
|
||||
Position2ByteMap map = Position2ByteMap.load(friendlyByteBuf, rateLimiter, reachedRateLimit);
|
||||
return new BiomeBuffer(map, palette, paletteReverse);
|
||||
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
this.map.clear();
|
||||
}
|
||||
|
||||
public void forEachEntry(PositionConsumer<ResourceKey<Biome>> consumer) {
|
||||
this.map.forEachEntry((x, y, z, v) -> {
|
||||
if (v != 0) consumer.accept(x, y, z, this.palette[(v & 0xFF) - 1]);
|
||||
});
|
||||
}
|
||||
|
||||
public ResourceKey<Biome> get(int quartX, int quartY, int quartZ) {
|
||||
int index = this.map.get(quartX, quartY, quartZ) & 0xFF;
|
||||
if (index == 0) return null;
|
||||
return this.palette[index - 1];
|
||||
}
|
||||
|
||||
private int getPaletteIndex(ResourceKey<Biome> biome) {
|
||||
int index = this.paletteReverse.getOrDefault(biome, (byte) 0) & 0xFF;
|
||||
if (index != 0) return index;
|
||||
|
||||
index = this.paletteSize;
|
||||
if (index >= this.palette.length) {
|
||||
throw new UnsupportedOperationException("Too many biomes! :(");
|
||||
}
|
||||
|
||||
this.palette[index] = biome;
|
||||
this.paletteReverse.put(biome, (byte)(index + 1));
|
||||
|
||||
this.paletteSize += 1;
|
||||
return index + 1;
|
||||
}
|
||||
|
||||
public void set(int quartX, int quartY, int quartZ, ResourceKey<Biome> biome) {
|
||||
this.map.put(quartX, quartY, quartZ, (byte) this.getPaletteIndex(biome));
|
||||
}
|
||||
|
||||
}
|
||||
|
187
src/main/java/com/moulberry/axiom/buffer/BlockBuffer.java
Normale Datei
187
src/main/java/com/moulberry/axiom/buffer/BlockBuffer.java
Normale Datei
@ -0,0 +1,187 @@
|
||||
package com.moulberry.axiom.buffer;
|
||||
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import it.unimi.dsi.fastutil.objects.ObjectSet;
|
||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.PalettedContainer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class BlockBuffer {
|
||||
|
||||
public static final BlockState EMPTY_STATE = Blocks.STRUCTURE_VOID.defaultBlockState();
|
||||
|
||||
private final Long2ObjectMap<PalettedContainer<BlockState>> values;
|
||||
|
||||
private PalettedContainer<BlockState> last = null;
|
||||
private long lastId = AxiomConstants.MIN_POSITION_LONG;
|
||||
private final Long2ObjectMap<Short2ObjectMap<CompressedBlockEntity>> blockEntities = new Long2ObjectOpenHashMap<>();
|
||||
private long totalBlockEntities = 0;
|
||||
private long totalBlockEntityBytes = 0;
|
||||
|
||||
public BlockBuffer() {
|
||||
this.values = new Long2ObjectOpenHashMap<>();
|
||||
}
|
||||
|
||||
public BlockBuffer(Long2ObjectMap<PalettedContainer<BlockState>> values) {
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
public void save(FriendlyByteBuf friendlyByteBuf) {
|
||||
for (Long2ObjectMap.Entry<PalettedContainer<BlockState>> entry : this.entrySet()) {
|
||||
friendlyByteBuf.writeLong(entry.getLongKey());
|
||||
entry.getValue().write(friendlyByteBuf);
|
||||
|
||||
Short2ObjectMap<CompressedBlockEntity> blockEntities = this.blockEntities.get(entry.getLongKey());
|
||||
if (blockEntities != null) {
|
||||
friendlyByteBuf.writeVarInt(blockEntities.size());
|
||||
for (Short2ObjectMap.Entry<CompressedBlockEntity> entry2 : blockEntities.short2ObjectEntrySet()) {
|
||||
friendlyByteBuf.writeShort(entry2.getShortKey());
|
||||
entry2.getValue().write(friendlyByteBuf);
|
||||
}
|
||||
} else {
|
||||
friendlyByteBuf.writeVarInt(0);
|
||||
}
|
||||
}
|
||||
|
||||
friendlyByteBuf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
}
|
||||
|
||||
public static BlockBuffer load(FriendlyByteBuf friendlyByteBuf, @Nullable RateLimiter rateLimiter, AtomicBoolean reachedRateLimit) {
|
||||
BlockBuffer buffer = new BlockBuffer();
|
||||
|
||||
long totalBlockEntities = 0;
|
||||
long totalBlockEntityBytes = 0;
|
||||
|
||||
while (true) {
|
||||
long index = friendlyByteBuf.readLong();
|
||||
if (index == AxiomConstants.MIN_POSITION_LONG) break;
|
||||
|
||||
if (rateLimiter != null) {
|
||||
if (!rateLimiter.tryAcquire()) {
|
||||
reachedRateLimit.set(true);
|
||||
buffer.totalBlockEntities = totalBlockEntities;
|
||||
buffer.totalBlockEntityBytes = totalBlockEntityBytes;
|
||||
return buffer;
|
||||
}
|
||||
}
|
||||
|
||||
PalettedContainer<BlockState> palettedContainer = buffer.getOrCreateSection(index);
|
||||
palettedContainer.read(friendlyByteBuf);
|
||||
|
||||
int blockEntitySize = Math.min(4096, friendlyByteBuf.readVarInt());
|
||||
if (blockEntitySize > 0) {
|
||||
Short2ObjectMap<CompressedBlockEntity> map = new Short2ObjectOpenHashMap<>(blockEntitySize);
|
||||
|
||||
int startIndex = friendlyByteBuf.readerIndex();
|
||||
|
||||
for (int i = 0; i < blockEntitySize; i++) {
|
||||
short offset = friendlyByteBuf.readShort();
|
||||
CompressedBlockEntity blockEntity = CompressedBlockEntity.read(friendlyByteBuf);
|
||||
map.put(offset, blockEntity);
|
||||
}
|
||||
|
||||
buffer.blockEntities.put(index, map);
|
||||
totalBlockEntities += blockEntitySize;
|
||||
totalBlockEntityBytes += friendlyByteBuf.readerIndex() - startIndex;
|
||||
}
|
||||
}
|
||||
|
||||
buffer.totalBlockEntities = totalBlockEntities;
|
||||
buffer.totalBlockEntityBytes = totalBlockEntityBytes;
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public long getTotalBlockEntities() {
|
||||
return this.totalBlockEntities;
|
||||
}
|
||||
|
||||
public long getTotalBlockEntityBytes() {
|
||||
return this.totalBlockEntityBytes;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Short2ObjectMap<CompressedBlockEntity> getBlockEntityChunkMap(long cpos) {
|
||||
return this.blockEntities.get(cpos);
|
||||
}
|
||||
|
||||
public BlockState get(int x, int y, int z) {
|
||||
var container = this.getSectionForCoord(x, y, z);
|
||||
if (container == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var state = container.get(x & 0xF, y & 0xF, z & 0xF);
|
||||
if (state == EMPTY_STATE) {
|
||||
return null;
|
||||
} else {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
public void set(int x, int y, int z, BlockState state) {
|
||||
var container = this.getOrCreateSectionForCoord(x, y, z);
|
||||
var old = container.getAndSet(x & 0xF, y & 0xF, z & 0xF, state);
|
||||
}
|
||||
|
||||
public void set(int cx, int cy, int cz, int lx, int ly, int lz, BlockState state) {
|
||||
var container = this.getOrCreateSection(BlockPos.asLong(cx, cy, cz));
|
||||
var old = container.getAndSet(lx, ly, lz, state);
|
||||
}
|
||||
|
||||
public BlockState remove(int x, int y, int z) {
|
||||
var container = this.getSectionForCoord(x, y, z);
|
||||
if (container == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
var state = container.get(x & 0xF, y & 0xF, z & 0xF);
|
||||
if (state == EMPTY_STATE) {
|
||||
return null;
|
||||
} else {
|
||||
container.set(x & 0xF, y & 0xF, z & 0xF, EMPTY_STATE);
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
public ObjectSet<Long2ObjectMap.Entry<PalettedContainer<BlockState>>> entrySet() {
|
||||
return this.values.long2ObjectEntrySet();
|
||||
}
|
||||
|
||||
public PalettedContainer<BlockState> getSectionForCoord(int x, int y, int z) {
|
||||
long id = BlockPos.asLong(x >> 4, y >> 4, z >> 4);
|
||||
|
||||
if (id != this.lastId) {
|
||||
this.lastId = id;
|
||||
this.last = this.values.get(id);
|
||||
}
|
||||
|
||||
return this.last;
|
||||
}
|
||||
|
||||
public PalettedContainer<BlockState> getOrCreateSectionForCoord(int x, int y, int z) {
|
||||
long id = BlockPos.asLong(x >> 4, y >> 4, z >> 4);
|
||||
return this.getOrCreateSection(id);
|
||||
}
|
||||
|
||||
public PalettedContainer<BlockState> getOrCreateSection(long id) {
|
||||
if (this.last == null || id != this.lastId) {
|
||||
this.lastId = id;
|
||||
this.last = this.values.computeIfAbsent(id, k -> new PalettedContainer<>(AxiomPaper.PLUGIN.allowedBlockRegistry,
|
||||
EMPTY_STATE, PalettedContainer.Strategy.SECTION_STATES));
|
||||
}
|
||||
|
||||
return this.last;
|
||||
}
|
||||
|
||||
}
|
@ -3,57 +3,65 @@ package com.moulberry.axiom.buffer;
|
||||
import com.github.luben.zstd.Zstd;
|
||||
import com.github.luben.zstd.ZstdDictCompress;
|
||||
import com.github.luben.zstd.ZstdDictDecompress;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.NbtAccounter;
|
||||
import net.minecraft.nbt.NbtIo;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Objects;
|
||||
|
||||
public record CompressedBlockEntity(int originalSize, byte compressionDict, byte[] compressed) {
|
||||
|
||||
private static final ZstdDictCompress zstdDictCompress;
|
||||
private static final ZstdDictDecompress zstdDictDecompress;
|
||||
static {
|
||||
try (InputStream is = Objects.requireNonNull(CompressedBlockEntity.class.getClassLoader().getResourceAsStream("zstd_dictionaries/block_entities_v1.dict"))) {
|
||||
private static ZstdDictCompress zstdDictCompress = null;
|
||||
private static ZstdDictDecompress zstdDictDecompress = null;
|
||||
|
||||
public static void initialize(AxiomPaper plugin) {
|
||||
try (InputStream is = Objects.requireNonNull(plugin.getResource("zstd_dictionaries/block_entities_v1.dict"))) {
|
||||
byte[] bytes = is.readAllBytes();
|
||||
zstdDictCompress = new ZstdDictCompress(bytes, Zstd.defaultCompressionLevel());
|
||||
zstdDictDecompress = new ZstdDictDecompress(bytes);
|
||||
} catch (IOException e) {
|
||||
throw new IllegalStateException(e);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Reflection.Method<NbtIo, Void> write = Reflection.getMethod(NbtIo.class, CompoundTag.class, DataOutput.class);
|
||||
public static CompressedBlockEntity compress(CompoundTag tag, ByteArrayOutputStream baos) {
|
||||
try {
|
||||
baos.reset();
|
||||
DataOutputStream dos = new DataOutputStream(baos);
|
||||
write.invoke(null, tag, dos);
|
||||
NbtIo.write(tag, dos);
|
||||
byte[] uncompressed = baos.toByteArray();
|
||||
byte[] compressed = Zstd.compress(uncompressed, zstdDictCompress);
|
||||
return new CompressedBlockEntity(uncompressed.length, (byte) 0, compressed);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Reflection.Method<NbtIo, CompoundTag> read = Reflection.getTypedMethod(NbtIo.class, CompoundTag.class, DataInput.class);
|
||||
public CompoundTag decompress() {
|
||||
if (this.compressionDict != 0) throw new UnsupportedOperationException("Unknown compression dict: " + this.compressionDict);
|
||||
|
||||
try {
|
||||
byte[] nbt = Zstd.decompress(this.compressed, zstdDictDecompress, this.originalSize);
|
||||
return read.invoke(null, new DataInputStream(new ByteArrayInputStream(nbt)));
|
||||
return NbtIo.read(new DataInputStream(new ByteArrayInputStream(nbt)), new NbtAccounter(131072));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static CompressedBlockEntity read(ByteBuf buf) {
|
||||
int originalSize = MojBuf.readVarInt(buf);
|
||||
byte compressionDict = buf.readByte();
|
||||
byte[] compressed = MojBuf.readByteArray(buf);
|
||||
public static CompressedBlockEntity read(FriendlyByteBuf friendlyByteBuf) {
|
||||
int originalSize = friendlyByteBuf.readVarInt();
|
||||
byte compressionDict = friendlyByteBuf.readByte();
|
||||
byte[] compressed = friendlyByteBuf.readByteArray();
|
||||
return new CompressedBlockEntity(originalSize, compressionDict, compressed);
|
||||
}
|
||||
|
||||
public void write(ByteBuf buf) {
|
||||
MojBuf.writeVarInt(buf, this.originalSize);
|
||||
buf.writeByte(this.compressionDict);
|
||||
MojBuf.writeByteArray(buf, this.compressed);
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeVarInt(this.originalSize);
|
||||
friendlyByteBuf.writeByte(this.compressionDict);
|
||||
friendlyByteBuf.writeByteArray(this.compressed);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,116 +0,0 @@
|
||||
package com.moulberry.axiom.buffer;
|
||||
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class MojBuf {
|
||||
private MojBuf() {}
|
||||
|
||||
public static ByteBuf unpooled() {
|
||||
return new FriendlyByteBuf(Unpooled.buffer());
|
||||
}
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, String> readUtf = Reflection.getTypedMethod(FriendlyByteBuf.class, String.class, int.class);
|
||||
public static String readUtf(ByteBuf buf, int maxLength) {
|
||||
return readUtf.invoke((FriendlyByteBuf) buf, maxLength);
|
||||
}
|
||||
|
||||
public static String readUtf(ByteBuf buf) {
|
||||
return readUtf(buf, Short.MAX_VALUE);
|
||||
}
|
||||
|
||||
public static NamespacedKey readKey(ByteBuf buf) {
|
||||
return NamespacedKey.fromString(readUtf(buf));
|
||||
}
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, FriendlyByteBuf> writeUtf = Reflection.getTypedMethod(FriendlyByteBuf.class, FriendlyByteBuf.class, String.class, int.class);
|
||||
public static void writeUtf(ByteBuf buf, String string, int maxLength) {
|
||||
writeUtf.invoke((FriendlyByteBuf) buf, string, maxLength);
|
||||
}
|
||||
|
||||
public static void writeUtf(ByteBuf buf, String string) {
|
||||
writeUtf(buf, string, Short.MAX_VALUE);
|
||||
}
|
||||
|
||||
public static void writeKey(ByteBuf buf, NamespacedKey key) {
|
||||
writeUtf(buf, key.asString());
|
||||
}
|
||||
|
||||
// is the first method returning an int with no arguments in 1.20.1
|
||||
private static final Reflection.Method<FriendlyByteBuf, Integer> readVarInt = Reflection.getTypedMethod(FriendlyByteBuf.class, int.class);
|
||||
public static int readVarInt(ByteBuf buf) {
|
||||
return readVarInt.invoke((FriendlyByteBuf) buf);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, FriendlyByteBuf> writeVarInt = Reflection.getTypedMethod(FriendlyByteBuf.class, FriendlyByteBuf.class, int.class);
|
||||
public static void writeVarInt(ByteBuf buf, int value) {
|
||||
writeVarInt.invoke((FriendlyByteBuf) buf, value);
|
||||
}
|
||||
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, CompoundTag> readNbt = Reflection.getTypedMethod(FriendlyByteBuf.class, CompoundTag.class);
|
||||
public static CompoundTag readNbt(ByteBuf buf) {
|
||||
return readNbt.invoke((FriendlyByteBuf) buf);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, UUID> readUUID = Reflection.getTypedMethod(FriendlyByteBuf.class, UUID.class);
|
||||
public static UUID readUUID(ByteBuf buf) {
|
||||
return readUUID.invoke((FriendlyByteBuf) buf);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, FriendlyByteBuf> writeUUID = Reflection.getTypedMethod(FriendlyByteBuf.class, FriendlyByteBuf.class, UUID.class);
|
||||
public static void writeUUID(ByteBuf buf, UUID uuid) {
|
||||
writeUUID.invoke((FriendlyByteBuf) buf, uuid);
|
||||
}
|
||||
|
||||
private static final Class<ItemStack> CraftItemStack = Reflection.getClass("org.bukkit.craftbukkit.inventory.CraftItemStack");
|
||||
private static final Reflection.Method<ItemStack, ItemStack> asCraftMirror = Reflection.getTypedMethod(CraftItemStack, "asCraftMirror", CraftItemStack, net.minecraft.world.item.ItemStack.class);
|
||||
public static ItemStack toBukkit(net.minecraft.world.item.ItemStack stack) {
|
||||
return asCraftMirror.invoke(null, stack);
|
||||
}
|
||||
private static final Reflection.Method<ItemStack, net.minecraft.world.item.ItemStack> asNMSCopy = Reflection.getTypedMethod(CraftItemStack, net.minecraft.world.item.ItemStack.class, ItemStack.class);
|
||||
public static net.minecraft.world.item.ItemStack toMojang(ItemStack stack) {
|
||||
return asNMSCopy.invoke(null, stack);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, net.minecraft.world.item.ItemStack> readItem = Reflection.getTypedMethod(FriendlyByteBuf.class, net.minecraft.world.item.ItemStack.class);
|
||||
public static ItemStack readItem(ByteBuf buf) {
|
||||
return toBukkit(readItem.invoke((FriendlyByteBuf) buf));
|
||||
}
|
||||
|
||||
private static final Reflection.Method<FriendlyByteBuf, FriendlyByteBuf> writeItem = Reflection.getTypedMethod(FriendlyByteBuf.class, FriendlyByteBuf.class, net.minecraft.world.item.ItemStack.class);
|
||||
public static void writeItem(ByteBuf buf, ItemStack stack) {
|
||||
writeItem.invoke((FriendlyByteBuf) buf, toMojang(stack));
|
||||
}
|
||||
|
||||
public static byte[] readByteArray(ByteBuf buf) {
|
||||
byte[] array = new byte[readVarInt(buf)];
|
||||
buf.readBytes(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
public static void writeByteArray(ByteBuf buf, byte[] array) {
|
||||
writeVarInt(buf, array.length);
|
||||
buf.writeBytes(array);
|
||||
}
|
||||
|
||||
public static <T> List<T> readList(ByteBuf buf, Function<ByteBuf, T> reader) {
|
||||
int length = readVarInt(buf);
|
||||
|
||||
List<T> list = new ArrayList<>(length);
|
||||
for(int i = 0; i < length; i++)
|
||||
list.add(reader.apply(buf));
|
||||
|
||||
return list;
|
||||
}
|
||||
}
|
@ -1,11 +1,16 @@
|
||||
package com.moulberry.axiom.buffer;
|
||||
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.function.LongFunction;
|
||||
|
||||
public class Position2ByteMap {
|
||||
|
||||
@ -15,35 +20,133 @@ public class Position2ByteMap {
|
||||
}
|
||||
|
||||
private final byte defaultValue;
|
||||
private final LongFunction<byte[]> defaultFunction;
|
||||
private final Long2ObjectMap<byte[]> map = new Long2ObjectOpenHashMap<>();
|
||||
|
||||
private long lastChunkPos = AxiomConstants.MIN_POSITION_LONG;
|
||||
private byte[] lastChunk = null;
|
||||
|
||||
public Position2ByteMap() {
|
||||
this((byte) 0);
|
||||
}
|
||||
|
||||
public Position2ByteMap(byte defaultValue) {
|
||||
this.defaultValue = defaultValue;
|
||||
|
||||
if (defaultValue == 0) {
|
||||
this.defaultFunction = k -> new byte[16*16*16];
|
||||
} else {
|
||||
this.defaultFunction = k -> {
|
||||
byte[] array = new byte[16*16*16];
|
||||
Arrays.fill(array, defaultValue);
|
||||
return array;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public static Position2ByteMap load(ByteBuf byteBuf) {
|
||||
Position2ByteMap map = new Position2ByteMap(byteBuf.readByte());
|
||||
public int size() {
|
||||
return this.map.size();
|
||||
}
|
||||
|
||||
public void save(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeByte(this.defaultValue);
|
||||
for (Long2ObjectMap.Entry<byte[]> entry : this.map.long2ObjectEntrySet()) {
|
||||
friendlyByteBuf.writeLong(entry.getLongKey());
|
||||
friendlyByteBuf.writeBytes(entry.getValue());
|
||||
}
|
||||
friendlyByteBuf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
}
|
||||
|
||||
public static Position2ByteMap load(FriendlyByteBuf friendlyByteBuf, @Nullable RateLimiter rateLimiter, AtomicBoolean reachedRateLimit) {
|
||||
Position2ByteMap map = new Position2ByteMap(friendlyByteBuf.readByte());
|
||||
|
||||
while (true) {
|
||||
long pos = byteBuf.readLong();
|
||||
long pos = friendlyByteBuf.readLong();
|
||||
if (pos == AxiomConstants.MIN_POSITION_LONG) break;
|
||||
|
||||
if (rateLimiter != null) {
|
||||
if (!rateLimiter.tryAcquire()) {
|
||||
reachedRateLimit.set(true);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[16*16*16];
|
||||
byteBuf.readBytes(bytes);
|
||||
friendlyByteBuf.readBytes(bytes);
|
||||
map.map.put(pos, bytes);
|
||||
}
|
||||
|
||||
return map;
|
||||
}
|
||||
|
||||
private static final Reflection.Method<BlockPos, Integer> getX = Reflection.getTypedMethod(BlockPos.class, int.class, 0, long.class);
|
||||
private static final Reflection.Method<BlockPos, Integer> getY = Reflection.getTypedMethod(BlockPos.class, int.class, 1, long.class);
|
||||
private static final Reflection.Method<BlockPos, Integer> getZ = Reflection.getTypedMethod(BlockPos.class, int.class, 2, long.class);
|
||||
public void clear() {
|
||||
this.map.clear();
|
||||
this.lastChunkPos = AxiomConstants.MIN_POSITION_LONG;
|
||||
this.lastChunk = null;
|
||||
}
|
||||
|
||||
public byte get(int x, int y, int z) {
|
||||
int xC = x >> 4;
|
||||
int yC = y >> 4;
|
||||
int zC = z >> 4;
|
||||
|
||||
byte[] array = this.getChunk(xC, yC, zC);
|
||||
if (array == null) return this.defaultValue;
|
||||
|
||||
return array[(x&15) + (y&15)*16 + (z&15)*16*16];
|
||||
}
|
||||
|
||||
public void put(int x, int y, int z, byte v) {
|
||||
int xC = x >> 4;
|
||||
int yC = y >> 4;
|
||||
int zC = z >> 4;
|
||||
|
||||
byte[] array = this.getOrCreateChunk(xC, yC, zC);
|
||||
array[(x&15) + (y&15)*16 + (z&15)*16*16] = v;
|
||||
}
|
||||
|
||||
public byte add(int x, int y, int z, byte v) {
|
||||
if (v == 0) return this.get(x, y, z);
|
||||
|
||||
int xC = x >> 4;
|
||||
int yC = y >> 4;
|
||||
int zC = z >> 4;
|
||||
|
||||
byte[] array = this.getOrCreateChunk(xC, yC, zC);
|
||||
return array[(x&15) + (y&15)*16 + (z&15)*16*16] += v;
|
||||
}
|
||||
|
||||
|
||||
public byte binaryAnd(int x, int y, int z, byte v) {
|
||||
int xC = x >> 4;
|
||||
int yC = y >> 4;
|
||||
int zC = z >> 4;
|
||||
|
||||
byte[] array = this.getOrCreateChunk(xC, yC, zC);
|
||||
return array[(x&15) + (y&15)*16 + (z&15)*16*16] &= v;
|
||||
}
|
||||
|
||||
public boolean min(int x, int y, int z, byte v) {
|
||||
int xC = x >> 4;
|
||||
int yC = y >> 4;
|
||||
int zC = z >> 4;
|
||||
|
||||
byte[] array = this.getOrCreateChunk(xC, yC, zC);
|
||||
int index = (x&15) + (y&15)*16 + (z&15)*16*16;
|
||||
|
||||
if (v < array[index]) {
|
||||
array[index] = v;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void forEachEntry(EntryConsumer consumer) {
|
||||
for (Long2ObjectMap.Entry<byte[]> entry : this.map.long2ObjectEntrySet()) {
|
||||
int cx = getX.invoke(null, entry.getLongKey()) * 16;
|
||||
int cy = getY.invoke(null, entry.getLongKey()) * 16;
|
||||
int cz = getZ.invoke(null, entry.getLongKey()) * 16;
|
||||
int cx = BlockPos.getX(entry.getLongKey()) * 16;
|
||||
int cy = BlockPos.getY(entry.getLongKey()) * 16;
|
||||
int cz = BlockPos.getZ(entry.getLongKey()) * 16;
|
||||
|
||||
int index = 0;
|
||||
for (int z=0; z<16; z++) {
|
||||
@ -59,4 +162,32 @@ public class Position2ByteMap {
|
||||
}
|
||||
}
|
||||
|
||||
public byte[] getChunk(int xC, int yC, int zC) {
|
||||
return this.getChunk(BlockPos.asLong(xC, yC, zC));
|
||||
}
|
||||
|
||||
public byte[] getChunk(long pos) {
|
||||
if (this.lastChunkPos != pos) {
|
||||
byte[] chunk = this.map.get(pos);
|
||||
this.lastChunkPos = pos;
|
||||
this.lastChunk = chunk;
|
||||
}
|
||||
|
||||
return this.lastChunk;
|
||||
}
|
||||
|
||||
public byte[] getOrCreateChunk(int xC, int yC, int zC) {
|
||||
return this.getOrCreateChunk(BlockPos.asLong(xC, yC, zC));
|
||||
}
|
||||
|
||||
public byte[] getOrCreateChunk(long pos) {
|
||||
if (this.lastChunk == null || this.lastChunkPos != pos) {
|
||||
byte[] chunk = this.map.computeIfAbsent(pos, this.defaultFunction);
|
||||
this.lastChunkPos = pos;
|
||||
this.lastChunk = chunk;
|
||||
}
|
||||
|
||||
return this.lastChunk;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,54 @@
|
||||
package com.moulberry.axiom.event;
|
||||
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyCategory;
|
||||
import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry;
|
||||
import com.moulberry.axiom.world_properties.server.ServerWorldPropertyBase;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class AxiomCreateWorldPropertiesEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
|
||||
private final World world;
|
||||
private final ServerWorldPropertiesRegistry registry;
|
||||
private boolean cancelled = false;
|
||||
|
||||
public AxiomCreateWorldPropertiesEvent(World world, ServerWorldPropertiesRegistry registry) {
|
||||
this.world = world;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public World getWorld() {
|
||||
return world;
|
||||
}
|
||||
|
||||
public void addCategory(WorldPropertyCategory category, List<ServerWorldPropertyBase<?>> properties) {
|
||||
this.registry.addCategory(category, properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancel) {
|
||||
this.cancelled = cancel;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
}
|
@ -12,10 +12,11 @@ public class AxiomHandshakeEvent extends Event implements Cancellable {
|
||||
|
||||
private final Player player;
|
||||
private boolean cancelled = false;
|
||||
private int maxBufferSize = Short.MAX_VALUE;
|
||||
private int maxBufferSize;
|
||||
|
||||
public AxiomHandshakeEvent(Player player) {
|
||||
public AxiomHandshakeEvent(Player player, int maxBufferSize) {
|
||||
this.player = player;
|
||||
this.maxBufferSize = maxBufferSize;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
|
56
src/main/java/com/moulberry/axiom/event/AxiomTimeChangeEvent.java
Normale Datei
56
src/main/java/com/moulberry/axiom/event/AxiomTimeChangeEvent.java
Normale Datei
@ -0,0 +1,56 @@
|
||||
package com.moulberry.axiom.event;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class AxiomTimeChangeEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
|
||||
private final Player player;
|
||||
private final @Nullable Integer time;
|
||||
private final @Nullable Boolean freezeTime;
|
||||
private boolean cancelled = false;
|
||||
|
||||
public AxiomTimeChangeEvent(Player player, @Nullable Integer time, @Nullable Boolean freezeTime) {
|
||||
this.player = player;
|
||||
this.time = time;
|
||||
this.freezeTime = freezeTime;
|
||||
}
|
||||
|
||||
public @Nullable Integer getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public @Nullable Boolean isFreezeTime() {
|
||||
return freezeTime;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancel) {
|
||||
this.cancelled = cancel;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,80 @@
|
||||
package com.moulberry.axiom.event;
|
||||
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AxiomUnknownTeleportEvent extends Event implements Cancellable {
|
||||
|
||||
private static final HandlerList HANDLERS = new HandlerList();
|
||||
|
||||
private final Player player;
|
||||
private final NamespacedKey world;
|
||||
private final double x;
|
||||
private final double y;
|
||||
private final double z;
|
||||
private final float yaw;
|
||||
private final float pitch;
|
||||
private boolean cancelled = false;
|
||||
|
||||
public AxiomUnknownTeleportEvent(Player player, NamespacedKey world, double x, double y, double z, float yaw, float pitch) {
|
||||
this.player = player;
|
||||
this.world = world;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.yaw = yaw;
|
||||
this.pitch = pitch;
|
||||
}
|
||||
|
||||
public Player getPlayer() {
|
||||
return this.player;
|
||||
}
|
||||
|
||||
public NamespacedKey getWorld() {
|
||||
return this.world;
|
||||
}
|
||||
|
||||
public double getX() {
|
||||
return this.x;
|
||||
}
|
||||
|
||||
public double getY() {
|
||||
return this.y;
|
||||
}
|
||||
|
||||
public double getZ() {
|
||||
return this.z;
|
||||
}
|
||||
|
||||
public float getYaw() {
|
||||
return this.yaw;
|
||||
}
|
||||
|
||||
public float getPitch() {
|
||||
return this.pitch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCancelled(boolean cancel) {
|
||||
this.cancelled = cancel;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull HandlerList getHandlers() {
|
||||
return HANDLERS;
|
||||
}
|
||||
|
||||
}
|
67
src/main/java/com/moulberry/axiom/integration/Box.java
Normale Datei
67
src/main/java/com/moulberry/axiom/integration/Box.java
Normale Datei
@ -0,0 +1,67 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public record Box(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) {
|
||||
|
||||
@Nullable
|
||||
public Box tryCombine(Box other) {
|
||||
if (this.completelyOverlaps(other)) {
|
||||
return this;
|
||||
}
|
||||
|
||||
if (other.completelyOverlaps(this)) {
|
||||
return other;
|
||||
}
|
||||
|
||||
if (other.minX == this.minX && other.maxX == this.maxX) {
|
||||
if (other.minY == this.minY && other.maxY == this.maxY) {
|
||||
if (areLineSegmentsContinuous(other.minZ, other.maxZ, this.minZ, this.maxZ)) {
|
||||
return new Box(
|
||||
other.minX, other.minY, Math.min(other.minZ, this.minZ),
|
||||
other.maxX, other.maxY, Math.max(other.maxZ, this.maxZ)
|
||||
);
|
||||
}
|
||||
} else if (other.minZ == this.minZ && other.maxZ == this.maxZ) {
|
||||
if (areLineSegmentsContinuous(other.minY, other.maxY, this.minY, this.maxY)) {
|
||||
return new Box(
|
||||
other.minX, Math.min(other.minY, this.minY), other.minZ,
|
||||
other.maxX, Math.max(other.maxY, this.maxY), other.maxZ
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if (other.minY == this.minY && other.maxY == this.maxY &&
|
||||
other.minZ == this.minZ && other.maxZ == this.maxZ) {
|
||||
if (areLineSegmentsContinuous(other.minX, other.maxX, this.minX, this.maxX)) {
|
||||
return new Box(
|
||||
Math.min(other.minX, this.minX), other.minY, other.minZ,
|
||||
Math.max(other.maxX, this.maxX), other.maxY, other.maxZ
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return null; // Not able to combine
|
||||
}
|
||||
|
||||
public boolean completelyOverlaps(Box other) {
|
||||
return this.minX() <= other.minX() && this.minY() <= other.minY() && this.minZ() <= other.minZ() &&
|
||||
this.maxX() >= other.maxX() && this.maxY() >= other.maxY() && this.maxZ() >= other.maxZ();
|
||||
}
|
||||
|
||||
public boolean contains(int x, int y, int z) {
|
||||
return this.minX() <= x && this.minY() <= y && this.minZ() <= z &&
|
||||
this.maxX() >= x && this.maxY() >= y && this.maxZ() >= z;
|
||||
}
|
||||
|
||||
private static boolean areLineSegmentsContinuous(int min1, int max1, int min2, int max2) {
|
||||
int size1 = max1 - min1 + 1;
|
||||
int size2 = max2 - min2 + 1;
|
||||
|
||||
float mid1 = (min1 + max1);
|
||||
float mid2 = (min2 + max2);
|
||||
|
||||
float midDiff = Math.abs(mid1 - mid2);
|
||||
return midDiff <= size1 + size2;
|
||||
}
|
||||
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import com.moulberry.axiom.buffer.PositionConsumer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.core.DefaultedRegistry;
|
||||
import net.minecraft.core.IdMapper;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.PalettedContainer;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
public class NoVersionTranslator implements VersionTranslator {
|
||||
|
||||
private static IdMapper<BlockState> BLOCK_STATE_REGISTRY = Reflection.getField(Block.class, IdMapper.class, BlockState.class).get(null);
|
||||
public static BlockState idToState(int state) {
|
||||
return BLOCK_STATE_REGISTRY.byId(state);
|
||||
}
|
||||
|
||||
private static final BlockState EMPTY_STATE =
|
||||
Reflection.getTypedMethod(Block.class, BlockState.class).invoke((Block) // Block.defaultBlockState()
|
||||
Reflection.getField(BuiltInRegistries.class, DefaultedRegistry.class, Block.class).get(null) // BuiltInRegistries.BLOCK
|
||||
.get(new ResourceLocation("structure_void"))
|
||||
);
|
||||
public static void iterOverSection(PosBlockState container, PositionConsumer<BlockState> consumer) {
|
||||
for (int y = 0; y < 16; y++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
for (int x = 0; x < 16; x++) {
|
||||
BlockState state = container.at(x, y, z);
|
||||
if (state == EMPTY_STATE) continue;
|
||||
|
||||
consumer.accept(x, y, z, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntFunction<BlockState> blockStateMapper(Player player) {
|
||||
return NoVersionTranslator::idToState;
|
||||
}
|
||||
|
||||
private static final PalettedContainer.Strategy SECTION_STATES = Reflection.getField(PalettedContainer.Strategy.class, PalettedContainer.Strategy.class).get(null);
|
||||
private static final Reflection.Method<PalettedContainer, Void> read = Reflection.getMethod(PalettedContainer.class, FriendlyByteBuf.class);
|
||||
@Override
|
||||
public void readPalettedContainer(Player player, ByteBuf buf, PositionConsumer<BlockState> consumer) {
|
||||
//EMPTY_STATE is completely ignored
|
||||
PalettedContainer<BlockState> container = new PalettedContainer<>(BLOCK_STATE_REGISTRY, EMPTY_STATE, SECTION_STATES);
|
||||
read.invoke(container, buf);
|
||||
iterOverSection(container::get, consumer);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PosBlockState {
|
||||
BlockState at(int sx, int sy, int sz);
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import io.papermc.paper.event.player.PlayerFailMoveEvent;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
|
||||
public class PaperFailMoveListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onFailMove(PlayerFailMoveEvent event) {
|
||||
if (event.getPlayer().hasPermission(AxiomConstants.PERMISSION) &&
|
||||
event.getFailReason() == PlayerFailMoveEvent.FailReason.MOVED_TOO_QUICKLY) {
|
||||
event.setAllowed(true);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
public interface RegionProtection {
|
||||
|
||||
BiFunction<Player, World, RegionProtection> getProtection = Bukkit.getPluginManager().isPluginEnabled("WorldGuard") ? WorldGuardProtection::create : (player, world) -> new Dummy();
|
||||
|
||||
boolean canBuildInSection(int cx, int cy, int cz);
|
||||
|
||||
class Dummy implements RegionProtection {
|
||||
|
||||
@Override
|
||||
public boolean canBuildInSection(int cx, int cy, int cz) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SectionPermissionChecker {
|
||||
|
||||
boolean allAllowed();
|
||||
boolean noneAllowed();
|
||||
boolean allowed(int x, int y, int z);
|
||||
Box bounds();
|
||||
|
||||
static SectionPermissionChecker fromAllowedBoxes(List<Box> allowed) {
|
||||
if (allowed.isEmpty()) return NONE_ALLOWED;
|
||||
|
||||
if (allowed.size() == 1) {
|
||||
Box allowedBox = allowed.get(0);
|
||||
if (allowedBox.completelyOverlaps(FULL_BOUNDS)) {
|
||||
return ALL_ALLOWED;
|
||||
} else {
|
||||
return new AllAllowedInBox(allowedBox);
|
||||
}
|
||||
}
|
||||
|
||||
int minBoundsX = 15;
|
||||
int minBoundsY = 15;
|
||||
int minBoundsZ = 15;
|
||||
int maxBoundsX = 0;
|
||||
int maxBoundsY = 0;
|
||||
int maxBoundsZ = 0;
|
||||
|
||||
for (Box box : allowed) {
|
||||
minBoundsX = Math.min(box.minX(), minBoundsX);
|
||||
minBoundsY = Math.min(box.minY(), minBoundsY);
|
||||
minBoundsZ = Math.min(box.minZ(), minBoundsZ);
|
||||
maxBoundsX = Math.max(box.maxX(), maxBoundsX);
|
||||
maxBoundsY = Math.max(box.maxY(), maxBoundsY);
|
||||
maxBoundsZ = Math.max(box.maxZ(), maxBoundsZ);
|
||||
}
|
||||
|
||||
return new AllAllowedBoxes(new Box(minBoundsX, minBoundsY, minBoundsZ, maxBoundsX, maxBoundsY, maxBoundsZ), allowed);
|
||||
}
|
||||
|
||||
record AllAllowedInBox(Box box) implements SectionPermissionChecker {
|
||||
@Override
|
||||
public boolean allAllowed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean noneAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowed(int x, int y, int z) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Box bounds() {
|
||||
return box;
|
||||
}
|
||||
}
|
||||
|
||||
record AllAllowedBoxes(Box bounds, List<Box> allowed) implements SectionPermissionChecker {
|
||||
@Override
|
||||
public boolean allAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean noneAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowed(int x, int y, int z) {
|
||||
for (Box box : this.allowed) {
|
||||
if (box.contains(x, y, z)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Box bounds() {
|
||||
return this.bounds;
|
||||
}
|
||||
}
|
||||
|
||||
Box FULL_BOUNDS = new Box(0, 0, 0, 15, 15, 15);
|
||||
SectionPermissionChecker ALL_ALLOWED = new SectionPermissionChecker() {
|
||||
@Override
|
||||
public boolean allAllowed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean noneAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowed(int x, int y, int z) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Box bounds() {
|
||||
return FULL_BOUNDS;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Box EMPTY_BOUNDS = new Box(0, 0, 0, 0, 0, 0);
|
||||
SectionPermissionChecker NONE_ALLOWED = new SectionPermissionChecker() {
|
||||
@Override
|
||||
public boolean allAllowed() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean noneAllowed() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean allowed(int x, int y, int z) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Box bounds() {
|
||||
return EMPTY_BOUNDS;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import com.moulberry.axiom.buffer.PositionConsumer;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
public interface VersionTranslator {
|
||||
|
||||
VersionTranslator impl = Bukkit.getPluginManager().isPluginEnabled("ViaVersion") ? new ViaVersionTranslator() : new NoVersionTranslator();
|
||||
|
||||
IntFunction<BlockState> blockStateMapper(Player player);
|
||||
|
||||
void readPalettedContainer(Player player, ByteBuf buf, PositionConsumer<BlockState> consumer);
|
||||
|
||||
}
|
@ -1,72 +0,0 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import com.moulberry.axiom.buffer.PositionConsumer;
|
||||
import com.viaversion.viaversion.api.Via;
|
||||
import com.viaversion.viaversion.api.data.MappingData;
|
||||
import com.viaversion.viaversion.api.minecraft.chunks.DataPalette;
|
||||
import com.viaversion.viaversion.api.minecraft.chunks.PaletteType;
|
||||
import com.viaversion.viaversion.api.protocol.ProtocolManager;
|
||||
import com.viaversion.viaversion.api.protocol.ProtocolPathEntry;
|
||||
import com.viaversion.viaversion.api.protocol.version.ServerProtocolVersion;
|
||||
import com.viaversion.viaversion.api.type.types.version.PaletteType1_18;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
public class ViaVersionTranslator implements VersionTranslator {
|
||||
|
||||
private final ProtocolManager protocolManager;
|
||||
private final ServerProtocolVersion serverVersion;
|
||||
|
||||
public ViaVersionTranslator() {
|
||||
protocolManager = Via.getManager().getProtocolManager();
|
||||
serverVersion = Via.getAPI().getServerVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntFunction<BlockState> blockStateMapper(Player player) {
|
||||
List<ProtocolPathEntry> path = protocolManager.getProtocolPath(Via.getAPI().getPlayerVersion(player.getUniqueId()), serverVersion.highestSupportedVersion());
|
||||
if(path == null)
|
||||
return NoVersionTranslator::idToState;
|
||||
|
||||
List<IntFunction<Integer>> mappers = new ArrayList<>(path.size());
|
||||
for(ProtocolPathEntry entry : path) {
|
||||
MappingData mappings = entry.protocol().getMappingData();
|
||||
if(mappings != null)
|
||||
mappers.add(mappings.getBlockStateMappings().inverse()::getNewId);
|
||||
}
|
||||
|
||||
return id -> {
|
||||
for(IntFunction<Integer> transformer : mappers)
|
||||
id = transformer.apply(id);
|
||||
return NoVersionTranslator.idToState(id);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readPalettedContainer(Player player, ByteBuf buf, PositionConsumer<BlockState> consumer) {
|
||||
//TODO GlobalPaletteBits depend on player version (currently only 1.20.1, depending on Axiom version)
|
||||
DataPalette container;
|
||||
try {
|
||||
container = new PaletteType1_18(PaletteType.BLOCKS, 15).read(buf);
|
||||
} catch (Exception e) {
|
||||
throw new IllegalArgumentException(e);
|
||||
}
|
||||
|
||||
BlockState[] palette = new BlockState[container.size()];
|
||||
IntFunction<BlockState> mapper = blockStateMapper(player);
|
||||
for(int i = 0; i < container.size(); i++) {
|
||||
palette[i] = mapper.apply(container.idByIndex(i));
|
||||
}
|
||||
|
||||
NoVersionTranslator.iterOverSection(
|
||||
(sx, sy, sz) -> palette[container.paletteIndexAt(container.index(sx, sy, sz))],
|
||||
consumer
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package com.moulberry.axiom.integration;
|
||||
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldguard.LocalPlayer;
|
||||
import com.sk89q.worldguard.WorldGuard;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.internal.platform.WorldGuardPlatform;
|
||||
import com.sk89q.worldguard.protection.ApplicableRegionSet;
|
||||
import com.sk89q.worldguard.protection.flags.Flags;
|
||||
import com.sk89q.worldguard.protection.managers.RegionManager;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
import com.sk89q.worldguard.protection.regions.RegionContainer;
|
||||
import com.sk89q.worldguard.protection.regions.RegionQuery;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class WorldGuardProtection implements RegionProtection {
|
||||
|
||||
private static final WorldGuardPlatform platform = WorldGuard.getInstance().getPlatform();
|
||||
private static final RegionContainer regionContainer = platform.getRegionContainer();
|
||||
|
||||
private final LocalPlayer player;
|
||||
private final RegionManager regionManager;
|
||||
|
||||
public WorldGuardProtection(LocalPlayer player, RegionManager regionManager) {
|
||||
this.player = player;
|
||||
this.regionManager = regionManager;
|
||||
}
|
||||
|
||||
public static RegionProtection create(Player player, World world) {
|
||||
com.sk89q.worldedit.world.World worldGuardWorld = BukkitAdapter.adapt(world);
|
||||
LocalPlayer worldGuardPlayer = WorldGuardPlugin.inst().wrapPlayer(player);
|
||||
|
||||
// Don't do any protection if player has bypass
|
||||
if (platform.getSessionManager().hasBypass(worldGuardPlayer, worldGuardWorld)) {
|
||||
return new RegionProtection.Dummy();
|
||||
}
|
||||
|
||||
RegionManager regionManager = regionContainer.get(worldGuardWorld);
|
||||
if (regionManager == null) return new RegionProtection.Dummy();
|
||||
|
||||
return new WorldGuardProtection(worldGuardPlayer, regionManager);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canBuildInSection(int cx, int cy, int cz) {
|
||||
BlockVector3 min = BlockVector3.at(cx*16, cy*16, cz*16);
|
||||
BlockVector3 max = BlockVector3.at(cx*16+15, cy*16+15, cz*16+15);
|
||||
ProtectedRegion test = new ProtectedCuboidRegion("dummy", min, max);
|
||||
ApplicableRegionSet regions = this.regionManager.getApplicableRegions(test, RegionQuery.QueryOption.COMPUTE_PARENTS);
|
||||
return regions.testState(this.player, Flags.BUILD);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.moulberry.axiom.integration.plotsquared;
|
||||
|
||||
|
||||
import com.moulberry.axiom.integration.SectionPermissionChecker;
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
||||
public class PlotSquaredIntegration {
|
||||
|
||||
public record PlotBounds(BlockPos min, BlockPos max, String worldName) {
|
||||
public PlotBounds(CuboidRegion cuboidRegion, String worldName) {
|
||||
this(
|
||||
new BlockPos(
|
||||
cuboidRegion.getMinimumPoint().getBlockX(),
|
||||
cuboidRegion.getMinimumPoint().getBlockY(),
|
||||
cuboidRegion.getMinimumPoint().getBlockZ()
|
||||
),
|
||||
new BlockPos(
|
||||
cuboidRegion.getMaximumPoint().getBlockX(),
|
||||
cuboidRegion.getMaximumPoint().getBlockY(),
|
||||
cuboidRegion.getMaximumPoint().getBlockZ()
|
||||
),
|
||||
worldName
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean canBreakBlock(Player player, Block block) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("PlotSquared")) {
|
||||
return true;
|
||||
}
|
||||
return PlotSquaredIntegrationImpl.canBreakBlock(player, block);
|
||||
}
|
||||
|
||||
public static boolean canPlaceBlock(Player player, org.bukkit.Location loc) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("PlotSquared")) {
|
||||
return true;
|
||||
}
|
||||
return PlotSquaredIntegrationImpl.canPlaceBlock(player, loc);
|
||||
}
|
||||
|
||||
public static boolean isPlotWorld(World world) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("PlotSquared")) {
|
||||
return false;
|
||||
}
|
||||
return PlotSquaredIntegrationImpl.isPlotWorld(world);
|
||||
}
|
||||
|
||||
public static PlotSquaredIntegration.PlotBounds getCurrentEditablePlot(Player player) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("PlotSquared")) {
|
||||
return null;
|
||||
}
|
||||
return PlotSquaredIntegrationImpl.getCurrentEditablePlot(player);
|
||||
}
|
||||
|
||||
public static SectionPermissionChecker checkSection(Player player, World world, int sectionX, int sectionY, int sectionZ) {
|
||||
if (!Bukkit.getPluginManager().isPluginEnabled("PlotSquared")) {
|
||||
return SectionPermissionChecker.ALL_ALLOWED;
|
||||
}
|
||||
return PlotSquaredIntegrationImpl.checkSection(player, world, sectionX, sectionY, sectionZ);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,268 @@
|
||||
package com.moulberry.axiom.integration.plotsquared;
|
||||
|
||||
import com.moulberry.axiom.integration.Box;
|
||||
import com.moulberry.axiom.integration.SectionPermissionChecker;
|
||||
import com.plotsquared.bukkit.player.BukkitPlayer;
|
||||
import com.plotsquared.bukkit.util.BukkitUtil;
|
||||
import com.plotsquared.core.PlotSquared;
|
||||
import com.plotsquared.core.configuration.Settings;
|
||||
import com.plotsquared.core.location.Location;
|
||||
import com.plotsquared.core.permissions.Permission;
|
||||
import com.plotsquared.core.plot.Plot;
|
||||
import com.plotsquared.core.plot.PlotArea;
|
||||
import com.plotsquared.core.plot.PlotId;
|
||||
import com.plotsquared.core.plot.flag.implementations.BreakFlag;
|
||||
import com.plotsquared.core.plot.flag.implementations.DoneFlag;
|
||||
import com.plotsquared.core.plot.flag.types.BlockTypeWrapper;
|
||||
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
||||
import com.sk89q.worldedit.math.BlockVector3;
|
||||
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||
import com.sk89q.worldedit.world.block.BlockType;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/*
|
||||
* PlotSquared, a land and world management plugin for Minecraft.
|
||||
* Copyright (C) IntellectualSites <https://intellectualsites.com>
|
||||
* Copyright (C) IntellectualSites team and contributors
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
public class PlotSquaredIntegrationImpl {
|
||||
|
||||
static boolean canBreakBlock(Player player, Block block) {
|
||||
Location location = BukkitUtil.adapt(block.getLocation());
|
||||
PlotArea area = location.getPlotArea();
|
||||
if (area == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
Plot plot = area.getPlot(location);
|
||||
if (plot != null) {
|
||||
BukkitPlayer plotPlayer = BukkitUtil.adapt(player);
|
||||
// == rather than <= as we only care about the "ground level" not being destroyed
|
||||
if (block.getY() == area.getMinGenHeight()) {
|
||||
if (!plotPlayer.hasPermission(Permission.PERMISSION_ADMIN_DESTROY_GROUNDLEVEL, true)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (area.notifyIfOutsideBuildArea(plotPlayer, location.getY())) {
|
||||
return false;
|
||||
}
|
||||
// check unowned plots
|
||||
if (!plot.hasOwner()) {
|
||||
return plotPlayer.hasPermission(Permission.PERMISSION_ADMIN_DESTROY_UNOWNED, true);
|
||||
}
|
||||
// player is breaking another player's plot
|
||||
if (!plot.isAdded(plotPlayer.getUUID())) {
|
||||
List<BlockTypeWrapper> destroy = plot.getFlag(BreakFlag.class);
|
||||
final BlockType blockType = BukkitAdapter.asBlockType(block.getType());
|
||||
for (final BlockTypeWrapper blockTypeWrapper : destroy) {
|
||||
if (blockTypeWrapper.accepts(blockType)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return plotPlayer.hasPermission(Permission.PERMISSION_ADMIN_DESTROY_OTHER, true);
|
||||
}
|
||||
// plot is 'done'
|
||||
if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) {
|
||||
return plotPlayer.hasPermission(Permission.PERMISSION_ADMIN_BUILD_OTHER, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
BukkitPlayer pp = BukkitUtil.adapt(player);
|
||||
return pp.hasPermission(Permission.PERMISSION_ADMIN_DESTROY_ROAD, true);
|
||||
}
|
||||
|
||||
static boolean canPlaceBlock(Player player, org.bukkit.Location loc) {
|
||||
Location location = BukkitUtil.adapt(loc);
|
||||
PlotArea area = location.getPlotArea();
|
||||
if (area == null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
BukkitPlayer pp = BukkitUtil.adapt(player);
|
||||
Plot plot = area.getPlot(location);
|
||||
if (plot != null) {
|
||||
if (area.notifyIfOutsideBuildArea(pp, location.getY())) {
|
||||
return false;
|
||||
}
|
||||
// check unowned plots
|
||||
if (!plot.hasOwner()) {
|
||||
return pp.hasPermission(Permission.PERMISSION_ADMIN_BUILD_UNOWNED, true);
|
||||
}
|
||||
// player is breaking another player's plot
|
||||
if (!plot.isAdded(pp.getUUID())) {
|
||||
return pp.hasPermission(Permission.PERMISSION_ADMIN_BUILD_OTHER, true);
|
||||
}
|
||||
// plot is 'done'
|
||||
if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) {
|
||||
return pp.hasPermission(Permission.PERMISSION_ADMIN_BUILD_OTHER, true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return pp.hasPermission(Permission.PERMISSION_ADMIN_BUILD_ROAD, true);
|
||||
}
|
||||
|
||||
private static final WeakHashMap<World, Boolean> plotWorldCache = new WeakHashMap<>();
|
||||
|
||||
static boolean isPlotWorld(World world) {
|
||||
if (plotWorldCache.containsKey(world)) {
|
||||
return plotWorldCache.get(world);
|
||||
}
|
||||
|
||||
String worldName = world.getName();
|
||||
PlotArea[] plotAreas = PlotSquared.get().getPlotAreaManager().getPlotAreas(worldName, null);
|
||||
boolean isPlotWorld = plotAreas.length > 0;
|
||||
plotWorldCache.put(world, isPlotWorld);
|
||||
return isPlotWorld;
|
||||
}
|
||||
|
||||
static PlotSquaredIntegration.PlotBounds getCurrentEditablePlot(Player player) {
|
||||
org.bukkit.Location loc = player.getLocation();
|
||||
|
||||
Location location = BukkitUtil.adapt(loc);
|
||||
PlotArea area = location.getPlotArea();
|
||||
if (area == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
BukkitPlayer pp = BukkitUtil.adapt(player);
|
||||
Plot plot = area.getPlot(location);
|
||||
if (plot != null) {
|
||||
Location bottom = plot.getExtendedBottomAbs();
|
||||
Location top = plot.getExtendedTopAbs();
|
||||
CuboidRegion cuboidRegion = new CuboidRegion(bottom.getBlockVector3(), top.getBlockVector3());
|
||||
|
||||
// check unowned plots
|
||||
if (!plot.hasOwner()) {
|
||||
if (!pp.hasPermission(Permission.PERMISSION_ADMIN_BUILD_UNOWNED, false)) {
|
||||
return null;
|
||||
} else {
|
||||
return new PlotSquaredIntegration.PlotBounds(cuboidRegion, player.getWorld().getName());
|
||||
}
|
||||
}
|
||||
// player is breaking another player's plot
|
||||
if (!plot.isAdded(pp.getUUID())) {
|
||||
if (!pp.hasPermission(Permission.PERMISSION_ADMIN_BUILD_OTHER, false)) {
|
||||
return null;
|
||||
} else {
|
||||
return new PlotSquaredIntegration.PlotBounds(cuboidRegion, player.getWorld().getName());
|
||||
}
|
||||
}
|
||||
// plot is 'done'
|
||||
if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) {
|
||||
if (!pp.hasPermission(Permission.PERMISSION_ADMIN_BUILD_OTHER, false)) {
|
||||
return null;
|
||||
} else {
|
||||
return new PlotSquaredIntegration.PlotBounds(cuboidRegion, player.getWorld().getName());
|
||||
}
|
||||
}
|
||||
return new PlotSquaredIntegration.PlotBounds(cuboidRegion, player.getWorld().getName());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static SectionPermissionChecker checkSection(Player player, World world, int sectionX, int sectionY, int sectionZ) {
|
||||
int minX = sectionX * 16;
|
||||
int minY = sectionY * 16;
|
||||
int minZ = sectionZ * 16;
|
||||
int maxX = sectionX * 16 + 15;
|
||||
int maxY = sectionY * 16 + 15;
|
||||
int maxZ = sectionZ * 16 + 15;
|
||||
|
||||
PlotArea[] plotAreas = PlotSquared.get().getPlotAreaManager().getPlotAreas(world.getName(), new CuboidRegion(
|
||||
BlockVector3.at(minX, minY, minZ),
|
||||
BlockVector3.at(maxX, maxY, maxZ)
|
||||
));
|
||||
|
||||
if (plotAreas.length == 0) {
|
||||
return SectionPermissionChecker.ALL_ALLOWED;
|
||||
}
|
||||
|
||||
Set<Plot> checkedPlots = new HashSet<>();
|
||||
List<Box> allowed = new ArrayList<>();
|
||||
|
||||
for (PlotArea plotArea : plotAreas) {
|
||||
for (int px = minX; px <= maxX; px += 15) {
|
||||
for (int py = minY; py <= maxY; py += 15) {
|
||||
for (int pz = minZ; pz <= maxZ; pz += 15) {
|
||||
PlotId pid = plotArea.getPlotManager().getPlotId(px, py, pz);
|
||||
if (pid == null) continue;
|
||||
Plot plot = plotArea.getOwnedPlot(pid);
|
||||
if (plot == null) continue;
|
||||
|
||||
if (!checkedPlots.add(plot)) continue;
|
||||
|
||||
if (!plot.hasOwner()) continue;
|
||||
if (!plot.isAdded(player.getUniqueId())) continue;
|
||||
if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) continue;
|
||||
|
||||
for (CuboidRegion region : plot.getRegions()) {
|
||||
BlockVector3 minPoint = region.getMinimumPoint();
|
||||
BlockVector3 maxPoint = region.getMaximumPoint();
|
||||
|
||||
int minPlotX = Math.max(minPoint.getX(), minX);
|
||||
int minPlotY = Math.max(minPoint.getY(), minY);
|
||||
int minPlotZ = Math.max(minPoint.getZ(), minZ);
|
||||
int maxPlotX = Math.min(maxPoint.getX(), maxX);
|
||||
int maxPlotY = Math.min(maxPoint.getY(), maxY);
|
||||
int maxPlotZ = Math.min(maxPoint.getZ(), maxZ);
|
||||
|
||||
if (minPlotX > maxPlotX) continue;
|
||||
if (minPlotY > maxPlotY) continue;
|
||||
if (minPlotZ > maxPlotZ) continue;
|
||||
|
||||
if (minPlotX <= minX && minPlotY <= minY && minPlotZ <= minZ &&
|
||||
maxPlotX >= maxX && maxPlotY >= maxY && maxPlotZ >= maxZ) {
|
||||
return SectionPermissionChecker.ALL_ALLOWED;
|
||||
}
|
||||
|
||||
allowed.add(new Box(minPlotX - minX, minPlotY - minY, minPlotZ - minZ,
|
||||
maxPlotX - minX, maxPlotY - minY, maxPlotZ - minZ));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Combine
|
||||
main:
|
||||
while (allowed.size() >= 2) {
|
||||
for (int i = 0; i < allowed.size() - 1; i++) {
|
||||
Box first = allowed.get(i);
|
||||
for (int j = i + 1; j < allowed.size(); j++) {
|
||||
Box second = allowed.get(j);
|
||||
|
||||
Box combined = first.tryCombine(second);
|
||||
if (combined != null) {
|
||||
allowed.remove(j);
|
||||
allowed.remove(i);
|
||||
allowed.add(combined);
|
||||
continue main;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return SectionPermissionChecker.fromAllowedBoxes(allowed);
|
||||
}
|
||||
}
|
102
src/main/java/com/moulberry/axiom/marker/MarkerData.java
Normale Datei
102
src/main/java/com/moulberry/axiom/marker/MarkerData.java
Normale Datei
@ -0,0 +1,102 @@
|
||||
package com.moulberry.axiom.marker;
|
||||
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.entity.Marker;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import xyz.jpenilla.reflectionremapper.ReflectionRemapper;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.UUID;
|
||||
|
||||
public record MarkerData(UUID uuid, Vec3 position, @Nullable String name, @Nullable Vec3 minRegion, @Nullable Vec3 maxRegion) {
|
||||
public static MarkerData read(FriendlyByteBuf friendlyByteBuf) {
|
||||
UUID uuid = friendlyByteBuf.readUUID();
|
||||
Vec3 position = new Vec3(friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble());
|
||||
String name = friendlyByteBuf.readNullable(FriendlyByteBuf::readUtf);
|
||||
|
||||
Vec3 minRegion = null;
|
||||
Vec3 maxRegion = null;
|
||||
if (friendlyByteBuf.readBoolean()) {
|
||||
minRegion = new Vec3(friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble());
|
||||
maxRegion = new Vec3(friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble());
|
||||
}
|
||||
|
||||
return new MarkerData(uuid, position, name, minRegion, maxRegion);
|
||||
}
|
||||
|
||||
public static void write(FriendlyByteBuf friendlyByteBuf, MarkerData markerData) {
|
||||
friendlyByteBuf.writeUUID(markerData.uuid);
|
||||
friendlyByteBuf.writeDouble(markerData.position.x);
|
||||
friendlyByteBuf.writeDouble(markerData.position.y);
|
||||
friendlyByteBuf.writeDouble(markerData.position.z);
|
||||
friendlyByteBuf.writeNullable(markerData.name, FriendlyByteBuf::writeUtf);
|
||||
|
||||
if (markerData.minRegion != null && markerData.maxRegion != null) {
|
||||
friendlyByteBuf.writeBoolean(true);
|
||||
friendlyByteBuf.writeDouble(markerData.minRegion.x);
|
||||
friendlyByteBuf.writeDouble(markerData.minRegion.y);
|
||||
friendlyByteBuf.writeDouble(markerData.minRegion.z);
|
||||
friendlyByteBuf.writeDouble(markerData.maxRegion.x);
|
||||
friendlyByteBuf.writeDouble(markerData.maxRegion.y);
|
||||
friendlyByteBuf.writeDouble(markerData.maxRegion.z);
|
||||
} else {
|
||||
friendlyByteBuf.writeBoolean(false);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Field dataField;
|
||||
static {
|
||||
ReflectionRemapper reflectionRemapper = ReflectionRemapper.forReobfMappingsInPaperJar();
|
||||
String fieldName = reflectionRemapper.remapFieldName(Marker.class, "data");
|
||||
|
||||
try {
|
||||
dataField = Marker.class.getDeclaredField(fieldName);
|
||||
dataField.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static CompoundTag getData(Marker marker) {
|
||||
try {
|
||||
return (CompoundTag) dataField.get(marker);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static MarkerData createFrom(Marker marker) {
|
||||
Vec3 position = marker.position();
|
||||
CompoundTag data = getData(marker);
|
||||
|
||||
String name = data.getString("name").trim();
|
||||
if (name.isEmpty()) name = null;
|
||||
|
||||
Vec3 minRegion = null;
|
||||
Vec3 maxRegion = null;
|
||||
if (data.contains("min", Tag.TAG_LIST) && data.contains("max", Tag.TAG_LIST)) {
|
||||
ListTag min = data.getList("min", Tag.TAG_DOUBLE);
|
||||
ListTag max = data.getList("max", Tag.TAG_DOUBLE);
|
||||
|
||||
if (min.size() == 3 && max.size() == 3) {
|
||||
double minX = min.getDouble(0);
|
||||
double minY = min.getDouble(1);
|
||||
double minZ = min.getDouble(2);
|
||||
double maxX = max.getDouble(0);
|
||||
double maxY = max.getDouble(1);
|
||||
double maxZ = max.getDouble(2);
|
||||
minRegion = new Vec3(minX, minY, minZ);
|
||||
maxRegion = new Vec3(maxX, maxY, maxZ);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return new MarkerData(marker.getUUID(), position, name, minRegion, maxRegion);
|
||||
}
|
||||
}
|
@ -1,104 +1,82 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.handler.codec.ByteToMessageDecoder;
|
||||
import io.papermc.paper.network.ConnectionEvent;
|
||||
import net.minecraft.network.ConnectionProtocol;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.Packet;
|
||||
import net.minecraft.network.protocol.PacketFlow;
|
||||
import net.minecraft.network.protocol.game.ServerboundCustomPayloadPacket;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ChannelHandler.Sharable
|
||||
public class AxiomBigPayloadHandler extends ChannelInboundHandlerAdapter {
|
||||
public class AxiomBigPayloadHandler extends ByteToMessageDecoder {
|
||||
|
||||
private static final Reflection.Method<ConnectionProtocol, Integer> getPacketId = Reflection.getTypedMethod(ConnectionProtocol.class, Reflection.VERSION > 1902 ? int.class : Integer.class, PacketFlow.class, Packet.class);
|
||||
private static final int PLUGINMESSAGE_PACKETID = getPacketId.invoke(ConnectionProtocol.PLAY, PacketFlow.SERVERBOUND, new ServerboundCustomPayloadPacket(null, null));
|
||||
private static final ResourceLocation SET_BUFFER = new ResourceLocation("axiom", "set_buffer");
|
||||
private final int payloadId;
|
||||
private final Connection connection;
|
||||
private final SetBlockBufferPacketListener listener;
|
||||
|
||||
private final Player player;
|
||||
|
||||
private final List<QueuedPacket> packets = new ArrayList<>();
|
||||
private final BukkitTask task;
|
||||
|
||||
public AxiomBigPayloadHandler(Player player) {
|
||||
this.player = player;
|
||||
this.task = Bukkit.getScheduler().runTaskTimer(AxiomPaper.getPlugin(), this::runHandlers, 1, 1);
|
||||
public AxiomBigPayloadHandler(int payloadId, Connection connection, SetBlockBufferPacketListener listener) {
|
||||
this.payloadId = payloadId;
|
||||
this.connection = connection;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelRead(@NotNull ChannelHandlerContext ctx, @NotNull Object msg) {
|
||||
if(!(msg instanceof ByteBuf in)) {
|
||||
ctx.fireChannelRead(msg);
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
||||
// Don't process if channel isn't active
|
||||
if (!ctx.channel().isActive()) {
|
||||
in.skipBytes(in.readableBytes());
|
||||
return;
|
||||
}
|
||||
|
||||
int readerIndexBackup = in.readerIndex();
|
||||
int i = in.readableBytes();
|
||||
if (i != 0) {
|
||||
int readerIndex = in.readerIndex();
|
||||
boolean success = false;
|
||||
try {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(in);
|
||||
int packetId = buf.readVarInt();
|
||||
|
||||
if(in.readableBytes() != 0) {
|
||||
ByteBuf buf = new FriendlyByteBuf(in);
|
||||
if (MojBuf.readVarInt(buf) == PLUGINMESSAGE_PACKETID) {
|
||||
NamespacedKey id = MojBuf.readKey(buf);
|
||||
if (id.getNamespace().equals("axiom") && player.hasPermission(AxiomConstants.PERMISSION)) {
|
||||
synchronized (packets) {
|
||||
packets.add(new QueuedPacket(id.getKey(), buf));
|
||||
}
|
||||
if (packetId == payloadId) {
|
||||
ResourceLocation identifier = buf.readResourceLocation();
|
||||
if (identifier.equals(SET_BUFFER)) {
|
||||
ServerPlayer player = connection.getPlayer();
|
||||
if (player != null && player.getBukkitEntity().hasPermission("axiom.*")) {
|
||||
if (listener.onReceive(player, buf)) {
|
||||
success = true;
|
||||
in.skipBytes(in.readableBytes());
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
} finally {
|
||||
if (!success) {
|
||||
in.readerIndex(readerIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
in.readerIndex(readerIndexBackup);
|
||||
ctx.fireChannelRead(in);
|
||||
ctx.fireChannelRead(in.retain());
|
||||
|
||||
// Skip remaining bytes
|
||||
if (in.readableBytes() > 0) {
|
||||
in.skipBytes(in.readableBytes());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
|
||||
if (evt == ConnectionEvent.COMPRESSION_THRESHOLD_SET || evt == ConnectionEvent.COMPRESSION_DISABLED) {
|
||||
ctx.channel().pipeline().remove("axiom-big-payload-handler");
|
||||
ctx.channel().pipeline().addBefore("decoder", "axiom-big-payload-handler", this);
|
||||
ctx.channel().pipeline().addBefore("decoder", "axiom-big-payload-handler",
|
||||
new AxiomBigPayloadHandler(payloadId, connection, listener));
|
||||
}
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void channelInactive(@NotNull ChannelHandlerContext ctx) {
|
||||
if(!task.isCancelled())
|
||||
task.cancel();
|
||||
|
||||
ctx.fireChannelInactive();
|
||||
}
|
||||
|
||||
private void runHandlers() {
|
||||
List<QueuedPacket> queue;
|
||||
synchronized (packets) {
|
||||
if(packets.isEmpty())
|
||||
return;
|
||||
|
||||
queue = new ArrayList<>(packets);
|
||||
packets.clear();
|
||||
}
|
||||
|
||||
for(QueuedPacket packet : queue) {
|
||||
AxiomPacketListener handler = AxiomPaper.getListener(packet.name);
|
||||
if(handler != null)
|
||||
handler.onMessage(player, packet.in);
|
||||
packet.in.release();
|
||||
}
|
||||
}
|
||||
|
||||
private record QueuedPacket(String name, ByteBuf in) {}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface AxiomPacketListener {
|
||||
|
||||
void onMessage(Player player, ByteBuf buf);
|
||||
|
||||
default void onMissingPerm(Player player, ByteBuf buf) {}
|
||||
|
||||
}
|
@ -1,144 +0,0 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import com.moulberry.axiom.version.VersionWrapper;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.SectionPos;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerChunkCache;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.EntityBlock;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkAccess;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.minecraft.world.level.chunk.LevelChunkSection;
|
||||
import net.minecraft.world.level.levelgen.Heightmap;
|
||||
import net.minecraft.world.level.lighting.LightEventListener;
|
||||
import org.bukkit.World;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class ChunkSectionModifier {
|
||||
|
||||
private static final Reflection.Method<Heightmap.Types, String> getSerializationKey = Reflection.getTypedMethod(Heightmap.Types.class, String.class);
|
||||
private static final List<Heightmap.Types> types = Arrays.stream(Heightmap.Types.values()).filter(type -> List.of("WORLD_SURFACE", "OCEAN_FLOOR", "MOTION_BLOCKING", "MOTION_BLOCKING_NO_LEAVES").contains(getSerializationKey.invoke(type))).toList();
|
||||
|
||||
private final int cx;
|
||||
private final int cy;
|
||||
private final int cz;
|
||||
|
||||
private final ServerChunkCache chunkSource;
|
||||
private final LightEventListener lightEngine;
|
||||
private final LevelChunk chunk;
|
||||
private final LevelChunkSection section;
|
||||
private final Heightmap[] heightmaps;
|
||||
private final boolean hadOnlyAir;
|
||||
|
||||
private static final Reflection.Method<ChunkAccess, LevelChunkSection> getSection = Reflection.getTypedMethod(ChunkAccess.class, LevelChunkSection.class, int.class);
|
||||
private static final Reflection.Method<LevelChunkSection, Boolean> hasOnlyAir = Reflection.getTypedMethod(LevelChunkSection.class, boolean.class, 1);
|
||||
private static final Reflection.Field<ChunkAccess, Map> chunkHeightmaps = Reflection.getField(ChunkAccess.class, Map.class);
|
||||
public ChunkSectionModifier(World world, int cx, int cy, int cz) {
|
||||
this.cx = cx;
|
||||
this.cy = cy;
|
||||
this.cz = cz;
|
||||
|
||||
ServerLevel level = AxiomPaper.convert(world);
|
||||
chunkSource = AxiomPaper.getChunkSource(level);
|
||||
lightEngine = AxiomPaper.getLightEngine(chunkSource);
|
||||
chunk = AxiomPaper.getChunk(level, cx, cz);
|
||||
|
||||
section = getSection.invoke(chunk, cy - world.getMinHeight() / 16);
|
||||
hadOnlyAir = hasOnlyAir.invoke(section);
|
||||
|
||||
heightmaps = types.stream().map(chunkHeightmaps.get(chunk)::get).toArray(Heightmap[]::new);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<LevelChunkSection, BlockState> setBlockState = Reflection.getTypedMethod(LevelChunkSection.class, BlockState.class, int.class, int.class, int.class, BlockState.class, boolean.class);
|
||||
private static final Reflection.Method<Heightmap, Boolean> update = Reflection.getTypedMethod(Heightmap.class, boolean.class, int.class, int.class, int.class, BlockState.class);
|
||||
private static final Reflection.Method<BlockState, Block> getBlock = Reflection.getTypedMethod(BlockState.class, Block.class);
|
||||
private static final Reflection.Method<ChunkAccess, Void> removeBlockEntity = Reflection.getMethod(ChunkAccess.class, BlockPos.class);
|
||||
private static final Reflection.Method<ServerChunkCache, Void> blockChanged = Reflection.getMethod(ServerChunkCache.class, BlockPos.class);
|
||||
private static final Reflection.Method<LightEventListener, Void> checkBlock = Reflection.getMethod(LightEventListener.class, BlockPos.class);
|
||||
public void setState(int sx, int sy, int sz, BlockState state) {
|
||||
BlockState old = setBlockState.invoke(section, sx, sy, sz, state, false);
|
||||
if (state == old)
|
||||
return;
|
||||
|
||||
int by = cy * 16 + sy;
|
||||
BlockPos pos = new BlockPos(cx * 16 + sx, by, cz * 16 + sz);
|
||||
|
||||
for (Heightmap heightmap : heightmaps)
|
||||
update.invoke(heightmap, sx, by, sz, state);
|
||||
|
||||
if (getBlock.invoke(state) instanceof EntityBlock) {
|
||||
setBlockEntity(state, pos);
|
||||
} else if (getBlock.invoke(old) instanceof EntityBlock) {
|
||||
removeBlockEntity.invoke(chunk, pos);
|
||||
}
|
||||
|
||||
blockChanged.invoke(chunkSource, pos);
|
||||
|
||||
if (VersionWrapper.impl.hasDifferentLightProperties(chunk, pos, old, state)) {
|
||||
checkBlock.invoke(lightEngine, pos);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Reflection.Method<BlockGetter, BlockEntity> getBlockEntity = Reflection.getTypedMethod(BlockGetter.class, BlockEntity.class, BlockPos.class);
|
||||
private static final Reflection.Method<BlockEntity, Void> load = Reflection.getMethod(BlockEntity.class, CompoundTag.class);
|
||||
public void setBlockEntity(int sx, int sy, int sz, CompoundTag tag) {
|
||||
BlockEntity blockEntity = getBlockEntity.invoke(chunk, new BlockPos(cx * 16 + sx, cy * 16 + sy, cz * 16 + sz));
|
||||
|
||||
if(blockEntity != null)
|
||||
load.invoke(blockEntity, tag);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<BlockEntity, BlockEntityType> getType = Reflection.getTypedMethod(BlockEntity.class, BlockEntityType.class);
|
||||
private static final Reflection.Method<BlockEntityType, Boolean> isValid = Reflection.getTypedMethod(BlockEntityType.class, boolean.class, BlockState.class);
|
||||
private static final Reflection.Method<BlockEntity, Void> blockEntitySetBlockState = Reflection.getMethod(BlockEntity.class, BlockState.class);
|
||||
private static final Reflection.Method<LevelChunk, Void> updateBlockEntityTicker = Reflection.getMethod(LevelChunk.class, 2, BlockEntity.class);
|
||||
private static final Reflection.Method<EntityBlock, BlockEntity> newBlockEntity = Reflection.getTypedMethod(EntityBlock.class, BlockEntity.class, BlockPos.class, BlockState.class);
|
||||
private static final Reflection.Method<LevelChunk, Void> addAndRegisterBlockEntity = Reflection.getMethod(LevelChunk.class, BlockEntity.class);
|
||||
private void setBlockEntity(BlockState state, BlockPos pos) {
|
||||
BlockEntity blockEntity = getBlockEntity.invoke(chunk, pos);
|
||||
|
||||
if (blockEntity != null) {
|
||||
if (isValid.invoke(getType.invoke(blockEntity), state)) {
|
||||
// Block entity is here and the type is correct
|
||||
blockEntitySetBlockState.invoke(blockEntity, state);
|
||||
|
||||
updateBlockEntityTicker.invoke(chunk, blockEntity);
|
||||
} else {
|
||||
// Block entity type isn't correct, we need to recreate it
|
||||
removeBlockEntity.invoke(chunk, pos);
|
||||
blockEntity = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (blockEntity == null) {
|
||||
// There isn't a block entity here, create it!
|
||||
EntityBlock block = (EntityBlock)getBlock.invoke(state);
|
||||
blockEntity = newBlockEntity.invoke(block, pos, state);
|
||||
if (blockEntity != null) {
|
||||
addAndRegisterBlockEntity.invoke(chunk, blockEntity);
|
||||
}
|
||||
}
|
||||
}
|
||||
private static final Reflection.Method<SectionPos, SectionPos> of = Reflection.getTypedMethod(SectionPos.class, SectionPos.class, int.class, int.class, int.class);
|
||||
private static final Reflection.Method<LightEventListener, Void> updateSectionStatus = Reflection.getMethod(LightEventListener.class, SectionPos.class, boolean.class);
|
||||
private static final Reflection.Method<ChunkAccess, Void> setUnsaved = Reflection.getMethod(ChunkAccess.class, boolean.class);
|
||||
public void finish() {
|
||||
boolean onlyAir = hasOnlyAir.invoke(section);
|
||||
if (hadOnlyAir != onlyAir) {
|
||||
updateSectionStatus.invoke(lightEngine, of.invoke(null, cx, cy, cz), onlyAir);
|
||||
}
|
||||
|
||||
setUnsaved.invoke(chunk, true);
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class DeleteEntityPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public DeleteEntityPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!player.hasPermission("axiom.entity.*") && !player.hasPermission("axiom.entity.delete")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.plugin.canModifyWorld(player, player.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
List<UUID> delete = friendlyByteBuf.readList(FriendlyByteBuf::readUUID);
|
||||
|
||||
ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle();
|
||||
|
||||
List<String> whitelistedEntities = this.plugin.configuration.getStringList("whitelist-entities");
|
||||
List<String> blacklistedEntities = this.plugin.configuration.getStringList("blacklist-entities");
|
||||
|
||||
for (UUID uuid : delete) {
|
||||
Entity entity = serverLevel.getEntity(uuid);
|
||||
if (entity == null || entity instanceof net.minecraft.world.entity.player.Player || entity.hasPassenger(e -> e instanceof net.minecraft.world.entity.player.Player)) continue;
|
||||
|
||||
String type = EntityType.getKey(entity.getType()).toString();
|
||||
|
||||
if (!whitelistedEntities.isEmpty() && !whitelistedEntities.contains(type)) continue;
|
||||
if (blacklistedEntities.contains(type)) continue;
|
||||
|
||||
entity.remove(Entity.RemovalReason.DISCARDED);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,108 +1,174 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.*;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.View;
|
||||
import com.moulberry.axiom.WorldExtension;
|
||||
import com.moulberry.axiom.event.AxiomHandshakeEvent;
|
||||
import com.moulberry.axiom.persistence.ItemStackDataType;
|
||||
import com.moulberry.axiom.persistence.UUIDDataType;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.Channel;
|
||||
import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.minecraft.network.Connection;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.kyori.adventure.text.format.NamedTextColor;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Listener;
|
||||
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;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class HelloPacketListener implements AxiomPacketListener, Listener {
|
||||
public class HelloPacketListener implements PluginMessageListener {
|
||||
|
||||
private static final Reflection.Field<ServerGamePacketListenerImpl, Connection> packetListenerConnection = Reflection.getField(ServerGamePacketListenerImpl.class, Connection.class);
|
||||
private static final Reflection.Field<Connection, Channel> channel = Reflection.getField(Connection.class, Channel.class);
|
||||
private final AxiomPaper plugin;
|
||||
|
||||
private final AxiomPlayerManager axiomPlayers;
|
||||
|
||||
public HelloPacketListener(AxiomPlayerManager axiomPlayers) {
|
||||
this.axiomPlayers = axiomPlayers;
|
||||
public HelloPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(Player player, ByteBuf buf) {
|
||||
int apiVersion = MojBuf.readVarInt(buf);
|
||||
MojBuf.readNbt(buf); // Discard
|
||||
|
||||
if (apiVersion != AxiomConstants.API_VERSION) {
|
||||
player.sendMessage(Component.text("Unsupported Axiom API Version. Server supports " + AxiomConstants.API_VERSION + ", while client is " + apiVersion));
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!player.hasPermission("axiom.*")) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
int apiVersion = friendlyByteBuf.readVarInt();
|
||||
int dataVersion = friendlyByteBuf.readVarInt();
|
||||
friendlyByteBuf.readNbt(); // Discard
|
||||
|
||||
int serverDataVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion();
|
||||
if (dataVersion != serverDataVersion) {
|
||||
Component text = Component.text("Axiom: Incompatible data version detected (client " + dataVersion +
|
||||
", server " + serverDataVersion + "), are you using ViaVersion?");
|
||||
|
||||
String incompatibleDataVersion = plugin.configuration.getString("incompatible-data-version");
|
||||
if (incompatibleDataVersion == null) incompatibleDataVersion = "kick";
|
||||
if (incompatibleDataVersion.equals("warn")) {
|
||||
player.sendMessage(text.color(NamedTextColor.RED));
|
||||
return;
|
||||
} else if (!incompatibleDataVersion.equals("ignore")) {
|
||||
player.kick(text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (apiVersion != AxiomConstants.API_VERSION) {
|
||||
Component text = Component.text("Unsupported Axiom API Version. Server supports " + AxiomConstants.API_VERSION +
|
||||
", while client is " + apiVersion);
|
||||
|
||||
String unsupportedAxiomVersion = plugin.configuration.getString("unsupported-axiom-version");
|
||||
if (unsupportedAxiomVersion == null) unsupportedAxiomVersion = "kick";
|
||||
if (unsupportedAxiomVersion.equals("warn")) {
|
||||
player.sendMessage(text.color(NamedTextColor.RED));
|
||||
return;
|
||||
} else if (!unsupportedAxiomVersion.equals("ignore")) {
|
||||
player.kick(text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!player.getListeningPluginChannels().contains("axiom:restrictions")) {
|
||||
Component text = Component.text("This server requires the use of Axiom 2.3 or later. Contact the server administrator if you believe this is unintentional");
|
||||
|
||||
String unsupportedRestrictions = plugin.configuration.getString("client-doesnt-support-restrictions");
|
||||
if (unsupportedRestrictions == null) unsupportedRestrictions = "kick";
|
||||
if (unsupportedRestrictions.equals("warn")) {
|
||||
player.sendMessage(text.color(NamedTextColor.RED));
|
||||
return;
|
||||
} else if (!unsupportedRestrictions.equals("ignore")) {
|
||||
player.kick(text);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Call handshake event
|
||||
AxiomHandshakeEvent handshakeEvent = new AxiomHandshakeEvent(player);
|
||||
int maxBufferSize = plugin.configuration.getInt("max-block-buffer-packet-size");
|
||||
AxiomHandshakeEvent handshakeEvent = new AxiomHandshakeEvent(player, maxBufferSize);
|
||||
Bukkit.getPluginManager().callEvent(handshakeEvent);
|
||||
if (handshakeEvent.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
axiomPlayers.add(player);
|
||||
// Welcome to multiversioning!
|
||||
channel.get(
|
||||
packetListenerConnection.get(
|
||||
AxiomPaper.getConnection(
|
||||
AxiomPaper.convert(player)
|
||||
)
|
||||
)
|
||||
).pipeline().addBefore("decoder", "axiom-big-payload-handler", new AxiomBigPayloadHandler(player));
|
||||
this.plugin.activeAxiomPlayers.add(player.getUniqueId());
|
||||
int rateLimit = this.plugin.configuration.getInt("block-buffer-rate-limit");
|
||||
if (rateLimit > 0) {
|
||||
this.plugin.playerBlockBufferRateLimiters.putIfAbsent(player.getUniqueId(), RateLimiter.create(rateLimit));
|
||||
}
|
||||
|
||||
// Enable
|
||||
ByteBuf out = MojBuf.unpooled();
|
||||
out.writeBoolean(true);
|
||||
out.writeByte(0); // todo: world properties
|
||||
out.writeInt(handshakeEvent.getMaxBufferSize()); // Max Buffer Size
|
||||
out.writeBoolean(false); // No source info
|
||||
out.writeBoolean(false); // No source settings
|
||||
MojBuf.writeVarInt(out, 5); // Maximum Reach
|
||||
MojBuf.writeVarInt(out, 16); // Max editor views
|
||||
out.writeBoolean(true); // Editable Views
|
||||
OutChannel.ENABLE.send(player, out);
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeBoolean(true);
|
||||
buf.writeInt(handshakeEvent.getMaxBufferSize()); // Max Buffer Size
|
||||
buf.writeBoolean(false); // No source info
|
||||
buf.writeBoolean(false); // No source settings
|
||||
buf.writeVarInt(5); // Maximum Reach
|
||||
buf.writeVarInt(16); // Max editor views
|
||||
buf.writeBoolean(true); // Editable Views
|
||||
|
||||
byte[] enableBytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, enableBytes);
|
||||
player.sendPluginMessage(this.plugin, "axiom:enable", enableBytes);
|
||||
|
||||
// Initialize Hotbars
|
||||
PersistentDataContainer container = player.getPersistentDataContainer();
|
||||
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) {
|
||||
out = MojBuf.unpooled();
|
||||
out.writeByte((byte) activeHotbarIndex);
|
||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeByte((byte) activeHotbarIndex);
|
||||
for (int i=0; i<9*9; i++) {
|
||||
// Ignore selected hotbar
|
||||
if (i / 9 == activeHotbarIndex) {
|
||||
MojBuf.writeItem(out, new ItemStack(Material.AIR, 0));
|
||||
buf.writeItem(net.minecraft.world.item.ItemStack.EMPTY);
|
||||
} else {
|
||||
ItemStack stack = hotbarItems.get(AxiomConstants.axiomKey("slot_"+i), ItemStackDataType.INSTANCE);
|
||||
MojBuf.writeItem(out, stack);
|
||||
ItemStack stack = hotbarItems.get(new NamespacedKey("axiom", "slot_"+i), ItemStackDataType.INSTANCE);
|
||||
buf.writeItem(CraftItemStack.asNMSCopy(stack));
|
||||
}
|
||||
}
|
||||
OutChannel.INITIALIZE_HOTBARS.send(player, out);
|
||||
|
||||
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) {
|
||||
out = MojBuf.unpooled();
|
||||
MojBuf.writeUUID(out, activeView);
|
||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeUUID(activeView);
|
||||
|
||||
PersistentDataContainer[] views = container.get(AxiomConstants.VIEWS, PersistentDataType.TAG_CONTAINER_ARRAY);
|
||||
MojBuf.writeVarInt(out, views.length);
|
||||
buf.writeVarInt(views.length);
|
||||
for (PersistentDataContainer view : views) {
|
||||
View.load(view).write(out);
|
||||
View.load(view).write(buf);
|
||||
}
|
||||
|
||||
OutChannel.SET_EDITOR_VIEWS.send(player, out);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.sendPluginMessage(this.plugin, "axiom:set_editor_views", bytes);
|
||||
}
|
||||
|
||||
// Register world properties
|
||||
World world = player.getWorld();
|
||||
ServerWorldPropertiesRegistry properties = plugin.getOrCreateWorldProperties(world);
|
||||
|
||||
if (properties == null) {
|
||||
player.sendPluginMessage(plugin, "axiom:register_world_properties", new byte[]{0});
|
||||
} else {
|
||||
properties.registerFor(plugin, player);
|
||||
}
|
||||
|
||||
WorldExtension.onPlayerJoin(world, player);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,212 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.NbtSanitization;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.RelativeMovement;
|
||||
import net.minecraft.world.entity.decoration.HangingEntity;
|
||||
import net.minecraft.world.entity.decoration.ItemFrame;
|
||||
import net.minecraft.world.level.block.Rotation;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
public class ManipulateEntityPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public ManipulateEntityPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public enum PassengerManipulation {
|
||||
NONE,
|
||||
REMOVE_ALL,
|
||||
ADD_LIST,
|
||||
REMOVE_LIST
|
||||
}
|
||||
|
||||
public record ManipulateEntry(UUID uuid, @Nullable Set<RelativeMovement> relativeMovementSet, @Nullable Vec3 position,
|
||||
float yaw, float pitch, CompoundTag merge, PassengerManipulation passengerManipulation, List<UUID> passengers) {
|
||||
public static ManipulateEntry read(FriendlyByteBuf friendlyByteBuf) {
|
||||
UUID uuid = friendlyByteBuf.readUUID();
|
||||
|
||||
int flags = friendlyByteBuf.readByte();
|
||||
Set<RelativeMovement> relativeMovementSet = null;
|
||||
Vec3 position = null;
|
||||
float yaw = 0;
|
||||
float pitch = 0;
|
||||
if (flags >= 0) {
|
||||
relativeMovementSet = RelativeMovement.unpack(flags);
|
||||
position = new Vec3(friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble());
|
||||
yaw = friendlyByteBuf.readFloat();
|
||||
pitch = friendlyByteBuf.readFloat();
|
||||
}
|
||||
|
||||
CompoundTag nbt = friendlyByteBuf.readNbt();
|
||||
|
||||
PassengerManipulation passengerManipulation = friendlyByteBuf.readEnum(PassengerManipulation.class);
|
||||
List<UUID> passengers = List.of();
|
||||
if (passengerManipulation == PassengerManipulation.ADD_LIST || passengerManipulation == PassengerManipulation.REMOVE_LIST) {
|
||||
passengers = friendlyByteBuf.readList(FriendlyByteBuf::readUUID);
|
||||
}
|
||||
|
||||
return new ManipulateEntry(uuid, relativeMovementSet, position, yaw, pitch, nbt,
|
||||
passengerManipulation, passengers);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Rotation[] ROTATION_VALUES = Rotation.values();
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!player.hasPermission("axiom.entity.*") && !player.hasPermission("axiom.entity.manipulate")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.plugin.canModifyWorld(player, player.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
List<ManipulateEntry> entries = friendlyByteBuf.readList(ManipulateEntry::read);
|
||||
|
||||
ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle();
|
||||
|
||||
List<String> whitelistedEntities = this.plugin.configuration.getStringList("whitelist-entities");
|
||||
List<String> blacklistedEntities = this.plugin.configuration.getStringList("blacklist-entities");
|
||||
|
||||
for (ManipulateEntry entry : entries) {
|
||||
Entity entity = serverLevel.getEntity(entry.uuid);
|
||||
if (entity == null || entity instanceof net.minecraft.world.entity.player.Player || entity.hasPassenger(ManipulateEntityPacketListener::isPlayer)) continue;
|
||||
|
||||
String type = EntityType.getKey(entity.getType()).toString();
|
||||
|
||||
if (!whitelistedEntities.isEmpty() && !whitelistedEntities.contains(type)) continue;
|
||||
if (blacklistedEntities.contains(type)) continue;
|
||||
|
||||
if (entry.merge != null && !entry.merge.isEmpty()) {
|
||||
NbtSanitization.sanitizeEntity(entry.merge);
|
||||
|
||||
CompoundTag compoundTag = entity.saveWithoutId(new CompoundTag());
|
||||
compoundTag = merge(compoundTag, entry.merge);
|
||||
entity.load(compoundTag);
|
||||
}
|
||||
|
||||
Vec3 entryPos = entry.position();
|
||||
if (entryPos != null && entry.relativeMovementSet != null) {
|
||||
double newX = entry.relativeMovementSet.contains(RelativeMovement.X) ? entity.position().x + entryPos.x : entryPos.x;
|
||||
double newY = entry.relativeMovementSet.contains(RelativeMovement.Y) ? entity.position().y + entryPos.y : entryPos.y;
|
||||
double newZ = entry.relativeMovementSet.contains(RelativeMovement.Z) ? entity.position().z + entryPos.z : entryPos.z;
|
||||
float newYaw = entry.relativeMovementSet.contains(RelativeMovement.Y_ROT) ? entity.getYRot() + entry.yaw : entry.yaw;
|
||||
float newPitch = entry.relativeMovementSet.contains(RelativeMovement.X_ROT) ? entity.getXRot() + entry.pitch : entry.pitch;
|
||||
|
||||
if (entity instanceof HangingEntity hangingEntity) {
|
||||
float changedYaw = newYaw - entity.getYRot();
|
||||
int rotations = Math.round(changedYaw / 90);
|
||||
hangingEntity.rotate(ROTATION_VALUES[rotations & 3]);
|
||||
|
||||
if (entity instanceof ItemFrame itemFrame && itemFrame.getDirection().getAxis() == Direction.Axis.Y) {
|
||||
itemFrame.setRotation(itemFrame.getRotation() - Math.round(changedYaw / 45));
|
||||
}
|
||||
}
|
||||
|
||||
entity.teleportTo(serverLevel, newX, newY, newZ, Set.of(), newYaw, newPitch);
|
||||
entity.setYHeadRot(newYaw);
|
||||
}
|
||||
|
||||
switch (entry.passengerManipulation) {
|
||||
case NONE -> {}
|
||||
case REMOVE_ALL -> entity.ejectPassengers();
|
||||
case ADD_LIST -> {
|
||||
for (UUID passengerUuid : entry.passengers) {
|
||||
Entity passenger = serverLevel.getEntity(passengerUuid);
|
||||
|
||||
if (passenger == null || passenger.isPassenger() ||
|
||||
passenger instanceof net.minecraft.world.entity.player.Player || passenger.hasPassenger(ManipulateEntityPacketListener::isPlayer)) continue;
|
||||
|
||||
String passengerType = EntityType.getKey(passenger.getType()).toString();
|
||||
|
||||
if (!whitelistedEntities.isEmpty() && !whitelistedEntities.contains(passengerType)) continue;
|
||||
if (blacklistedEntities.contains(passengerType)) continue;
|
||||
|
||||
// Prevent mounting loop
|
||||
if (passenger.getSelfAndPassengers().anyMatch(entity2 -> entity2 == entity)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
passenger.startRiding(entity, true);
|
||||
}
|
||||
}
|
||||
case REMOVE_LIST -> {
|
||||
for (UUID passengerUuid : entry.passengers) {
|
||||
Entity passenger = serverLevel.getEntity(passengerUuid);
|
||||
if (passenger == null || passenger == entity || passenger instanceof net.minecraft.world.entity.player.Player ||
|
||||
passenger.hasPassenger(ManipulateEntityPacketListener::isPlayer)) continue;
|
||||
|
||||
String passengerType = EntityType.getKey(passenger.getType()).toString();
|
||||
|
||||
if (!whitelistedEntities.isEmpty() && !whitelistedEntities.contains(passengerType)) continue;
|
||||
if (blacklistedEntities.contains(passengerType)) continue;
|
||||
|
||||
Entity vehicle = passenger.getVehicle();
|
||||
if (vehicle == entity) {
|
||||
passenger.stopRiding();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static CompoundTag merge(CompoundTag left, CompoundTag right) {
|
||||
if (right.contains("axiom:modify")) {
|
||||
right.remove("axiom:modify");
|
||||
return right;
|
||||
}
|
||||
|
||||
for (String key : right.getAllKeys()) {
|
||||
Tag tag = right.get(key);
|
||||
if (tag instanceof CompoundTag compound) {
|
||||
if (compound.isEmpty()) {
|
||||
left.remove(key);
|
||||
} else if (left.contains(key, Tag.TAG_COMPOUND)) {
|
||||
CompoundTag child = left.getCompound(key);
|
||||
child = merge(child, compound);
|
||||
left.put(key, child);
|
||||
} else {
|
||||
CompoundTag copied = compound.copy();
|
||||
if (copied.contains("axiom:modify")) {
|
||||
copied.remove("axiom:modify");
|
||||
}
|
||||
left.put(key, copied);
|
||||
}
|
||||
} else {
|
||||
left.put(key, tag.copy());
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
private static boolean isPlayer(Entity entity) {
|
||||
return entity instanceof net.minecraft.world.entity.player.Player;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.marker.MarkerData;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.Marker;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class MarkerNbtRequestPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public MarkerNbtRequestPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!player.hasPermission("axiom.entity.*") && !player.hasPermission("axiom.entity.manipulate")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.plugin.canModifyWorld(player, player.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
UUID uuid = friendlyByteBuf.readUUID();
|
||||
|
||||
ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle();
|
||||
|
||||
Entity entity = serverLevel.getEntity(uuid);
|
||||
if (entity instanceof Marker marker) {
|
||||
CompoundTag data = MarkerData.getData(marker);
|
||||
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeUUID(uuid);
|
||||
buf.writeNbt(data);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
|
||||
player.sendPluginMessage(AxiomPaper.PLUGIN, "axiom:marker_nbt_response", bytes);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.OutChannel;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import com.moulberry.axiom.buffer.CompressedBlockEntity;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class RequestBlockEntityPacketListener implements AxiomPacketListener {
|
||||
|
||||
private static final Reflection.Method<ServerLevel, BlockEntity> getBlockEntity = Reflection.getTypedMethod(ServerLevel.class, BlockEntity.class, BlockPos.class);
|
||||
|
||||
private static final Reflection.Method<BlockEntity, CompoundTag> saveWithoutMetadata = Reflection.getTypedMethod(BlockEntity.class, CompoundTag.class, 2);
|
||||
|
||||
@Override
|
||||
public void onMessage(@NotNull Player bukkitPlayer, @NotNull ByteBuf buf) {
|
||||
long id = buf.readLong();
|
||||
|
||||
World world = Bukkit.getWorld(MojBuf.readKey(buf));
|
||||
if (world == null) {
|
||||
sendEmptyResponse(bukkitPlayer, id);
|
||||
return;
|
||||
}
|
||||
|
||||
ServerLevel level = AxiomPaper.convert(world);
|
||||
Long2ObjectMap<CompressedBlockEntity> map = new Long2ObjectOpenHashMap<>();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
// Save and compress block entities
|
||||
int count = MojBuf.readVarInt(buf);
|
||||
for (int i = 0; i < count; i++) {
|
||||
long pos = buf.readLong();
|
||||
BlockEntity blockEntity = getBlockEntity.invoke(level, AxiomPaper.convert(pos));
|
||||
if (blockEntity != null) {
|
||||
CompoundTag tag = saveWithoutMetadata.invoke(blockEntity);
|
||||
map.put(pos, CompressedBlockEntity.compress(tag, baos));
|
||||
}
|
||||
}
|
||||
|
||||
// Send response packet
|
||||
ByteBuf out = MojBuf.unpooled();
|
||||
out.writeLong(id);
|
||||
MojBuf.writeVarInt(out, map.size());
|
||||
for (Long2ObjectMap.Entry<CompressedBlockEntity> entry : map.long2ObjectEntrySet()) {
|
||||
out.writeLong(entry.getLongKey());
|
||||
entry.getValue().write(out);
|
||||
}
|
||||
OutChannel.BLOCK_ENTITIES.send(bukkitPlayer, out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMissingPerm(Player player, ByteBuf buf) {
|
||||
sendEmptyResponse(player, buf.readLong());
|
||||
}
|
||||
|
||||
private void sendEmptyResponse(Player player, long id) {
|
||||
ByteBuf buf = Unpooled.buffer(16);
|
||||
buf.writeLong(id);
|
||||
buf.writeByte(0); // no block entities
|
||||
OutChannel.BLOCK_ENTITIES.send(player, buf);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,278 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.buffer.CompressedBlockEntity;
|
||||
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.minecraft.world.level.chunk.LevelChunkSection;
|
||||
import net.minecraft.world.level.chunk.PalettedContainer;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class RequestChunkDataPacketListener implements PluginMessageListener {
|
||||
|
||||
private static final ResourceLocation RESPONSE_ID = new ResourceLocation("axiom:response_chunk_data");
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public RequestChunkDataPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player bukkitPlayer, @NotNull byte[] message) {
|
||||
ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle();
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
long id = friendlyByteBuf.readLong();
|
||||
|
||||
if (!this.plugin.canUseAxiom(bukkitPlayer)) {
|
||||
// We always send an 'empty' response in order to make the client happy
|
||||
sendEmptyResponse(player, id);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.plugin.canModifyWorld(bukkitPlayer, bukkitPlayer.getWorld())) {
|
||||
sendEmptyResponse(player, id);
|
||||
return;
|
||||
}
|
||||
|
||||
MinecraftServer server = player.getServer();
|
||||
if (server == null) {
|
||||
sendEmptyResponse(player, id);
|
||||
return;
|
||||
}
|
||||
|
||||
ResourceKey<Level> worldKey = friendlyByteBuf.readResourceKey(Registries.DIMENSION);
|
||||
ServerLevel level = server.getLevel(worldKey);
|
||||
if (level == null) {
|
||||
sendEmptyResponse(player, id);
|
||||
return;
|
||||
}
|
||||
|
||||
boolean sendBlockEntitiesInChunks = friendlyByteBuf.readBoolean();
|
||||
|
||||
Long2ObjectMap<CompressedBlockEntity> blockEntityMap = new Long2ObjectOpenHashMap<>();
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
|
||||
|
||||
// Save and compress block entities
|
||||
int count = friendlyByteBuf.readVarInt();
|
||||
for (int i = 0; i < count; i++) {
|
||||
long pos = friendlyByteBuf.readLong();
|
||||
BlockEntity blockEntity = level.getBlockEntity(mutableBlockPos.set(pos));
|
||||
if (blockEntity != null) {
|
||||
CompoundTag tag = blockEntity.saveWithoutMetadata();
|
||||
blockEntityMap.put(pos, CompressedBlockEntity.compress(tag, baos));
|
||||
}
|
||||
}
|
||||
|
||||
int playerSectionX = player.getBlockX() >> 4;
|
||||
int playerSectionZ = player.getBlockZ() >> 4;
|
||||
|
||||
Long2ObjectMap<PalettedContainer<BlockState>> sections = new Long2ObjectOpenHashMap<>();
|
||||
|
||||
int maxChunkLoadDistance = this.plugin.configuration.getInt("max-chunk-load-distance");
|
||||
|
||||
// Don't allow loading chunks outside render distance for plot worlds
|
||||
if (PlotSquaredIntegration.isPlotWorld(level.getWorld())) {
|
||||
maxChunkLoadDistance = 0;
|
||||
}
|
||||
|
||||
if (maxChunkLoadDistance > 0) {
|
||||
count = friendlyByteBuf.readVarInt();
|
||||
for (int i = 0; i < count; i++) {
|
||||
long pos = friendlyByteBuf.readLong();
|
||||
|
||||
int sx = BlockPos.getX(pos);
|
||||
int sy = BlockPos.getY(pos);
|
||||
int sz = BlockPos.getZ(pos);
|
||||
|
||||
int distance = Math.abs(playerSectionX - sx) + Math.abs(playerSectionZ - sz);
|
||||
if (distance > maxChunkLoadDistance) continue;
|
||||
|
||||
LevelChunk chunk = level.getChunk(sx, sz);
|
||||
int sectionIndex = chunk.getSectionIndexFromSectionY(sy);
|
||||
if (sectionIndex < 0 || sectionIndex >= chunk.getSectionsCount()) continue;
|
||||
LevelChunkSection section = chunk.getSection(sectionIndex);
|
||||
|
||||
if (section.hasOnlyAir()) {
|
||||
sections.put(pos, null);
|
||||
} else {
|
||||
PalettedContainer<BlockState> container = section.getStates();
|
||||
sections.put(pos, container);
|
||||
|
||||
if (sendBlockEntitiesInChunks && section.maybeHas(BlockState::hasBlockEntity)) {
|
||||
for (int x = 0; x < 16; x++) {
|
||||
for (int y = 0; y < 16; y++) {
|
||||
for (int z = 0; z < 16; z++) {
|
||||
BlockState blockState = container.get(x, y, z);
|
||||
if (blockState.hasBlockEntity()) {
|
||||
mutableBlockPos.set(sx*16 + x, sy*16 + y, sz*16 + z);
|
||||
BlockEntity blockEntity = chunk.getBlockEntity(mutableBlockPos, LevelChunk.EntityCreationType.CHECK);
|
||||
if (blockEntity != null) {
|
||||
CompoundTag tag = blockEntity.saveWithoutMetadata();
|
||||
blockEntityMap.put(mutableBlockPos.asLong(), CompressedBlockEntity.compress(tag, baos));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send response packet
|
||||
|
||||
boolean firstPart = true;
|
||||
int maxSize = 0x100000 - 64; // Leeway of 64 bytes
|
||||
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeLong(id);
|
||||
|
||||
for (Long2ObjectMap.Entry<CompressedBlockEntity> entry : blockEntityMap.long2ObjectEntrySet()) {
|
||||
int beforeWriterIndex = buf.writerIndex();
|
||||
|
||||
buf.writeLong(entry.getLongKey());
|
||||
entry.getValue().write(buf);
|
||||
|
||||
if (buf.writerIndex() >= maxSize) {
|
||||
if (firstPart) {
|
||||
// Finish and send current packet
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
buf.writeBoolean(false);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.connection.send(new ClientboundCustomPayloadPacket(RESPONSE_ID, buf));
|
||||
|
||||
// Continuation packet
|
||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeLong(id);
|
||||
} else {
|
||||
// Copy extra bytes
|
||||
int copiedSize = buf.writerIndex() - beforeWriterIndex;
|
||||
byte[] copied = new byte[copiedSize];
|
||||
buf.getBytes(beforeWriterIndex, copied);
|
||||
|
||||
// Discard extra bytes
|
||||
buf.writerIndex(beforeWriterIndex);
|
||||
|
||||
// Finish and send current packet
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
buf.writeBoolean(false);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.connection.send(new ClientboundCustomPayloadPacket(RESPONSE_ID, buf));
|
||||
|
||||
// Continuation packet
|
||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeLong(id);
|
||||
|
||||
// Write start of new packet
|
||||
buf.writeBytes(copied);
|
||||
firstPart = true;
|
||||
}
|
||||
} else {
|
||||
firstPart = false;
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
|
||||
for (Long2ObjectMap.Entry<PalettedContainer<BlockState>> entry : sections.long2ObjectEntrySet()) {
|
||||
int beforeWriterIndex = buf.writerIndex();
|
||||
|
||||
buf.writeLong(entry.getLongKey());
|
||||
var container = entry.getValue();
|
||||
if (container == null) {
|
||||
buf.writeBoolean(false);
|
||||
} else {
|
||||
buf.writeBoolean(true);
|
||||
entry.getValue().write(buf);
|
||||
}
|
||||
|
||||
if (buf.writerIndex() >= maxSize) {
|
||||
if (firstPart) {
|
||||
// Finish and send current packet
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
buf.writeBoolean(false);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.connection.send(new ClientboundCustomPayloadPacket(RESPONSE_ID, buf));
|
||||
|
||||
// Continuation packet
|
||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeLong(id);
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
} else {
|
||||
// Copy extra bytes
|
||||
int copiedSize = buf.writerIndex() - beforeWriterIndex;
|
||||
byte[] copied = new byte[copiedSize];
|
||||
buf.getBytes(beforeWriterIndex, copied);
|
||||
|
||||
// Discard extra bytes
|
||||
buf.writerIndex(beforeWriterIndex);
|
||||
|
||||
// Finish and send current packet
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
buf.writeBoolean(false);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.connection.send(new ClientboundCustomPayloadPacket(RESPONSE_ID, buf));
|
||||
|
||||
// Continuation packet
|
||||
buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeLong(id);
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
|
||||
// Write start of new packet
|
||||
buf.writeBytes(copied);
|
||||
firstPart = true;
|
||||
}
|
||||
} else {
|
||||
firstPart = false;
|
||||
}
|
||||
}
|
||||
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG);
|
||||
buf.writeBoolean(true);
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.connection.send(new ClientboundCustomPayloadPacket(RESPONSE_ID, buf));
|
||||
}
|
||||
|
||||
private void sendEmptyResponse(ServerPlayer player, long id) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(16));
|
||||
buf.writeLong(id);
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG); // no block entities
|
||||
buf.writeLong(AxiomConstants.MIN_POSITION_LONG); // no chunks
|
||||
buf.writeBoolean(true); // finished
|
||||
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.connection.send(new ClientboundCustomPayloadPacket(RESPONSE_ID, buf));
|
||||
}
|
||||
|
||||
}
|
@ -1,87 +1,373 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.google.common.util.concurrent.RateLimiter;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.WorldExtension;
|
||||
import com.moulberry.axiom.buffer.BiomeBuffer;
|
||||
import com.moulberry.axiom.buffer.BlockBuffer;
|
||||
import com.moulberry.axiom.buffer.CompressedBlockEntity;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.event.AxiomModifyWorldEvent;
|
||||
import com.moulberry.axiom.integration.RegionProtection;
|
||||
import com.moulberry.axiom.integration.VersionTranslator;
|
||||
import com.moulberry.axiom.version.VersionWrapper;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import com.moulberry.axiom.integration.SectionPermissionChecker;
|
||||
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
|
||||
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
||||
import it.unimi.dsi.fastutil.shorts.Short2ObjectMap;
|
||||
import net.minecraft.ChatFormatting;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.SectionPos;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.protocol.game.ClientboundChunksBiomesPacket;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.ai.village.poi.PoiType;
|
||||
import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
||||
import net.minecraft.world.level.ChunkPos;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.biome.Biome;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.EntityBlock;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.ChunkStatus;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.minecraft.world.level.chunk.LevelChunkSection;
|
||||
import net.minecraft.world.level.chunk.PalettedContainer;
|
||||
import net.minecraft.world.level.levelgen.Heightmap;
|
||||
import net.minecraft.world.level.lighting.LightEngine;
|
||||
import org.bukkit.Location;
|
||||
import xyz.jpenilla.reflectionremapper.ReflectionRemapper;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class SetBlockBufferPacketListener implements AxiomPacketListener {
|
||||
public class SetBlockBufferPacketListener {
|
||||
|
||||
@Override
|
||||
public void onMessage(Player player, ByteBuf buf) {
|
||||
World world = Bukkit.getWorld(MojBuf.readKey(buf));
|
||||
private final AxiomPaper plugin;
|
||||
private final Method updateBlockEntityTicker;
|
||||
private final WeakHashMap<ServerPlayer, RateLimiter> packetRateLimiter = new WeakHashMap<>();
|
||||
|
||||
// Call AxiomModifyWorldEvent event
|
||||
AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(player, world);
|
||||
Bukkit.getPluginManager().callEvent(modifyWorldEvent);
|
||||
if (modifyWorldEvent.isCancelled()) return;
|
||||
public SetBlockBufferPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
MojBuf.readUUID(buf); // Discard, we don't need to associate buffers
|
||||
if (!buf.readBoolean()) {
|
||||
MojBuf.readNbt(buf); // Discard sourceInfo
|
||||
ReflectionRemapper reflectionRemapper = ReflectionRemapper.forReobfMappingsInPaperJar();
|
||||
String methodName = reflectionRemapper.remapMethodName(LevelChunk.class, "updateBlockEntityTicker", BlockEntity.class);
|
||||
|
||||
try {
|
||||
this.updateBlockEntityTicker = LevelChunk.class.getDeclaredMethod(methodName, BlockEntity.class);
|
||||
this.updateBlockEntityTicker.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
byte type = buf.readByte();
|
||||
public boolean onReceive(ServerPlayer player, FriendlyByteBuf friendlyByteBuf) {
|
||||
MinecraftServer server = player.getServer();
|
||||
if (server == null) return false;
|
||||
|
||||
ResourceKey<Level> worldKey = friendlyByteBuf.readResourceKey(Registries.DIMENSION);
|
||||
friendlyByteBuf.readUUID(); // Discard, we don't need to associate buffers
|
||||
boolean continuation = friendlyByteBuf.readBoolean();
|
||||
|
||||
if (!continuation) {
|
||||
friendlyByteBuf.readNbt(); // Discard sourceInfo
|
||||
}
|
||||
|
||||
RateLimiter rateLimiter = this.plugin.getBlockBufferRateLimiter(player.getUUID());
|
||||
|
||||
byte type = friendlyByteBuf.readByte();
|
||||
if (type == 0) {
|
||||
applyBlockBuffer(world, player, buf);
|
||||
AtomicBoolean reachedRateLimit = new AtomicBoolean(false);
|
||||
BlockBuffer buffer = BlockBuffer.load(friendlyByteBuf, rateLimiter, reachedRateLimit);
|
||||
if (reachedRateLimit.get()) {
|
||||
player.sendSystemMessage(Component.literal("[Axiom] Exceeded server rate-limit of " + (int)rateLimiter.getRate() + " sections per second")
|
||||
.withStyle(ChatFormatting.RED));
|
||||
}
|
||||
|
||||
if (this.plugin.logLargeBlockBufferChanges()) {
|
||||
this.plugin.getLogger().info("Player " + player.getUUID() + " modified " + buffer.entrySet().size() + " chunk sections (blocks)");
|
||||
if (buffer.getTotalBlockEntities() > 0) {
|
||||
this.plugin.getLogger().info("Player " + player.getUUID() + " modified " + buffer.getTotalBlockEntities() + " block entities, compressed bytes = " +
|
||||
buffer.getTotalBlockEntityBytes());
|
||||
}
|
||||
}
|
||||
|
||||
applyBlockBuffer(player, server, buffer, worldKey);
|
||||
} else if (type == 1) {
|
||||
applyBiomeBuffer(world, new BiomeBuffer(buf));
|
||||
AtomicBoolean reachedRateLimit = new AtomicBoolean(false);
|
||||
BiomeBuffer buffer = BiomeBuffer.load(friendlyByteBuf, rateLimiter, reachedRateLimit);
|
||||
if (reachedRateLimit.get()) {
|
||||
player.sendSystemMessage(Component.literal("[Axiom] Exceeded server rate-limit of " + (int)rateLimiter.getRate() + " sections per second")
|
||||
.withStyle(ChatFormatting.RED));
|
||||
}
|
||||
|
||||
if (this.plugin.logLargeBlockBufferChanges()) {
|
||||
this.plugin.getLogger().info("Player " + player.getUUID() + " modified " + buffer.size() + " chunk sections (biomes)");
|
||||
}
|
||||
|
||||
applyBiomeBuffer(player, server, buffer, worldKey);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Unknown buffer type: " + type);
|
||||
throw new RuntimeException("Unknown buffer type: " + type);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void applyBlockBuffer(ServerPlayer player, MinecraftServer server, BlockBuffer buffer, ResourceKey<Level> worldKey) {
|
||||
server.execute(() -> {
|
||||
try {
|
||||
ServerLevel world = player.serverLevel();
|
||||
if (!world.dimension().equals(worldKey)) return;
|
||||
|
||||
if (!this.plugin.canUseAxiom(player.getBukkitEntity())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.plugin.canModifyWorld(player.getBukkitEntity(), world.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Allowed, apply buffer
|
||||
BlockPos.MutableBlockPos blockPos = new BlockPos.MutableBlockPos();
|
||||
WorldExtension extension = WorldExtension.get(world);
|
||||
|
||||
BlockState emptyState = BlockBuffer.EMPTY_STATE;
|
||||
|
||||
for (Long2ObjectMap.Entry<PalettedContainer<BlockState>> entry : buffer.entrySet()) {
|
||||
int cx = BlockPos.getX(entry.getLongKey());
|
||||
int cy = BlockPos.getY(entry.getLongKey());
|
||||
int cz = BlockPos.getZ(entry.getLongKey());
|
||||
PalettedContainer<BlockState> container = entry.getValue();
|
||||
|
||||
if (cy < world.getMinSection() || cy >= world.getMaxSection()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SectionPermissionChecker checker = PlotSquaredIntegration.checkSection(player.getBukkitEntity(), world.getWorld(), cx, cy, cz);
|
||||
if (checker != null && checker.noneAllowed()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
LevelChunk chunk = world.getChunk(cx, cz);
|
||||
|
||||
LevelChunkSection section = chunk.getSection(world.getSectionIndexFromSectionY(cy));
|
||||
PalettedContainer<BlockState> sectionStates = section.getStates();
|
||||
boolean hasOnlyAir = section.hasOnlyAir();
|
||||
|
||||
Heightmap worldSurface = null;
|
||||
Heightmap oceanFloor = null;
|
||||
Heightmap motionBlocking = null;
|
||||
Heightmap motionBlockingNoLeaves = null;
|
||||
for (Map.Entry<Heightmap.Types, Heightmap> heightmap : chunk.getHeightmaps()) {
|
||||
switch (heightmap.getKey()) {
|
||||
case WORLD_SURFACE -> worldSurface = heightmap.getValue();
|
||||
case OCEAN_FLOOR -> oceanFloor = heightmap.getValue();
|
||||
case MOTION_BLOCKING -> motionBlocking = heightmap.getValue();
|
||||
case MOTION_BLOCKING_NO_LEAVES -> motionBlockingNoLeaves = heightmap.getValue();
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
|
||||
private void applyBlockBuffer(World world, Player player, ByteBuf buf) {
|
||||
RegionProtection regionProtection = RegionProtection.getProtection.apply(player, world);
|
||||
boolean sectionChanged = false;
|
||||
boolean sectionLightChanged = false;
|
||||
|
||||
long sectionPos;
|
||||
while ((sectionPos = buf.readLong()) != AxiomConstants.MIN_POSITION_LONG) {
|
||||
int cx = BlockPos.getX(sectionPos);
|
||||
int cy = BlockPos.getY(sectionPos);
|
||||
int cz = BlockPos.getZ(sectionPos);
|
||||
boolean containerMaybeHasPoi = container.maybeHas(PoiTypes::hasPoi);
|
||||
boolean sectionMaybeHasPoi = section.maybeHas(PoiTypes::hasPoi);
|
||||
|
||||
boolean canBuildInSection = regionProtection.canBuildInSection(cx, cy, cz);
|
||||
ChunkSectionModifier section = new ChunkSectionModifier(world, cx, cy, cz);
|
||||
Short2ObjectMap<CompressedBlockEntity> blockEntityChunkMap = buffer.getBlockEntityChunkMap(entry.getLongKey());
|
||||
|
||||
VersionTranslator.impl.readPalettedContainer(player, buf, canBuildInSection ? section::setState : (x, y, z, blockState) -> {});
|
||||
int minX = 0;
|
||||
int minY = 0;
|
||||
int minZ = 0;
|
||||
int maxX = 15;
|
||||
int maxY = 15;
|
||||
int maxZ = 15;
|
||||
|
||||
int blockEntitySize = Math.min(4096, Math.max(0, MojBuf.readVarInt(buf)));
|
||||
for (int i = 0; i < blockEntitySize; i++) {
|
||||
short offset = buf.readShort();
|
||||
CompressedBlockEntity entity = CompressedBlockEntity.read(buf);
|
||||
if(canBuildInSection)
|
||||
section.setBlockEntity(offset & 0xF, (offset >> 4) & 0xF, offset >> 8, entity.decompress());
|
||||
}
|
||||
|
||||
if(canBuildInSection)
|
||||
section.finish();
|
||||
if (checker != null) {
|
||||
minX = checker.bounds().minX();
|
||||
minY = checker.bounds().minY();
|
||||
minZ = checker.bounds().minZ();
|
||||
maxX = checker.bounds().maxX();
|
||||
maxY = checker.bounds().maxY();
|
||||
maxZ = checker.bounds().maxZ();
|
||||
if (checker.allAllowed()) {
|
||||
checker = null;
|
||||
}
|
||||
}
|
||||
|
||||
private void applyBiomeBuffer(World world, BiomeBuffer biomeBuffer) {
|
||||
Set<Chunk> changedChunks = new HashSet<>();
|
||||
for (int x = minX; x <= maxX; x++) {
|
||||
for (int y = minY; y <= maxY; y++) {
|
||||
for (int z = minZ; z <= maxZ; z++) {
|
||||
BlockState blockState = container.get(x, y, z);
|
||||
if (blockState == emptyState) continue;
|
||||
|
||||
int bx = cx*16 + x;
|
||||
int by = cy*16 + y;
|
||||
int bz = cz*16 + z;
|
||||
|
||||
if (hasOnlyAir && blockState.isAir()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (checker != null && !checker.allowed(x, y, z)) continue;
|
||||
|
||||
BlockState old = section.setBlockState(x, y, z, blockState, true);
|
||||
if (blockState != old) {
|
||||
sectionChanged = true;
|
||||
blockPos.set(bx, by, bz);
|
||||
|
||||
Block block = blockState.getBlock();
|
||||
motionBlocking.update(x, by, z, blockState);
|
||||
motionBlockingNoLeaves.update(x, by, z, blockState);
|
||||
oceanFloor.update(x, by, z, blockState);
|
||||
worldSurface.update(x, by, z, blockState);
|
||||
|
||||
if (false) { // Full update
|
||||
old.onRemove(world, blockPos, blockState, false);
|
||||
|
||||
if (sectionStates.get(x, y, z).is(block)) {
|
||||
blockState.onPlace(world, blockPos, old, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (blockState.hasBlockEntity()) {
|
||||
BlockEntity blockEntity = chunk.getBlockEntity(blockPos, LevelChunk.EntityCreationType.CHECK);
|
||||
|
||||
if (blockEntity == null) {
|
||||
// There isn't a block entity here, create it!
|
||||
blockEntity = ((EntityBlock)block).newBlockEntity(blockPos, blockState);
|
||||
if (blockEntity != null) {
|
||||
chunk.addAndRegisterBlockEntity(blockEntity);
|
||||
}
|
||||
} else if (blockEntity.getType().isValid(blockState)) {
|
||||
// Block entity is here and the type is correct
|
||||
blockEntity.setBlockState(blockState);
|
||||
|
||||
try {
|
||||
this.updateBlockEntityTicker.invoke(chunk, blockEntity);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
// Block entity type isn't correct, we need to recreate it
|
||||
chunk.removeBlockEntity(blockPos);
|
||||
|
||||
blockEntity = ((EntityBlock)block).newBlockEntity(blockPos, blockState);
|
||||
if (blockEntity != null) {
|
||||
chunk.addAndRegisterBlockEntity(blockEntity);
|
||||
}
|
||||
}
|
||||
if (blockEntity != null && blockEntityChunkMap != null) {
|
||||
int key = x | (y << 4) | (z << 8);
|
||||
CompressedBlockEntity savedBlockEntity = blockEntityChunkMap.get((short) key);
|
||||
if (savedBlockEntity != null) {
|
||||
blockEntity.load(savedBlockEntity.decompress());
|
||||
}
|
||||
}
|
||||
} else if (old.hasBlockEntity()) {
|
||||
chunk.removeBlockEntity(blockPos);
|
||||
}
|
||||
|
||||
// Update Light
|
||||
sectionLightChanged |= LightEngine.hasDifferentLightProperties(chunk, blockPos, old, blockState);
|
||||
|
||||
// Update Poi
|
||||
Optional<Holder<PoiType>> newPoi = containerMaybeHasPoi ? PoiTypes.forState(blockState) : Optional.empty();
|
||||
Optional<Holder<PoiType>> oldPoi = sectionMaybeHasPoi ? PoiTypes.forState(old) : Optional.empty();
|
||||
if (!Objects.equals(oldPoi, newPoi)) {
|
||||
if (oldPoi.isPresent()) world.getPoiManager().remove(blockPos);
|
||||
if (newPoi.isPresent()) world.getPoiManager().add(blockPos, newPoi.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boolean nowHasOnlyAir = section.hasOnlyAir();
|
||||
if (hasOnlyAir != nowHasOnlyAir) {
|
||||
world.getChunkSource().getLightEngine().updateSectionStatus(SectionPos.of(cx, cy, cz), nowHasOnlyAir);
|
||||
}
|
||||
|
||||
if (sectionChanged) {
|
||||
extension.sendChunk(cx, cz);
|
||||
chunk.setUnsaved(true);
|
||||
}
|
||||
if (sectionLightChanged) {
|
||||
extension.lightChunk(cx, cz);
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
player.getBukkitEntity().kick(net.kyori.adventure.text.Component.text("An error occured while processing block change: " + t.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void applyBiomeBuffer(ServerPlayer player, MinecraftServer server, BiomeBuffer biomeBuffer, ResourceKey<Level> worldKey) {
|
||||
server.execute(() -> {
|
||||
try {
|
||||
ServerLevel world = player.serverLevel();
|
||||
if (!world.dimension().equals(worldKey)) return;
|
||||
|
||||
if (!this.plugin.canUseAxiom(player.getBukkitEntity())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.plugin.canModifyWorld(player.getBukkitEntity(), world.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
Set<LevelChunk> changedChunks = new HashSet<>();
|
||||
|
||||
int minSection = world.getMinSection();
|
||||
int maxSection = world.getMaxSection();
|
||||
|
||||
Optional<Registry<Biome>> registryOptional = world.registryAccess().registry(Registries.BIOME);
|
||||
if (registryOptional.isEmpty()) return;
|
||||
|
||||
Registry<Biome> registry = registryOptional.get();
|
||||
|
||||
biomeBuffer.forEachEntry((x, y, z, biome) -> {
|
||||
world.setBiome(x*4, y*4, z*4, biome);
|
||||
int cx = x/4 - (x < 0 ? 1 : 0);
|
||||
int cz = z/4 - (z < 0 ? 1 : 0);
|
||||
changedChunks.add(world.getChunkAt(cx, cz));
|
||||
int cy = y >> 2;
|
||||
if (cy < minSection || cy >= maxSection) {
|
||||
return;
|
||||
}
|
||||
|
||||
var chunk = (LevelChunk) world.getChunk(x >> 2, z >> 2, ChunkStatus.FULL, false);
|
||||
if (chunk == null) return;
|
||||
|
||||
var section = chunk.getSection(cy - minSection);
|
||||
PalettedContainer<Holder<Biome>> container = (PalettedContainer<Holder<Biome>>) section.getBiomes();
|
||||
|
||||
var holder = registry.getHolder(biome);
|
||||
if (holder.isPresent()) {
|
||||
if (!PlotSquaredIntegration.canPlaceBlock(player.getBukkitEntity(),
|
||||
new Location(player.getBukkitEntity().getWorld(), x+1, y+1, z+1))) return;
|
||||
|
||||
container.set(x & 3, y & 3, z & 3, holder.get());
|
||||
changedChunks.add(chunk);
|
||||
}
|
||||
});
|
||||
|
||||
VersionWrapper.impl.publishBiomeChange(AxiomPaper.convert(world), changedChunks);
|
||||
var chunkMap = world.getChunkSource().chunkMap;
|
||||
HashMap<ServerPlayer, List<LevelChunk>> map = new HashMap<>();
|
||||
for (LevelChunk chunk : changedChunks) {
|
||||
chunk.setUnsaved(true);
|
||||
ChunkPos chunkPos = chunk.getPos();
|
||||
for (ServerPlayer serverPlayer2 : chunkMap.getPlayers(chunkPos, false)) {
|
||||
map.computeIfAbsent(serverPlayer2, serverPlayer -> new ArrayList<>()).add(chunk);
|
||||
}
|
||||
}
|
||||
map.forEach((serverPlayer, list) -> serverPlayer.connection.send(ClientboundChunksBiomesPacket.forChunks(list)));
|
||||
} catch (Throwable t) {
|
||||
player.getBukkitEntity().kick(net.kyori.adventure.text.Component.text("An error occured while processing biome change: " + t.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,68 +1,268 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.event.AxiomModifyWorldEvent;
|
||||
import com.moulberry.axiom.integration.RegionProtection;
|
||||
import com.moulberry.axiom.integration.VersionTranslator;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Vec3i;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.core.SectionPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.network.ServerGamePacketListenerImpl;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.InteractionHand;
|
||||
import net.minecraft.world.entity.ai.village.poi.PoiType;
|
||||
import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.context.BlockPlaceContext;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.EntityBlock;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.minecraft.world.level.chunk.LevelChunkSection;
|
||||
import net.minecraft.world.level.levelgen.Heightmap;
|
||||
import net.minecraft.world.level.lighting.LightEngine;
|
||||
import net.minecraft.world.phys.BlockHitResult;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.block.CraftBlock;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import xyz.jpenilla.reflectionremapper.ReflectionRemapper;
|
||||
|
||||
public class SetBlockPacketListener implements AxiomPacketListener {
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.function.IntFunction;
|
||||
|
||||
private static final Reflection.Method<Level, Boolean> setBlock = Reflection.getTypedMethod(Level.class, boolean.class, BlockPos.class, BlockState.class, int.class);
|
||||
public class SetBlockPacketListener implements PluginMessageListener {
|
||||
|
||||
private static final Reflection.Method<Vec3i, Integer> getX = Reflection.getTypedMethod(Vec3i.class, int.class, 1);
|
||||
private static final Reflection.Method<Vec3i, Integer> getY = Reflection.getTypedMethod(Vec3i.class, int.class, 2);
|
||||
private static final Reflection.Method<Vec3i, Integer> getZ = Reflection.getTypedMethod(Vec3i.class, int.class, 3);
|
||||
private final AxiomPaper plugin;
|
||||
private final Method updateBlockEntityTicker;
|
||||
|
||||
public SetBlockPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
|
||||
ReflectionRemapper reflectionRemapper = ReflectionRemapper.forReobfMappingsInPaperJar();
|
||||
String methodName = reflectionRemapper.remapMethodName(LevelChunk.class, "updateBlockEntityTicker", BlockEntity.class);
|
||||
|
||||
try {
|
||||
this.updateBlockEntityTicker = LevelChunk.class.getDeclaredMethod(methodName, BlockEntity.class);
|
||||
this.updateBlockEntityTicker.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final Reflection.Method<ServerGamePacketListenerImpl, Void> ackBlockChangesUpTo = Reflection.getMethod(ServerGamePacketListenerImpl.class, int.class);
|
||||
@Override
|
||||
public void onMessage(@NotNull Player player, @NotNull ByteBuf buf) {
|
||||
World world = player.getWorld();
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player bukkitPlayer, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(bukkitPlayer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if player is allowed to modify this world
|
||||
AxiomModifyWorldEvent modifyWorldEvent = new AxiomModifyWorldEvent(player, world);
|
||||
Bukkit.getPluginManager().callEvent(modifyWorldEvent);
|
||||
if (modifyWorldEvent.isCancelled()) return;
|
||||
if (!this.plugin.canModifyWorld(bukkitPlayer, bukkitPlayer.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Read packet
|
||||
BlockPos pos = AxiomPaper.convert(buf.readLong());
|
||||
BlockState state = VersionTranslator.impl.blockStateMapper(player).apply(MojBuf.readVarInt(buf));
|
||||
boolean updateNeighbors = buf.readBoolean();
|
||||
int sequenceId = buf.readInt();
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
IntFunction<Map<BlockPos, BlockState>> mapFunction = FriendlyByteBuf.limitValue(Maps::newLinkedHashMapWithExpectedSize, 512);
|
||||
Map<BlockPos, BlockState> blocks = friendlyByteBuf.readMap(mapFunction,
|
||||
FriendlyByteBuf::readBlockPos, buf -> buf.readById(this.plugin.allowedBlockRegistry));
|
||||
boolean updateNeighbors = friendlyByteBuf.readBoolean();
|
||||
|
||||
ServerPlayer serverPlayer = AxiomPaper.convert(player);
|
||||
int x = getX.invoke(pos);
|
||||
int y = getY.invoke(pos);
|
||||
int z = getZ.invoke(pos);
|
||||
int cx = x / 16 - (x < 0 ? 1 : 0);
|
||||
int cy = y / 16 - (y < 0 ? 1 : 0);
|
||||
int cz = z / 16 - (z < 0 ? 1 : 0);
|
||||
int reason = friendlyByteBuf.readVarInt();
|
||||
boolean breaking = friendlyByteBuf.readBoolean();
|
||||
BlockHitResult blockHit = friendlyByteBuf.readBlockHitResult();
|
||||
InteractionHand hand = friendlyByteBuf.readEnum(InteractionHand.class);
|
||||
int sequenceId = friendlyByteBuf.readVarInt();
|
||||
|
||||
ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle();
|
||||
|
||||
org.bukkit.inventory.ItemStack heldItem;
|
||||
if (hand == InteractionHand.MAIN_HAND) {
|
||||
heldItem = bukkitPlayer.getInventory().getItemInMainHand();
|
||||
} else {
|
||||
heldItem = bukkitPlayer.getInventory().getItemInOffHand();
|
||||
}
|
||||
|
||||
org.bukkit.block.Block blockClicked = bukkitPlayer.getWorld().getBlockAt(blockHit.getBlockPos().getX(),
|
||||
blockHit.getBlockPos().getY(), blockHit.getBlockPos().getZ());
|
||||
|
||||
BlockFace blockFace = CraftBlock.notchToBlockFace(blockHit.getDirection());
|
||||
|
||||
// Call interact event
|
||||
PlayerInteractEvent playerInteractEvent = new PlayerInteractEvent(bukkitPlayer,
|
||||
breaking ? Action.LEFT_CLICK_BLOCK : Action.RIGHT_CLICK_BLOCK, heldItem, blockClicked, blockFace);
|
||||
if (!playerInteractEvent.callEvent()) {
|
||||
if (sequenceId >= 0) {
|
||||
player.connection.ackBlockChangesUpTo(sequenceId);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
CraftWorld world = player.level().getWorld();
|
||||
|
||||
BlockPlaceContext blockPlaceContext = new BlockPlaceContext(player, hand, player.getItemInHand(hand), blockHit);
|
||||
|
||||
// Update blocks
|
||||
if(RegionProtection.getProtection.apply(player, world).canBuildInSection(cx, cy, cz)) {
|
||||
if (updateNeighbors) {
|
||||
setBlock.invoke(AxiomPaper.convert(player.getWorld()), pos, state, 3);
|
||||
} else {
|
||||
ChunkSectionModifier section = new ChunkSectionModifier(player.getWorld(), cx, cy, cz);
|
||||
section.setState(x & 0xF, y & 0xF, z & 0xF, state);
|
||||
section.finish();
|
||||
int count = 0;
|
||||
for (Map.Entry<BlockPos, BlockState> entry : blocks.entrySet()) {
|
||||
if (count++ > 64) break;
|
||||
|
||||
BlockPos blockPos = entry.getKey();
|
||||
BlockState blockState = entry.getValue();
|
||||
|
||||
// Check PlotSquared
|
||||
if (blockState.isAir()) {
|
||||
if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) {
|
||||
continue;
|
||||
}
|
||||
} else if (!PlotSquaredIntegration.canPlaceBlock(bukkitPlayer, new Location(world, blockPos.getX(), blockPos.getY(), blockPos.getZ()))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Place block
|
||||
player.level().setBlock(blockPos, blockState, 3);
|
||||
}
|
||||
} else {
|
||||
int count = 0;
|
||||
for (Map.Entry<BlockPos, BlockState> entry : blocks.entrySet()) {
|
||||
if (count++ > 64) break;
|
||||
|
||||
BlockPos blockPos = entry.getKey();
|
||||
BlockState blockState = entry.getValue();
|
||||
|
||||
// Check PlotSquared
|
||||
if (blockState.isAir()) {
|
||||
if (!PlotSquaredIntegration.canBreakBlock(bukkitPlayer, world.getBlockAt(blockPos.getX(), blockPos.getY(), blockPos.getZ()))) {
|
||||
continue;
|
||||
}
|
||||
} else if (!PlotSquaredIntegration.canPlaceBlock(bukkitPlayer, new Location(world, blockPos.getX(), blockPos.getY(), blockPos.getZ()))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Place block
|
||||
int bx = blockPos.getX();
|
||||
int by = blockPos.getY();
|
||||
int bz = blockPos.getZ();
|
||||
int x = bx & 0xF;
|
||||
int y = by & 0xF;
|
||||
int z = bz & 0xF;
|
||||
int cx = bx >> 4;
|
||||
int cy = by >> 4;
|
||||
int cz = bz >> 4;
|
||||
|
||||
ServerLevel level = player.serverLevel();
|
||||
LevelChunk chunk = level.getChunk(cx, cz);
|
||||
chunk.setUnsaved(true);
|
||||
|
||||
LevelChunkSection section = chunk.getSection(level.getSectionIndexFromSectionY(cy));
|
||||
boolean hasOnlyAir = section.hasOnlyAir();
|
||||
|
||||
Heightmap worldSurface = null;
|
||||
Heightmap oceanFloor = null;
|
||||
Heightmap motionBlocking = null;
|
||||
Heightmap motionBlockingNoLeaves = null;
|
||||
for (Map.Entry<Heightmap.Types, Heightmap> heightmap : chunk.getHeightmaps()) {
|
||||
switch (heightmap.getKey()) {
|
||||
case WORLD_SURFACE -> worldSurface = heightmap.getValue();
|
||||
case OCEAN_FLOOR -> oceanFloor = heightmap.getValue();
|
||||
case MOTION_BLOCKING -> motionBlocking = heightmap.getValue();
|
||||
case MOTION_BLOCKING_NO_LEAVES -> motionBlockingNoLeaves = heightmap.getValue();
|
||||
default -> {}
|
||||
}
|
||||
}
|
||||
|
||||
BlockState old = section.setBlockState(x, y, z, blockState, true);
|
||||
if (blockState != old) {
|
||||
Block block = blockState.getBlock();
|
||||
motionBlocking.update(x, by, z, blockState);
|
||||
motionBlockingNoLeaves.update(x, by, z, blockState);
|
||||
oceanFloor.update(x, by, z, blockState);
|
||||
worldSurface.update(x, by, z, blockState);
|
||||
|
||||
if (blockState.hasBlockEntity()) {
|
||||
BlockEntity blockEntity = chunk.getBlockEntity(blockPos, LevelChunk.EntityCreationType.CHECK);
|
||||
|
||||
if (blockEntity == null) {
|
||||
// There isn't a block entity here, create it!
|
||||
blockEntity = ((EntityBlock)block).newBlockEntity(blockPos, blockState);
|
||||
if (blockEntity != null) {
|
||||
chunk.addAndRegisterBlockEntity(blockEntity);
|
||||
}
|
||||
} else if (blockEntity.getType().isValid(blockState)) {
|
||||
// Block entity is here and the type is correct
|
||||
// Just update the state and ticker and move on
|
||||
blockEntity.setBlockState(blockState);
|
||||
|
||||
try {
|
||||
this.updateBlockEntityTicker.invoke(chunk, blockEntity);
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
} else {
|
||||
// Block entity type isn't correct, we need to recreate it
|
||||
chunk.removeBlockEntity(blockPos);
|
||||
|
||||
blockEntity = ((EntityBlock)block).newBlockEntity(blockPos, blockState);
|
||||
if (blockEntity != null) {
|
||||
chunk.addAndRegisterBlockEntity(blockEntity);
|
||||
}
|
||||
}
|
||||
} else if (old.hasBlockEntity()) {
|
||||
chunk.removeBlockEntity(blockPos);
|
||||
}
|
||||
|
||||
// Mark block changed
|
||||
level.getChunkSource().blockChanged(blockPos);
|
||||
|
||||
// Update Light
|
||||
if (LightEngine.hasDifferentLightProperties(chunk, blockPos, old, blockState)) {
|
||||
// Note: Skylight Sources not currently needed on Paper due to Starlight
|
||||
// This might change in the future, so be careful!
|
||||
// chunk.getSkyLightSources().update(chunk, x, by, z);
|
||||
level.getChunkSource().getLightEngine().checkBlock(blockPos);
|
||||
}
|
||||
|
||||
// Update Poi
|
||||
Optional<Holder<PoiType>> newPoi = PoiTypes.forState(blockState);
|
||||
Optional<Holder<PoiType>> oldPoi = PoiTypes.forState(old);
|
||||
if (!Objects.equals(oldPoi, newPoi)) {
|
||||
if (oldPoi.isPresent()) level.getPoiManager().remove(blockPos);
|
||||
if (newPoi.isPresent()) level.getPoiManager().add(blockPos, newPoi.get());
|
||||
}
|
||||
}
|
||||
|
||||
boolean nowHasOnlyAir = section.hasOnlyAir();
|
||||
if (hasOnlyAir != nowHasOnlyAir) {
|
||||
level.getChunkSource().getLightEngine().updateSectionStatus(SectionPos.of(cx, cy, cz), nowHasOnlyAir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!breaking) {
|
||||
BlockPos clickedPos = blockPlaceContext.getClickedPos();
|
||||
ItemStack inHand = player.getItemInHand(hand);
|
||||
BlockState blockState = player.level().getBlockState(clickedPos);
|
||||
|
||||
BlockItem.updateCustomBlockEntityTag(player.level(), player, clickedPos, inHand);
|
||||
blockState.getBlock().setPlacedBy(player.level(), clickedPos, blockState, player, inHand);
|
||||
}
|
||||
|
||||
if (sequenceId >= 0) {
|
||||
ackBlockChangesUpTo.invoke(AxiomPaper.getConnection(serverPlayer), sequenceId);
|
||||
player.connection.ackBlockChangesUpTo(sequenceId);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,24 +1,39 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.View;
|
||||
import com.moulberry.axiom.persistence.UUIDDataType;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
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 AxiomPacketListener {
|
||||
public class SetEditorViewsPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public SetEditorViewsPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(@NotNull Player player, @NotNull ByteBuf buf) {
|
||||
UUID uuid = MojBuf.readUUID(buf);
|
||||
List<View> views = MojBuf.readList(buf, View::read);
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
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);
|
||||
|
@ -1,23 +1,30 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import com.moulberry.axiom.event.AxiomFlySpeedChangeEvent;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.entity.player.Abilities;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SetFlySpeedPacketListener implements AxiomPacketListener {
|
||||
public class SetFlySpeedPacketListener implements PluginMessageListener {
|
||||
|
||||
private static final Reflection.Method<ServerPlayer, Abilities> getAbilities = Reflection.getTypedMethod(ServerPlayer.class, Abilities.class);
|
||||
private static final Reflection.Field<Abilities, Float> flyingSpeed = Reflection.getField(Abilities.class, float.class);
|
||||
private final AxiomPaper plugin;
|
||||
public SetFlySpeedPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(@NotNull Player player, @NotNull ByteBuf buf) {
|
||||
float flySpeed = buf.readFloat();
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
float flySpeed = friendlyByteBuf.readFloat();
|
||||
|
||||
// Call event
|
||||
AxiomFlySpeedChangeEvent flySpeedChangeEvent = new AxiomFlySpeedChangeEvent(player, flySpeed);
|
||||
@ -25,7 +32,7 @@ public class SetFlySpeedPacketListener implements AxiomPacketListener {
|
||||
if (flySpeedChangeEvent.isCancelled()) return;
|
||||
|
||||
// Change flying speed
|
||||
flyingSpeed.set(getAbilities.invoke(AxiomPaper.convert(player)), flySpeed);
|
||||
((CraftPlayer)player).getHandle().getAbilities().setFlyingSpeed(flySpeed);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,26 +1,40 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.event.AxiomGameModeChangeEvent;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.level.GameType;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SetGamemodePacketListener implements AxiomPacketListener {
|
||||
public class SetGamemodePacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public SetGamemodePacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(Player player, ByteBuf buf) {
|
||||
@SuppressWarnings("deprecation")
|
||||
GameMode gameMode = GameMode.getByValue(buf.readByte());
|
||||
assert gameMode != null;
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
GameType gameType = GameType.byId(friendlyByteBuf.readByte());
|
||||
|
||||
// Call event
|
||||
AxiomGameModeChangeEvent gameModeChangeEvent = new AxiomGameModeChangeEvent(player, gameMode);
|
||||
AxiomGameModeChangeEvent gameModeChangeEvent = new AxiomGameModeChangeEvent(player, GameMode.getByValue(gameType.getId()));
|
||||
Bukkit.getPluginManager().callEvent(gameModeChangeEvent);
|
||||
if (gameModeChangeEvent.isCancelled()) return;
|
||||
|
||||
// Change gamemode
|
||||
player.setGameMode(gameMode);
|
||||
((CraftPlayer)player).getHandle().setGameMode(gameType);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,27 +1,40 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.persistence.ItemStackDataType;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.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 SetHotbarSlotPacketListener implements AxiomPacketListener {
|
||||
public class SetHotbarSlotPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public SetHotbarSlotPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(@NotNull Player player, @NotNull ByteBuf buf) {
|
||||
int index = buf.readByte();
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
int index = friendlyByteBuf.readByte();
|
||||
if (index < 0 || index >= 9*9) return;
|
||||
ItemStack stack = MojBuf.readItem(buf);
|
||||
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(AxiomConstants.axiomKey("slot_"+index), ItemStackDataType.INSTANCE, stack);
|
||||
hotbarItems.set(new NamespacedKey("axiom", "slot_"+index), ItemStackDataType.INSTANCE, CraftItemStack.asCraftMirror(nmsStack));
|
||||
container.set(AxiomConstants.HOTBAR_DATA, PersistentDataType.TAG_CONTAINER, hotbarItems);
|
||||
}
|
||||
|
||||
|
62
src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java
Normale Datei
62
src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java
Normale Datei
@ -0,0 +1,62 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.event.AxiomTimeChangeEvent;
|
||||
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.GameRules;
|
||||
import net.minecraft.world.level.Level;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SetTimePacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public SetTimePacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
ResourceKey<Level> key = friendlyByteBuf.readResourceKey(Registries.DIMENSION);
|
||||
Integer time = friendlyByteBuf.readNullable(FriendlyByteBuf::readInt);
|
||||
Boolean freezeTime = friendlyByteBuf.readNullable(FriendlyByteBuf::readBoolean);
|
||||
|
||||
if (time == null && freezeTime == null) return;
|
||||
|
||||
ServerLevel level = ((CraftWorld)player.getWorld()).getHandle();
|
||||
if (!level.dimension().equals(key)) return;
|
||||
|
||||
// Call modify world
|
||||
if (!this.plugin.canModifyWorld(player, player.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't allow on plot worlds
|
||||
if (PlotSquaredIntegration.isPlotWorld(player.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Call time change event
|
||||
AxiomTimeChangeEvent timeChangeEvent = new AxiomTimeChangeEvent(player, time, freezeTime);
|
||||
Bukkit.getPluginManager().callEvent(timeChangeEvent);
|
||||
if (timeChangeEvent.isCancelled()) return;
|
||||
|
||||
// Change time
|
||||
if (time != null) level.setDayTime(time);
|
||||
if (freezeTime != null) level.getGameRules().getRule(GameRules.RULE_DAYLIGHT).set(!freezeTime, null);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration;
|
||||
import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry;
|
||||
import com.moulberry.axiom.world_properties.server.ServerWorldPropertyHolder;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class SetWorldPropertyListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public SetWorldPropertyListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
ResourceLocation id = friendlyByteBuf.readResourceLocation();
|
||||
int type = friendlyByteBuf.readVarInt();
|
||||
byte[] data = friendlyByteBuf.readByteArray();
|
||||
int updateId = friendlyByteBuf.readVarInt();
|
||||
|
||||
// Call modify world
|
||||
if (!this.plugin.canModifyWorld(player, player.getWorld())) {
|
||||
sendAck(player, updateId);
|
||||
return;
|
||||
}
|
||||
|
||||
// Don't allow on plot worlds
|
||||
if (PlotSquaredIntegration.isPlotWorld(player.getWorld())) {
|
||||
sendAck(player, updateId);
|
||||
return;
|
||||
}
|
||||
|
||||
ServerWorldPropertiesRegistry registry = AxiomPaper.PLUGIN.getOrCreateWorldProperties(player.getWorld());
|
||||
if (registry == null) {
|
||||
sendAck(player, updateId);
|
||||
return;
|
||||
}
|
||||
|
||||
ServerWorldPropertyHolder<?> property = registry.getById(id);
|
||||
if (property != null && property.getType().getTypeId() == type) {
|
||||
property.update(player, player.getWorld(), data);
|
||||
}
|
||||
|
||||
sendAck(player, updateId);
|
||||
}
|
||||
|
||||
private void sendAck(Player player, int updateId) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
buf.writeVarInt(updateId);
|
||||
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
player.sendPluginMessage(AxiomPaper.PLUGIN, "axiom:ack_world_properties", bytes);
|
||||
}
|
||||
|
||||
}
|
123
src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java
Normale Datei
123
src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java
Normale Datei
@ -0,0 +1,123 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.NbtSanitization;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.entity.decoration.HangingEntity;
|
||||
import net.minecraft.world.entity.decoration.ItemFrame;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Rotation;
|
||||
import net.minecraft.world.phys.Vec3;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public class SpawnEntityPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public SpawnEntityPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
private record SpawnEntry(UUID newUuid, double x, double y, double z, float yaw, float pitch,
|
||||
@Nullable UUID copyFrom, CompoundTag tag) {
|
||||
public SpawnEntry(FriendlyByteBuf friendlyByteBuf) {
|
||||
this(friendlyByteBuf.readUUID(), friendlyByteBuf.readDouble(), friendlyByteBuf.readDouble(),
|
||||
friendlyByteBuf.readDouble(), friendlyByteBuf.readFloat(), friendlyByteBuf.readFloat(),
|
||||
friendlyByteBuf.readNullable(FriendlyByteBuf::readUUID), friendlyByteBuf.readNbt());
|
||||
}
|
||||
}
|
||||
|
||||
private static final Rotation[] ROTATION_VALUES = Rotation.values();
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!player.hasPermission("axiom.entity.*") && !player.hasPermission("axiom.entity.spawn")) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.plugin.canModifyWorld(player, player.getWorld())) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
List<SpawnEntry> entries = friendlyByteBuf.readList(SpawnEntry::new);
|
||||
|
||||
ServerLevel serverLevel = ((CraftWorld)player.getWorld()).getHandle();
|
||||
|
||||
List<String> whitelistedEntities = this.plugin.configuration.getStringList("whitelist-entities");
|
||||
List<String> blacklistedEntities = this.plugin.configuration.getStringList("blacklist-entities");
|
||||
|
||||
for (SpawnEntry entry : entries) {
|
||||
Vec3 position = new Vec3(entry.x, entry.y, entry.z);
|
||||
|
||||
BlockPos blockPos = BlockPos.containing(position);
|
||||
if (!Level.isInSpawnableBounds(blockPos)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (serverLevel.getEntity(entry.newUuid) != null) continue;
|
||||
|
||||
CompoundTag tag = entry.tag == null ? new CompoundTag() : entry.tag;
|
||||
|
||||
NbtSanitization.sanitizeEntity(tag);
|
||||
|
||||
if (entry.copyFrom != null) {
|
||||
Entity entityCopyFrom = serverLevel.getEntity(entry.copyFrom);
|
||||
if (entityCopyFrom != null) {
|
||||
CompoundTag compoundTag = new CompoundTag();
|
||||
if (entityCopyFrom.saveAsPassenger(compoundTag)) {
|
||||
compoundTag.remove("Dimension");
|
||||
tag = tag.merge(compoundTag);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!tag.contains("id")) continue;
|
||||
|
||||
Entity spawned = EntityType.loadEntityRecursive(tag, serverLevel, entity -> {
|
||||
String type = EntityType.getKey(entity.getType()).toString();
|
||||
if (!whitelistedEntities.isEmpty() && !whitelistedEntities.contains(type)) return null;
|
||||
if (blacklistedEntities.contains(type)) return null;
|
||||
|
||||
entity.setUUID(entry.newUuid);
|
||||
|
||||
if (entity instanceof HangingEntity hangingEntity) {
|
||||
float changedYaw = entry.yaw - entity.getYRot();
|
||||
int rotations = Math.round(changedYaw / 90);
|
||||
hangingEntity.rotate(ROTATION_VALUES[rotations & 3]);
|
||||
|
||||
if (entity instanceof ItemFrame itemFrame && itemFrame.getDirection().getAxis() == Direction.Axis.Y) {
|
||||
itemFrame.setRotation(itemFrame.getRotation() - Math.round(changedYaw / 45));
|
||||
}
|
||||
}
|
||||
|
||||
entity.moveTo(position.x, position.y, position.z, entry.yaw, entry.pitch);
|
||||
entity.setYHeadRot(entity.getYRot());
|
||||
|
||||
return entity;
|
||||
});
|
||||
|
||||
if (spawned != null) {
|
||||
serverLevel.tryAddFreshEntityWithPassengers(spawned);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,27 +1,41 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.AxiomConstants;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.persistence.ItemStackDataType;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.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 AxiomPacketListener {
|
||||
public class SwitchActiveHotbarPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public SwitchActiveHotbarPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(@NotNull Player player, @NotNull ByteBuf buf) {
|
||||
int oldHotbarIndex = buf.readByte();
|
||||
int activeHotbarIndex = buf.readByte();
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
int oldHotbarIndex = friendlyByteBuf.readByte();
|
||||
int activeHotbarIndex = friendlyByteBuf.readByte();
|
||||
|
||||
ItemStack[] hotbarItems = new ItemStack[9];
|
||||
for (int i=0; i<9; i++) {
|
||||
hotbarItems[i] = MojBuf.readItem(buf);
|
||||
hotbarItems[i] = CraftItemStack.asCraftMirror(friendlyByteBuf.readItem());
|
||||
}
|
||||
|
||||
PersistentDataContainer container = player.getPersistentDataContainer();
|
||||
@ -37,10 +51,10 @@ public class SwitchActiveHotbarPacketListener implements AxiomPacketListener {
|
||||
} else {
|
||||
stack = stack.clone();
|
||||
}
|
||||
containerHotbarItems.set(AxiomConstants.axiomKey("slot_"+index), ItemStackDataType.INSTANCE, stack);
|
||||
containerHotbarItems.set(new NamespacedKey("axiom", "slot_"+index), ItemStackDataType.INSTANCE, stack);
|
||||
}
|
||||
int index = activeHotbarIndex*9 + i;
|
||||
containerHotbarItems.set(AxiomConstants.axiomKey("slot_"+index), ItemStackDataType.INSTANCE, hotbarItems[i].clone());
|
||||
containerHotbarItems.set(new NamespacedKey("axiom", "slot_"+index), ItemStackDataType.INSTANCE, hotbarItems[i].clone());
|
||||
if (player.getGameMode() == GameMode.CREATIVE) player.getInventory().setItem(i, hotbarItems[i]);
|
||||
}
|
||||
|
||||
|
@ -1,27 +1,66 @@
|
||||
package com.moulberry.axiom.packet;
|
||||
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.event.AxiomTeleportEvent;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import com.moulberry.axiom.event.AxiomUnknownTeleportEvent;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.world.level.Level;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.util.CraftNamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class TeleportPacketListener implements AxiomPacketListener {
|
||||
public class TeleportPacketListener implements PluginMessageListener {
|
||||
|
||||
private final AxiomPaper plugin;
|
||||
public TeleportPacketListener(AxiomPaper plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(@NotNull Player player, @NotNull ByteBuf buf) {
|
||||
World world = Bukkit.getWorld(MojBuf.readKey(buf));
|
||||
double x = buf.readDouble();
|
||||
double y = buf.readDouble();
|
||||
double z = buf.readDouble();
|
||||
float yRot = buf.readFloat();
|
||||
float xRot = buf.readFloat();
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) {
|
||||
if (!this.plugin.canUseAxiom(player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message));
|
||||
ResourceKey<Level> resourceKey = friendlyByteBuf.readResourceKey(Registries.DIMENSION);
|
||||
double x = friendlyByteBuf.readDouble();
|
||||
double y = friendlyByteBuf.readDouble();
|
||||
double z = friendlyByteBuf.readDouble();
|
||||
float yRot = friendlyByteBuf.readFloat();
|
||||
float xRot = friendlyByteBuf.readFloat();
|
||||
|
||||
// Prevent teleport based on config value
|
||||
boolean allowTeleportBetweenWorlds = this.plugin.configuration.getBoolean("allow-teleport-between-worlds");
|
||||
if (!allowTeleportBetweenWorlds && !((CraftPlayer)player).getHandle().serverLevel().dimension().equals(resourceKey)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Call unknown teleport event
|
||||
AxiomUnknownTeleportEvent preTeleportEvent = new AxiomUnknownTeleportEvent(player,
|
||||
CraftNamespacedKey.fromMinecraft(resourceKey.location()), x, y, z, yRot, xRot);
|
||||
Bukkit.getPluginManager().callEvent(preTeleportEvent);
|
||||
if (preTeleportEvent.isCancelled()) return;
|
||||
|
||||
// Get bukkit world
|
||||
NamespacedKey namespacedKey = new NamespacedKey(resourceKey.location().getNamespace(), resourceKey.location().getPath());
|
||||
World world = Bukkit.getWorld(namespacedKey);
|
||||
if (world == null) return;
|
||||
|
||||
// Prevent teleport based on config value
|
||||
if (!allowTeleportBetweenWorlds && world != player.getWorld()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Call event
|
||||
AxiomTeleportEvent teleportEvent = new AxiomTeleportEvent(player, new Location(world, x, y, z, yRot, xRot));
|
||||
Bukkit.getPluginManager().callEvent(teleportEvent);
|
||||
|
@ -1,8 +1,8 @@
|
||||
package com.moulberry.axiom.persistence;
|
||||
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import com.moulberry.axiom.buffer.MojBuf;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.inventory.CraftItemStack;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.persistence.CraftPersistentDataContainer;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.persistence.PersistentDataAdapterContext;
|
||||
import org.bukkit.persistence.PersistentDataContainer;
|
||||
@ -10,9 +10,9 @@ import org.bukkit.persistence.PersistentDataType;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ItemStackDataType implements PersistentDataType<PersistentDataContainer, ItemStack> {
|
||||
private ItemStackDataType() {}
|
||||
|
||||
public static final ItemStackDataType INSTANCE = new ItemStackDataType();
|
||||
public static ItemStackDataType INSTANCE = new ItemStackDataType();
|
||||
private ItemStackDataType() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Class<PersistentDataContainer> getPrimitiveType() {
|
||||
@ -24,23 +24,23 @@ public class ItemStackDataType implements PersistentDataType<PersistentDataConta
|
||||
return ItemStack.class;
|
||||
}
|
||||
|
||||
private static final Class<PersistentDataContainer> CraftPersistentDataContainer = Reflection.getClass("org.bukkit.craftbukkit.persistence.CraftPersistentDataContainer");
|
||||
private static final Reflection.Method<PersistentDataContainer, Void> putAll = Reflection.getMethod(CraftPersistentDataContainer, "putAll", CompoundTag.class);
|
||||
private static final Reflection.Method<PersistentDataContainer, CompoundTag> toTagCompound = Reflection.getTypedMethod(CraftPersistentDataContainer, "toTagCompound", CompoundTag.class);
|
||||
|
||||
private static final Reflection.Method<net.minecraft.world.item.ItemStack, CompoundTag> save = Reflection.getTypedMethod(net.minecraft.world.item.ItemStack.class, CompoundTag.class, CompoundTag.class);
|
||||
@Override
|
||||
public @NotNull PersistentDataContainer toPrimitive(@NotNull ItemStack stack, @NotNull PersistentDataAdapterContext context) {
|
||||
CompoundTag tag = save.invoke(MojBuf.toMojang(stack), new CompoundTag());
|
||||
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();
|
||||
putAll.invoke(container, tag);
|
||||
((CraftPersistentDataContainer)container).putAll(tag);
|
||||
return container;
|
||||
}
|
||||
|
||||
private static final Reflection.Method<net.minecraft.world.item.ItemStack, net.minecraft.world.item.ItemStack> of = Reflection.getTypedMethod(net.minecraft.world.item.ItemStack.class, net.minecraft.world.item.ItemStack.class, CompoundTag.class);
|
||||
@Override
|
||||
public @NotNull ItemStack fromPrimitive(@NotNull PersistentDataContainer primitive, @NotNull PersistentDataAdapterContext context) {
|
||||
return MojBuf.toBukkit(of.invoke(null, toTagCompound.invoke(primitive)));
|
||||
CompoundTag tag = ((CraftPersistentDataContainer)primitive).toTagCompound();
|
||||
net.minecraft.world.item.ItemStack nmsStack = net.minecraft.world.item.ItemStack.of(tag);
|
||||
|
||||
return CraftItemStack.asCraftMirror(nmsStack);
|
||||
}
|
||||
}
|
||||
|
@ -7,9 +7,9 @@ import java.nio.ByteBuffer;
|
||||
import java.util.UUID;
|
||||
|
||||
public class UUIDDataType implements PersistentDataType<byte[], UUID> {
|
||||
private UUIDDataType() {}
|
||||
|
||||
public static final UUIDDataType INSTANCE = new UUIDDataType();
|
||||
public static UUIDDataType INSTANCE = new UUIDDataType();
|
||||
private UUIDDataType() {
|
||||
}
|
||||
|
||||
public Class<byte[]> getPrimitiveType() {
|
||||
return byte[].class;
|
||||
|
@ -1,47 +0,0 @@
|
||||
package com.moulberry.axiom.version;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.server.level.ThreadedLevelLightEngine;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.ChunkPos;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import org.bukkit.Chunk;
|
||||
|
||||
import java.util.BitSet;
|
||||
import java.util.Set;
|
||||
|
||||
public class Version19R2 implements VersionWrapper {
|
||||
|
||||
private static final Reflection.Method<BlockState, Integer> getLightBlock = Reflection.getTypedMethod(BlockState.class, int.class, BlockGetter.class, BlockPos.class);
|
||||
private static final Reflection.Field<BlockState, Integer> lightEmission = Reflection.getField(BlockState.class, int.class);
|
||||
private static final Reflection.Field<BlockState, Boolean> useShapeForLightOcclusion = Reflection.getField(BlockState.class, boolean.class);
|
||||
@Override
|
||||
public boolean hasDifferentLightProperties(BlockGetter chunk, BlockPos pos, BlockState old, BlockState state) {
|
||||
return getLightBlock.invoke(old, chunk, pos) != getLightBlock.invoke(state, chunk, pos) || lightEmission.get(old) != lightEmission.get(state) || useShapeForLightOcclusion.get(old) || useShapeForLightOcclusion.get(state);
|
||||
}
|
||||
|
||||
private static final Class<?> LevelLightEngine = Reflection.getClass("net.minecraft.world.level.lighting.LightEngine");
|
||||
private static final Reflection.Constructor<ClientboundLevelChunkWithLightPacket> newClientboundLevelChunkWithLightPacket = Reflection.getConstructor(
|
||||
ClientboundLevelChunkWithLightPacket.class,
|
||||
LevelChunk.class, LevelLightEngine, BitSet.class, BitSet.class, boolean.class
|
||||
);
|
||||
@Override
|
||||
public void publishBiomeChange(ServerLevel level, Set<Chunk> chunks) {
|
||||
ThreadedLevelLightEngine lightEngine = AxiomPaper.getLightEngine(AxiomPaper.getChunkSource(level));
|
||||
|
||||
for (Chunk chunk : chunks) {
|
||||
ChunkPos chunkPos = new ChunkPos(chunk.getX(), chunk.getZ());
|
||||
LevelChunk nativeChunk = AxiomPaper.getChunk(level, chunk.getX(), chunk.getZ());
|
||||
ClientboundLevelChunkWithLightPacket packet = newClientboundLevelChunkWithLightPacket.newInstance(nativeChunk, lightEngine, null, null, true);
|
||||
for (ServerPlayer player : AxiomPaper.getPlayersSeeingChunk(level, chunkPos)) {
|
||||
AxiomPaper.sendPacket(player, packet);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package com.moulberry.axiom.version;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.protocol.game.ClientboundChunksBiomesPacket;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.server.level.ServerPlayer;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.ChunkPos;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.minecraft.world.level.lighting.LightEngine;
|
||||
import org.bukkit.Chunk;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class Version20R1 implements VersionWrapper {
|
||||
|
||||
private static final Reflection.Method<LightEngine, Boolean> hasDifferentLightProperties = Reflection.getTypedMethod(LightEngine.class, boolean.class, BlockGetter.class, BlockPos.class, BlockState.class, BlockState.class);
|
||||
@Override
|
||||
public boolean hasDifferentLightProperties(BlockGetter chunk, BlockPos pos, BlockState old, BlockState state) {
|
||||
return hasDifferentLightProperties.invoke(null, chunk, pos, old, state);
|
||||
}
|
||||
|
||||
private static final Reflection.Method<ClientboundChunksBiomesPacket, ClientboundChunksBiomesPacket> forChunks = Reflection.getTypedMethod(ClientboundChunksBiomesPacket.class, ClientboundChunksBiomesPacket.class, List.class);
|
||||
@Override
|
||||
public void publishBiomeChange(ServerLevel level, Set<Chunk> chunks) {
|
||||
HashMap<ServerPlayer, List<LevelChunk>> playerChunks = new HashMap<>();
|
||||
for (Chunk chunk : chunks) {
|
||||
ChunkPos chunkPos = new ChunkPos(chunk.getX(), chunk.getZ());
|
||||
LevelChunk nativeChunk = AxiomPaper.getChunk(level, chunk.getX(), chunk.getZ());
|
||||
for (ServerPlayer player : AxiomPaper.getPlayersSeeingChunk(level, chunkPos)) {
|
||||
playerChunks.computeIfAbsent(player, p -> new ArrayList<>()).add(nativeChunk);
|
||||
}
|
||||
}
|
||||
playerChunks.forEach((serverPlayer, list) -> AxiomPaper.sendPacket(serverPlayer, forChunks.invoke(null, list)));
|
||||
}
|
||||
}
|
@ -1,18 +0,0 @@
|
||||
package com.moulberry.axiom.version;
|
||||
|
||||
import com.moulberry.axiom.Reflection;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import org.bukkit.Chunk;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface VersionWrapper {
|
||||
VersionWrapper impl = Reflection.VERSION > 1902 ? new Version20R1() : new Version19R2();
|
||||
|
||||
boolean hasDifferentLightProperties(BlockGetter chunk, BlockPos pos, BlockState old, BlockState state);
|
||||
void publishBiomeChange(ServerLevel level, Set<Chunk> chunks);
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.moulberry.axiom.world_properties;
|
||||
|
||||
import com.moulberry.axiom.world_properties.server.PropertyUpdateResult;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface PropertyUpdateHandler<T> {
|
||||
|
||||
/**
|
||||
* @param player the player that updated the property
|
||||
* @param world the world for which the property has been updated
|
||||
* @param value the new value of the property
|
||||
* @return {@link PropertyUpdateResult}
|
||||
*/
|
||||
PropertyUpdateResult update(Player player, World world, T value);
|
||||
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.moulberry.axiom.world_properties;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
|
||||
public record WorldPropertyCategory(String name, boolean localizeName) {
|
||||
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeUtf(this.name);
|
||||
friendlyByteBuf.writeBoolean(this.localizeName);
|
||||
}
|
||||
|
||||
public static WorldPropertyCategory read(FriendlyByteBuf friendlyByteBuf) {
|
||||
return new WorldPropertyCategory(
|
||||
friendlyByteBuf.readUtf(),
|
||||
friendlyByteBuf.readBoolean()
|
||||
);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,146 @@
|
||||
package com.moulberry.axiom.world_properties;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.Blocks;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public abstract class WorldPropertyDataType<T> {
|
||||
|
||||
public abstract int getTypeId();
|
||||
public abstract byte[] serialize(T value);
|
||||
public abstract T deserialize(byte[] bytes);
|
||||
|
||||
public static WorldPropertyDataType<Boolean> BOOLEAN = new WorldPropertyDataType<>() {
|
||||
@Override
|
||||
public int getTypeId() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Boolean value) {
|
||||
return new byte[] { value != null && value ? (byte)1 : (byte)0 };
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean deserialize(byte[] bytes) {
|
||||
return bytes[0] != 0;
|
||||
}
|
||||
};
|
||||
|
||||
public static WorldPropertyDataType<Integer> INTEGER = new WorldPropertyDataType<>() {
|
||||
@Override
|
||||
public int getTypeId() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Integer value) {
|
||||
if (value == null) value = 0;
|
||||
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(8));
|
||||
buf.writeVarInt(value);
|
||||
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer deserialize(byte[] bytes) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(bytes));
|
||||
return buf.readVarInt();
|
||||
}
|
||||
};
|
||||
|
||||
public static WorldPropertyDataType<String> STRING = new WorldPropertyDataType<>() {
|
||||
@Override
|
||||
public int getTypeId() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(String value) {
|
||||
if (value == null) value = "";
|
||||
return value.getBytes(StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String deserialize(byte[] bytes) {
|
||||
return new String(bytes, StandardCharsets.UTF_8);
|
||||
}
|
||||
};
|
||||
|
||||
public static WorldPropertyDataType<Item> ITEM = new WorldPropertyDataType<>() {
|
||||
@Override
|
||||
public int getTypeId() {
|
||||
return 3;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Item value) {
|
||||
if (value == null) value = Items.AIR;
|
||||
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(8));
|
||||
buf.writeId(BuiltInRegistries.ITEM, value);
|
||||
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Item deserialize(byte[] bytes) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(bytes));
|
||||
return buf.readById(BuiltInRegistries.ITEM);
|
||||
}
|
||||
};
|
||||
|
||||
public static WorldPropertyDataType<Block> BLOCK = new WorldPropertyDataType<>() {
|
||||
@Override
|
||||
public int getTypeId() {
|
||||
return 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Block value) {
|
||||
if (value == null) value = Blocks.AIR;
|
||||
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer(8));
|
||||
buf.writeId(BuiltInRegistries.BLOCK, value);
|
||||
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block deserialize(byte[] bytes) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(bytes));
|
||||
return buf.readById(BuiltInRegistries.BLOCK);
|
||||
}
|
||||
};
|
||||
|
||||
public static WorldPropertyDataType<Void> EMPTY = new WorldPropertyDataType<>() {
|
||||
@Override
|
||||
public int getTypeId() {
|
||||
return 5;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] serialize(Void value) {
|
||||
return new byte[0];
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void deserialize(byte[] bytes) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
package com.moulberry.axiom.world_properties;
|
||||
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface WorldPropertyWidgetType<T> {
|
||||
|
||||
WorldPropertyDataType<T> dataType();
|
||||
void write(FriendlyByteBuf friendlyByteBuf);
|
||||
|
||||
WorldPropertyWidgetType<Boolean> CHECKBOX = new WorldPropertyWidgetType<>() {
|
||||
@Override
|
||||
public WorldPropertyDataType<Boolean> dataType() {
|
||||
return WorldPropertyDataType.BOOLEAN;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeVarInt(0);
|
||||
}
|
||||
};
|
||||
|
||||
record Slider(int min, int max) implements WorldPropertyWidgetType<Integer> {
|
||||
@Override
|
||||
public WorldPropertyDataType<Integer> dataType() {
|
||||
return WorldPropertyDataType.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeVarInt(1);
|
||||
friendlyByteBuf.writeInt(this.min);
|
||||
friendlyByteBuf.writeInt(this.max);
|
||||
}
|
||||
}
|
||||
|
||||
WorldPropertyWidgetType<String> TEXTBOX = new WorldPropertyWidgetType<>() {
|
||||
@Override
|
||||
public WorldPropertyDataType<String> dataType() {
|
||||
return WorldPropertyDataType.STRING;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeVarInt(2);
|
||||
}
|
||||
};
|
||||
|
||||
WorldPropertyWidgetType<Integer> TIME = new WorldPropertyWidgetType<>() {
|
||||
@Override
|
||||
public WorldPropertyDataType<Integer> dataType() {
|
||||
return WorldPropertyDataType.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeVarInt(3);
|
||||
}
|
||||
};
|
||||
|
||||
WorldPropertyWidgetType<Void> BUTTON = new WorldPropertyWidgetType<>() {
|
||||
@Override
|
||||
public WorldPropertyDataType<Void> dataType() {
|
||||
return WorldPropertyDataType.EMPTY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeVarInt(4);
|
||||
}
|
||||
};
|
||||
|
||||
record ButtonArray(List<String> otherButtons) implements WorldPropertyWidgetType<Integer> {
|
||||
@Override
|
||||
public WorldPropertyDataType<Integer> dataType() {
|
||||
return WorldPropertyDataType.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeVarInt(5);
|
||||
friendlyByteBuf.writeCollection(this.otherButtons, FriendlyByteBuf::writeUtf);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.moulberry.axiom.world_properties.server;
|
||||
|
||||
public enum PropertyUpdateResult {
|
||||
|
||||
UPDATE_AND_SYNC(true, true),
|
||||
UPDATE_WITHOUT_SYNC(true, false),
|
||||
CANCEL(false, false);
|
||||
|
||||
private final boolean update;
|
||||
private final boolean sync;
|
||||
|
||||
PropertyUpdateResult(boolean update, boolean sync) {
|
||||
this.update = update;
|
||||
this.sync = sync;
|
||||
}
|
||||
|
||||
public boolean isUpdate() {
|
||||
return this.update;
|
||||
}
|
||||
|
||||
public boolean isSync() {
|
||||
return this.sync;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,114 @@
|
||||
package com.moulberry.axiom.world_properties.server;
|
||||
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyCategory;
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import org.bukkit.GameRule;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.CraftWorld;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class ServerWorldPropertiesRegistry {
|
||||
|
||||
private final LinkedHashMap<WorldPropertyCategory, List<ServerWorldPropertyHolder<?>>> propertyList = new LinkedHashMap<>();
|
||||
private final Map<ResourceLocation, ServerWorldPropertyHolder<?>> propertyMap = new HashMap<>();
|
||||
private final World world;
|
||||
|
||||
public ServerWorldPropertiesRegistry(World world) {
|
||||
this.world = world;
|
||||
this.registerDefault();
|
||||
}
|
||||
|
||||
public ServerWorldPropertyHolder<?> getById(ResourceLocation resourceLocation) {
|
||||
return propertyMap.get(resourceLocation);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void addCategory(WorldPropertyCategory category, List<ServerWorldPropertyBase<?>> properties) {
|
||||
List<ServerWorldPropertyHolder<?>> holders = new ArrayList<>();
|
||||
for (ServerWorldPropertyBase<?> property : properties) {
|
||||
Object defaultValue = property.getDefaultValue(this.world);
|
||||
holders.add(new ServerWorldPropertyHolder<>(defaultValue, (ServerWorldPropertyBase<Object>) property));
|
||||
}
|
||||
|
||||
this.propertyList.put(category, holders);
|
||||
|
||||
for (ServerWorldPropertyHolder<?> holder : holders) {
|
||||
ResourceLocation id = holder.getId();
|
||||
if (this.propertyMap.containsKey(id)) {
|
||||
throw new RuntimeException("Duplicate property: " + id);
|
||||
}
|
||||
this.propertyMap.put(id, holder);
|
||||
}
|
||||
}
|
||||
|
||||
public void registerFor(Plugin plugin, Player bukkitPlayer) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
|
||||
buf.writeVarInt(this.propertyList.size());
|
||||
|
||||
for (Map.Entry<WorldPropertyCategory, List<ServerWorldPropertyHolder<?>>> entry : this.propertyList.entrySet()) {
|
||||
entry.getKey().write(buf);
|
||||
buf.writeCollection(entry.getValue(), (buffer, p) -> p.write(buffer));
|
||||
}
|
||||
|
||||
byte[] bytes = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, bytes);
|
||||
bukkitPlayer.sendPluginMessage(plugin, "axiom:register_world_properties", bytes);
|
||||
}
|
||||
|
||||
private static final ServerWorldProperty<Integer> TIME = new ServerWorldProperty<>(
|
||||
new NamespacedKey("axiom", "time"),
|
||||
"axiom.editorui.window.world_properties.time",
|
||||
true, WorldPropertyWidgetType.TIME, world -> 0,
|
||||
(player, w, integer) -> PropertyUpdateResult.UPDATE_WITHOUT_SYNC
|
||||
);
|
||||
|
||||
public static final ServerWorldProperty<Boolean> PAUSE_WEATHER = new ServerWorldProperty<>(
|
||||
new NamespacedKey("axiom", "pause_weather"),
|
||||
"axiom.editorui.window.world_properties.pause_weather",
|
||||
true, WorldPropertyWidgetType.CHECKBOX, world -> !world.getGameRuleValue(GameRule.DO_WEATHER_CYCLE),
|
||||
(player, world, bool) -> {
|
||||
world.setGameRule(GameRule.DO_WEATHER_CYCLE, !bool);
|
||||
return PropertyUpdateResult.UPDATE_WITHOUT_SYNC;
|
||||
}
|
||||
);
|
||||
|
||||
private static final ServerWorldProperty<Integer> WEATHER_TYPE = new ServerWorldProperty<>(
|
||||
new NamespacedKey("axiom", "weather_type"),
|
||||
"axiom.editorui.window.world_properties.clear_weather",
|
||||
true, new WorldPropertyWidgetType.ButtonArray(
|
||||
List.of("axiom.editorui.window.world_properties.rain_weather", "axiom.editorui.window.world_properties.thunder_weather")
|
||||
), world -> 0, (player, world, index) -> {
|
||||
ServerLevel serverLevel = ((CraftWorld)world).getHandle();
|
||||
if (index == 0) {
|
||||
serverLevel.setWeatherParameters(ServerLevel.RAIN_DELAY.sample(serverLevel.random), 0, false, false);
|
||||
} else if (index == 1) {
|
||||
serverLevel.setWeatherParameters(0, ServerLevel.RAIN_DURATION.sample(serverLevel.random), true, false);
|
||||
} else if (index == 2) {
|
||||
serverLevel.setWeatherParameters(0, ServerLevel.THUNDER_DURATION.sample(serverLevel.random), true, true);
|
||||
}
|
||||
return PropertyUpdateResult.UPDATE_WITHOUT_SYNC;
|
||||
});
|
||||
|
||||
public void registerDefault() {
|
||||
// Time
|
||||
WorldPropertyCategory timeCategory = new WorldPropertyCategory("axiom.editorui.window.world_properties.time", true);
|
||||
|
||||
this.addCategory(timeCategory, List.of(TIME));
|
||||
|
||||
// Weather
|
||||
WorldPropertyCategory weatherCategory = new WorldPropertyCategory("axiom.editorui.window.world_properties.weather",
|
||||
true);
|
||||
|
||||
this.addCategory(weatherCategory, List.of(PAUSE_WEATHER, WEATHER_TYPE));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.moulberry.axiom.world_properties.server;
|
||||
|
||||
import com.moulberry.axiom.world_properties.PropertyUpdateHandler;
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ServerWorldProperty<T> extends ServerWorldPropertyBase<T> {
|
||||
|
||||
private final Function<World, T> defaultValueFunction;
|
||||
private final PropertyUpdateHandler<T> handler;
|
||||
|
||||
public ServerWorldProperty(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType<T> widget,
|
||||
Function<World, T> defaultValueFunction, PropertyUpdateHandler<T> handler) {
|
||||
super(id, name, localizeName, widget);
|
||||
this.defaultValueFunction = defaultValueFunction;
|
||||
this.handler = handler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getDefaultValue(World world) {
|
||||
return this.defaultValueFunction.apply(world);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PropertyUpdateResult handleUpdateProperty(Player player, World world, T value) {
|
||||
return this.handler.update(player, world, value);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.moulberry.axiom.world_properties.server;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyDataType;
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyWidgetType;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.craftbukkit.v1_20_R1.util.CraftNamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class ServerWorldPropertyBase<T> {
|
||||
|
||||
private final ResourceLocation id;
|
||||
/*package-private*/ final String name;
|
||||
/*package-private*/ final boolean localizeName;
|
||||
/*package-private*/ WorldPropertyWidgetType<T> widget;
|
||||
|
||||
public ServerWorldPropertyBase(NamespacedKey id, String name, boolean localizeName, WorldPropertyWidgetType<T> widget) {
|
||||
this.id = CraftNamespacedKey.toMinecraft(id);
|
||||
this.name = name;
|
||||
this.localizeName = localizeName;
|
||||
this.widget = widget;
|
||||
}
|
||||
|
||||
public abstract T getDefaultValue(World world);
|
||||
|
||||
public abstract PropertyUpdateResult handleUpdateProperty(Player player, World world, T value);
|
||||
|
||||
public ResourceLocation getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public WorldPropertyDataType<T> getType() {
|
||||
return this.widget.dataType();
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean setValueWithoutSyncing(World world, T value) {
|
||||
ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world);
|
||||
if (properties != null) {
|
||||
ServerWorldPropertyHolder<?> property = properties.getById(this.id);
|
||||
if (property != null && property.getProperty() == this) {
|
||||
((ServerWorldPropertyHolder<T>)property).setValueWithoutSyncing(value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean setValue(World world, T value) {
|
||||
ServerWorldPropertiesRegistry properties = AxiomPaper.PLUGIN.getWorldPropertiesIfPresent(world);
|
||||
if (properties != null) {
|
||||
ServerWorldPropertyHolder<?> property = properties.getById(this.id);
|
||||
if (property != null && property.getProperty() == this) {
|
||||
((ServerWorldPropertyHolder<T>)property).setValue(world, value);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,90 @@
|
||||
package com.moulberry.axiom.world_properties.server;
|
||||
|
||||
import com.moulberry.axiom.AxiomPaper;
|
||||
import com.moulberry.axiom.world_properties.WorldPropertyDataType;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ServerWorldPropertyHolder<T> {
|
||||
|
||||
private T value;
|
||||
private ServerWorldPropertyBase<T> property;
|
||||
private boolean unsyncedValue = false;
|
||||
|
||||
public ServerWorldPropertyHolder(T value, ServerWorldPropertyBase<T> property) {
|
||||
this.value = value;
|
||||
this.property = property;
|
||||
}
|
||||
|
||||
public ResourceLocation getId() {
|
||||
return this.property.getId();
|
||||
}
|
||||
|
||||
public WorldPropertyDataType<T> getType() {
|
||||
return this.property.widget.dataType();
|
||||
}
|
||||
|
||||
public ServerWorldPropertyBase<T> getProperty() {
|
||||
return property;
|
||||
}
|
||||
|
||||
public void update(Player player, World world, byte[] data) {
|
||||
T newValue = this.property.widget.dataType().deserialize(data);
|
||||
|
||||
PropertyUpdateResult result = this.property.handleUpdateProperty(player, world, newValue);
|
||||
|
||||
if (result.isUpdate()) {
|
||||
this.value = newValue;
|
||||
|
||||
if (result.isSync()) {
|
||||
this.sync(world);
|
||||
} else {
|
||||
this.unsyncedValue = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setValueWithoutSyncing(T value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public void setValue(World world, T value) {
|
||||
boolean sync = this.unsyncedValue || !Objects.equals(value, this.value);
|
||||
this.value = value;
|
||||
if (sync) {
|
||||
this.sync(world);
|
||||
}
|
||||
}
|
||||
|
||||
public void sync(World world) {
|
||||
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
|
||||
buf.writeResourceLocation(this.getId());
|
||||
buf.writeVarInt(this.property.widget.dataType().getTypeId());
|
||||
buf.writeByteArray(this.property.widget.dataType().serialize(this.value));
|
||||
|
||||
byte[] message = new byte[buf.writerIndex()];
|
||||
buf.getBytes(0, message);
|
||||
for (Player player : world.getPlayers()) {
|
||||
if (AxiomPaper.PLUGIN.activeAxiomPlayers.contains(player.getUniqueId())) {
|
||||
player.sendPluginMessage(AxiomPaper.PLUGIN, "axiom:set_world_property", message);
|
||||
}
|
||||
}
|
||||
|
||||
this.unsyncedValue = false;
|
||||
}
|
||||
|
||||
public void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeResourceLocation(this.getId());
|
||||
friendlyByteBuf.writeUtf(this.property.name);
|
||||
friendlyByteBuf.writeBoolean(this.property.localizeName);
|
||||
this.property.widget.write(friendlyByteBuf);
|
||||
friendlyByteBuf.writeByteArray(this.property.widget.dataType().serialize(this.value));
|
||||
}
|
||||
|
||||
}
|
78
src/main/resources/config.yml
Normale Datei
78
src/main/resources/config.yml
Normale Datei
@ -0,0 +1,78 @@
|
||||
# Max chunk sends per tick (per-world), 0 = no limit
|
||||
max-chunk-sends-per-tick: 0
|
||||
|
||||
# Max chunk relights per tick (per-world), 0 = no limit
|
||||
max-chunk-relights-per-tick: 0
|
||||
|
||||
# Maximum distance from which players can load chunks
|
||||
# Set to 0 to only allow editing within render distance
|
||||
max-chunk-load-distance: 128
|
||||
|
||||
# Whether players are allowed to teleport between worlds using views
|
||||
allow-teleport-between-worlds: true
|
||||
|
||||
# Action to take when a user with an incompatible Minecraft version or Axiom version joins
|
||||
# Valid actions are 'kick', 'warn' and 'ignore'
|
||||
# 'warn' will give the player a warning and disable Axiom
|
||||
# Using 'ignore' may result in corruption and is only provided for debugging purposes
|
||||
incompatible-data-version: "warn"
|
||||
unsupported-axiom-version: "warn"
|
||||
client-doesnt-support-restrictions: "ignore"
|
||||
|
||||
# Maximum packet size. Must not be less than 32767
|
||||
max-block-buffer-packet-size: 0x100000
|
||||
|
||||
# Regex for whitelisting worlds
|
||||
# The world can only be modified if the regex matches the world's name
|
||||
whitelist-world-regex: null
|
||||
|
||||
# Regex for blacklisting worlds
|
||||
# If the regex matches the world's name, the world can't be modified
|
||||
blacklist-world-regex: null
|
||||
|
||||
# Block buffer rate-limit (in chunk sections per second), 0 = no limit
|
||||
block-buffer-rate-limit: 0
|
||||
|
||||
# Log large block buffer changes
|
||||
log-large-block-buffer-changes: false
|
||||
|
||||
# Whitelist entities that can be spawned/manipulated/deleted by the client
|
||||
whitelist-entities:
|
||||
# - "minecraft:item_display"
|
||||
# - "minecraft:block_display"
|
||||
# - "minecraft:text_display"
|
||||
# - "minecraft:painting"
|
||||
# - "minecraft:armor_stand"
|
||||
# - "minecraft:item_frame"
|
||||
# - "minecraft:glow_item_frame"
|
||||
|
||||
# Blacklist entities that can be spawned/manipulated/deleted by the client
|
||||
blacklist-entities:
|
||||
# - "minecraft:ender_dragon"
|
||||
|
||||
# True allows players to see/manipulate marker entities
|
||||
send-markers: false
|
||||
|
||||
# Disallowed blocks
|
||||
disallowed-blocks:
|
||||
# - "minecraft:wheat"
|
||||
# - "minecraft:oak_stairs[waterlogged=true]"
|
||||
|
||||
# Toggles for individual packet handlers. May break certain features
|
||||
packet-handlers:
|
||||
hello: true
|
||||
set-gamemode: true
|
||||
set-fly-speed: true
|
||||
set-world-time: true
|
||||
set-world-property: true
|
||||
set-single-block: true
|
||||
set-hotbar-slot: true
|
||||
switch-active-hotbar: true
|
||||
teleport: true
|
||||
set-editor-views: true
|
||||
request-chunk-data: true
|
||||
set-buffer: true
|
||||
spawn-entity: true
|
||||
manipulate-entity: true
|
||||
delete-entity: true
|
||||
marker-nbt-request: true
|
@ -2,15 +2,27 @@ name: $name
|
||||
version: $version
|
||||
main: com.moulberry.axiom.AxiomPaper
|
||||
description: $description
|
||||
softdepend:
|
||||
- ViaVersion
|
||||
- WorldGuard
|
||||
authors:
|
||||
- Moulberry
|
||||
- Lixfel
|
||||
api-version: "1.17"
|
||||
api-version: "$apiVersion"
|
||||
permissions:
|
||||
axiom.*:
|
||||
description: Allows use of all Axiom features
|
||||
description: Allows use of all default Axiom features
|
||||
default: op
|
||||
|
||||
axiom.entity.*:
|
||||
description: Allows use of all entity-related features (spawning, manipulating, deleting)
|
||||
default: op
|
||||
axiom.entity.spawn:
|
||||
description: Allows entity spawning
|
||||
axiom.entity.manipulate:
|
||||
description: Allows entity manipulation
|
||||
axiom.entity.delete:
|
||||
description: Allows entity deletion
|
||||
|
||||
axiom.allow_copying_other_plots:
|
||||
description: This permission allows users to copy other user's plots
|
||||
default: true
|
||||
axiom.can_import_blocks:
|
||||
description: Allows players to import schematics/blueprints into Axiom
|
||||
default: true
|
||||
|
@ -6,7 +6,7 @@ build:
|
||||
- "JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64 ./gradlew --stop"
|
||||
|
||||
artifacts:
|
||||
"/binarys/AxiomPaper.jar": "build/libs/AxiomPaper-1.2.1.jar"
|
||||
"/binarys/AxiomPaper.jar": "build/libs/AxiomPaper-1.5.8.jar"
|
||||
|
||||
release:
|
||||
- "mvn deploy:deploy-file -DgroupId=de.steamwar -DartifactId=axiompaper -Dversion=RELEASE -Dpackaging=jar -Dfile=build/libs/AxiomPaper-1.2.1.jar -Durl=file:///var/www/html/maven/"
|
||||
- "mvn deploy:deploy-file -DgroupId=de.steamwar -DartifactId=axiompaper -Dversion=RELEASE -Dpackaging=jar -Dfile=build/libs/AxiomPaper-1.5.8.jar -Durl=file:///var/www/html/maven/"
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren