Skip to content

Commit

Permalink
Use integrated world managers for decorated pot animation
Browse files Browse the repository at this point in the history
  • Loading branch information
Camotoy committed Jun 19, 2024
1 parent 6884a0f commit 2c47330
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -39,10 +40,12 @@
import net.minecraft.world.level.block.entity.BannerBlockEntity;
import net.minecraft.world.level.block.entity.BannerPatternLayers;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.DecoratedPotBlockEntity;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.status.ChunkStatus;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.geyser.level.GeyserWorldManager;
import org.geysermc.geyser.network.GameProtocol;
import org.geysermc.geyser.platform.mod.GeyserModBootstrap;
Expand All @@ -56,6 +59,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

public class GeyserModWorldManager extends GeyserWorldManager {

Expand Down Expand Up @@ -161,6 +165,27 @@ public CompletableFuture<org.geysermc.mcprotocollib.protocol.data.game.item.comp
return future;
}

@Override
public void getDecoratedPotData(GeyserSession session, Vector3i pos, Consumer<List<String>> apply) {
server.execute(() -> {
ServerPlayer player = getPlayer(session);
if (player == null) {
return;
}

BlockPos blockPos = new BlockPos(pos.getX(), pos.getY(), pos.getZ());
// Don't create a new block entity if invalid
//noinspection resource - level() is just a getter
BlockEntity blockEntity = player.level().getChunkAt(blockPos).getBlockEntity(blockPos);
if (blockEntity instanceof DecoratedPotBlockEntity pot) {
List<String> sherds = pot.getDecorations().ordered()
.stream().map(item -> BuiltInRegistries.ITEM.getKey(item).toString())
.toList();
apply.accept(sherds);
}
});
}

private ServerPlayer getPlayer(GeyserSession session) {
return server.getPlayerList().getPlayer(session.getPlayerEntity().getUuid());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.DecoratedPot;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.cloudburstmc.math.vector.Vector3i;
import org.geysermc.erosion.bukkit.BukkitUtils;
import org.geysermc.erosion.bukkit.PickBlockUtils;
import org.geysermc.erosion.bukkit.SchedulerUtils;
import org.geysermc.geyser.GeyserImpl;
Expand All @@ -43,8 +46,10 @@
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
import org.geysermc.mcprotocollib.protocol.data.game.item.component.DataComponents;

import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;

/**
* The base world manager to use when there is no supported NMS revision
Expand Down Expand Up @@ -146,6 +151,21 @@ public boolean hasPermission(GeyserSession session, String permission) {
return future.thenApply(RAW_TRANSFORMER);
}

public void getDecoratedPotData(GeyserSession session, Vector3i pos, Consumer<List<String>> apply) {
Player bukkitPlayer;
if ((bukkitPlayer = Bukkit.getPlayer(session.getPlayerEntity().getUuid())) == null) {
return;
}
Block block = bukkitPlayer.getWorld().getBlockAt(pos.getX(), pos.getY(), pos.getZ());
SchedulerUtils.runTask(this.plugin, () -> {
var state = BukkitUtils.getBlockState(block);
if (!(state instanceof DecoratedPot pot)) {
return;
}
apply.accept(pot.getShards().stream().map(material -> material.getKey().toString()).toList());
}, block);
}

/**
* This should be set to true if we are post-1.13 but before the latest version, and we should convert the old block state id
* to the current one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@
import org.geysermc.mcprotocollib.protocol.data.game.setting.Difficulty;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -209,6 +211,13 @@ public void setDifficulty(GeyserSession session, Difficulty difficulty) {
return CompletableFuture.completedFuture(null);
}

/**
* Retrieves decorated pot sherds from the server. Used to ensure the data is not erased on animation sent
* through the BlockEntityDataPacket.
*/
public void getDecoratedPotData(GeyserSession session, Vector3i pos, Consumer<List<String>> apply) {
}

protected static final Function<Int2ObjectMap<byte[]>, DataComponents> RAW_TRANSFORMER = map -> {
try {
Map<DataComponentType<?>, DataComponent<?, ?>> components = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import org.cloudburstmc.math.vector.Vector3i;
import org.cloudburstmc.nbt.NbtMapBuilder;
import org.cloudburstmc.nbt.NbtType;
import org.cloudburstmc.protocol.bedrock.packet.BlockEntityDataPacket;
import org.cloudburstmc.protocol.bedrock.packet.BlockEventPacket;
import org.geysermc.geyser.api.util.PlatformType;
Expand Down Expand Up @@ -126,6 +127,26 @@ public void translate(GeyserSession session, ClientboundBlockEventPacket packet)

blockEntityPacket.setData(builder.build());
session.sendUpstreamPacket(blockEntityPacket);
} else if (value instanceof DecoratedPotValue potValue) {
// Decorated pots - wobble wobble
// We need to send the sherd data with the client, but we don't really care about latency here so we
// can safely get this from the server
session.getGeyser().getWorldManager().getDecoratedPotData(session, position, sherds -> {
BlockEntityDataPacket blockEntityPacket = new BlockEntityDataPacket();
blockEntityPacket.setBlockPosition(position);

NbtMapBuilder builder = BlockEntityTranslator.getConstantBedrockTag("DecoratedPot", position);
builder.putList("sherds", NbtType.STRING, sherds);
builder.putByte("animation", switch (potValue.getWobbleStyle()) {
case POSITIVE -> (byte) 2;
case NEGATIVE -> (byte) 1;
});

blockEntityPacket.setData(builder.build());
session.sendUpstreamPacket(blockEntityPacket);
});
} else if (session.getGeyser().getLogger().isDebug()) {
session.getGeyser().getLogger().debug("Unhandled block event packet: " + packet);
}
}
}

0 comments on commit 2c47330

Please sign in to comment.