Skip to content

Commit

Permalink
make ModifyBlockRenderPowerType call on block updates and block secti…
Browse files Browse the repository at this point in the history
…on updates
  • Loading branch information
Dueris committed Nov 16, 2024
1 parent 8e962c7 commit 997dab6
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.github.dueris.originspaper.access;

import net.minecraft.core.SectionPos;
import net.minecraft.world.level.block.state.BlockState;

public interface BlockStateOwner {
void apoli$setBlockState(BlockState state);
BlockState apoli$getBlockState();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.dueris.originspaper.access;

import net.minecraft.core.SectionPos;
import net.minecraft.world.level.block.state.BlockState;

public interface SectionBlocksOwner {
void apoli$setBlockStates(BlockState[] states);
BlockState[] apoli$getBlockStates();

SectionPos apoli$sectionPos();
short[] apoli$positions();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.github.dueris.originspaper.mixin;

import io.github.dueris.originspaper.access.BlockStateOwner;
import net.minecraft.core.SectionPos;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

/**
* Need to update blocks for {@link io.github.dueris.originspaper.power.type.ModifyBlockRenderPowerType}...
*/
@Mixin(ClientboundBlockUpdatePacket.class)
public class ClientboundBlockUpdatePacketMixin implements BlockStateOwner {
@Shadow
public BlockState blockState;

@Override
public void apoli$setBlockState(BlockState state) {
this.blockState = state;
}

@Override
public BlockState apoli$getBlockState() {
return this.blockState;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.github.dueris.originspaper.mixin;

import io.github.dueris.originspaper.access.BlockStateOwner;
import io.github.dueris.originspaper.access.SectionBlocksOwner;
import net.minecraft.core.SectionPos;
import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

/**
* Primarily used to maintain compatibility with {@link io.github.dueris.originspaper.power.type.ModifyBlockRenderPowerType} and plugins
*/
@Mixin(ClientboundSectionBlocksUpdatePacket.class)
public class ClientboundSectionBlocksUpdatePacketMixin implements SectionBlocksOwner {
@Shadow @Final private BlockState[] states;

@Shadow @Final private SectionPos sectionPos;

@Shadow @Final private short[] positions;

@Override
public void apoli$setBlockStates(BlockState[] states) {
System.arraycopy(states, 0, this.states, 0, states.length);
}

@Override
public BlockState[] apoli$getBlockStates() {
return this.states;
}

@Override
public SectionPos apoli$sectionPos() {
return this.sectionPos;
}

@Override
public short[] apoli$positions() {
return this.positions;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.github.dueris.originspaper.mixin;

import io.github.dueris.originspaper.access.BlockStateOwner;
import io.github.dueris.originspaper.access.SectionBlocksOwner;
import io.github.dueris.originspaper.component.PowerHolderComponent;
import io.github.dueris.originspaper.power.type.ModifyBlockRenderPowerType;
import net.minecraft.network.PacketSendListener;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundSectionBlocksUpdatePacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerCommonPacketListenerImpl;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerCommonPacketListenerImpl.class)
public class ServerCommonPacketListenerImplMixin {

@Shadow @Final protected ServerPlayer player;

@Inject(method = "send(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketSendListener;)V", at = @At("HEAD"))
public void apoli$modifyBlockRender(Packet<?> packet, PacketSendListener callbacks, CallbackInfo ci) {
if (packet instanceof ClientboundBlockUpdatePacket blockUpdatePacket) {
for (ModifyBlockRenderPowerType powerType : PowerHolderComponent.getPowerTypes(player, ModifyBlockRenderPowerType.class)) {
if (powerType.doesPrevent(player.level(), blockUpdatePacket.getPos())) {
((BlockStateOwner) blockUpdatePacket).apoli$setBlockState(powerType.getBlockState());
}
}
} else if (packet instanceof ClientboundSectionBlocksUpdatePacket sectionBlocksUpdatePacket) {
SectionBlocksOwner sectionBlocksOwner = (SectionBlocksOwner) sectionBlocksUpdatePacket;
BlockState[] states = sectionBlocksOwner.apoli$getBlockStates();
short[] positions = sectionBlocksOwner.apoli$positions();
for (ModifyBlockRenderPowerType powerType : PowerHolderComponent.getPowerTypes(player, ModifyBlockRenderPowerType.class)) {
if (powerType.isSending()) continue;
for (int i = 0; i < states.length; i++) {
if (powerType.doesPrevent(player.level(), sectionBlocksOwner.apoli$sectionPos().relativeToBlockPos(positions[i]))) {
states[i] = powerType.getBlockState();
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class ModifyBlockRenderPowerType extends PowerType {
private final Optional<BlockCondition> blockCondition;
private final BlockState blockState;
private boolean refreshingChunks = false;
private boolean sending;

public ModifyBlockRenderPowerType(Optional<BlockCondition> blockCondition, BlockState state) {
this.blockCondition = blockCondition;
Expand Down Expand Up @@ -96,18 +97,24 @@ public BlockState getBlockState() {
return blockState;
}

public boolean isSending() {
return this.sending;
}

public record RenderUpdate(Chunk chunk, ServerLevel level,
ServerPlayer player) implements Consumer<ModifyBlockRenderPowerType> {

@Override
public void accept(@NotNull ModifyBlockRenderPowerType mbrpt) {
if (player.hasDisconnected() || mbrpt.refreshingChunks) return;
if (player.hasDisconnected() || mbrpt.refreshingChunks) return; // TODO - dueris - remove

Map<Position, BlockData> updates = new ConcurrentHashMap<>();
BlockData toSend = mbrpt.getBlockState().createCraftBlockData();
Util.runOnAllMatchingBlocks(chunk, mbrpt::doesPrevent, (pos) -> updates.put(MCUtil.toPosition(pos), toSend));

mbrpt.sending = true;
player.getBukkitEntity().sendMultiBlockChange(updates);
mbrpt.sending = false;
}
}
}
1 change: 1 addition & 0 deletions origins/src/main/resources/origins.accesswidener
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@ mutable field net/minecraft/world/level/storage/loot/LootTable$Builder poo
accessible field net/minecraft/world/level/storage/loot/LootTable$Builder pools Lcom/google/common/collect/ImmutableList$Builder;
mutable field net/minecraft/world/level/storage/loot/LootTable$Builder functions Lcom/google/common/collect/ImmutableList$Builder;
accessible field net/minecraft/world/level/storage/loot/LootTable$Builder functions Lcom/google/common/collect/ImmutableList$Builder;
mutable field net/minecraft/network/protocol/game/ClientboundBlockUpdatePacket blockState Lnet/minecraft/world/level/block/state/BlockState;
5 changes: 4 additions & 1 deletion origins/src/main/resources/origins.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@
"AdvancementCommandsAccessor",
"ServerPlayerGameModeAccessor",
"PlayerChunkSenderMixin",
"CraftEventFactoryMixin"
"CraftEventFactoryMixin",
"ClientboundBlockUpdatePacketMixin",
"ServerCommonPacketListenerImplMixin",
"ClientboundSectionBlocksUpdatePacketMixin"
],
"compatibilityLevel": "JAVA_17"
}

0 comments on commit 997dab6

Please sign in to comment.