|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 GeyserMC. http://geysermc.org |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | + * THE SOFTWARE. |
| 21 | + * |
| 22 | + * @author GeyserMC |
| 23 | + * @link https://github.com/GeyserMC/Geyser |
| 24 | + */ |
| 25 | + |
| 26 | +package org.geysermc.geyser.platform.mod.mixin.server; |
| 27 | + |
| 28 | +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
| 29 | +import com.llamalad7.mixinextras.sugar.Share; |
| 30 | +import com.llamalad7.mixinextras.sugar.ref.LocalRef; |
| 31 | +import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap; |
| 32 | +import it.unimi.dsi.fastutil.objects.Object2ObjectMap; |
| 33 | +import net.minecraft.core.BlockPos; |
| 34 | +import net.minecraft.core.Direction; |
| 35 | +import net.minecraft.world.entity.player.Player; |
| 36 | +import net.minecraft.world.level.Level; |
| 37 | +import net.minecraft.world.level.block.Block; |
| 38 | +import net.minecraft.world.level.block.piston.PistonBaseBlock; |
| 39 | +import net.minecraft.world.level.block.state.BlockState; |
| 40 | +import org.cloudburstmc.math.vector.Vector3i; |
| 41 | +import org.geysermc.geyser.GeyserImpl; |
| 42 | +import org.geysermc.geyser.session.GeyserSession; |
| 43 | +import org.geysermc.geyser.session.cache.PistonCache; |
| 44 | +import org.geysermc.geyser.translator.level.block.entity.PistonBlockEntity; |
| 45 | +import org.geysermc.mcprotocollib.protocol.data.game.level.block.value.PistonValueType; |
| 46 | +import org.spongepowered.asm.mixin.Final; |
| 47 | +import org.spongepowered.asm.mixin.Mixin; |
| 48 | +import org.spongepowered.asm.mixin.Shadow; |
| 49 | +import org.spongepowered.asm.mixin.Unique; |
| 50 | +import org.spongepowered.asm.mixin.injection.At; |
| 51 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 52 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; |
| 53 | + |
| 54 | +import java.util.HashMap; |
| 55 | +import java.util.Map; |
| 56 | +import java.util.UUID; |
| 57 | + |
| 58 | +@Mixin(PistonBaseBlock.class) |
| 59 | +public class PistonBaseBlockMixin { |
| 60 | + |
| 61 | + @Shadow |
| 62 | + @Final |
| 63 | + private boolean isSticky; |
| 64 | + |
| 65 | + @ModifyExpressionValue(method = "moveBlocks", |
| 66 | + at = @At(value = "INVOKE", target = "Lcom/google/common/collect/Maps;newHashMap()Ljava/util/HashMap;") |
| 67 | + ) |
| 68 | + private HashMap<BlockPos, BlockState> geyser$onMapCreate(HashMap<BlockPos, BlockState> original, @Share("pushBlocks") LocalRef<Map<BlockPos, BlockState>> localRef) { |
| 69 | + localRef.set(original); |
| 70 | + return original; |
| 71 | + } |
| 72 | + |
| 73 | + @Inject(method = "moveBlocks", |
| 74 | + at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/piston/PistonStructureResolver;getToDestroy()Ljava/util/List;") |
| 75 | + ) |
| 76 | + private void geyser$onBlocksMove(Level level, BlockPos blockPos, Direction direction, boolean isExtending, CallbackInfoReturnable<Boolean> cir, @Share("pushBlocks") LocalRef<Map<BlockPos, BlockState>> localRef) { |
| 77 | + PistonValueType type = isExtending ? PistonValueType.PUSHING : PistonValueType.PULLING; |
| 78 | + boolean sticky = this.isSticky; |
| 79 | + |
| 80 | + Object2ObjectMap<Vector3i, org.geysermc.geyser.level.block.type.BlockState> attachedBlocks = new Object2ObjectArrayMap<>(); |
| 81 | + boolean blocksFilled = false; |
| 82 | + |
| 83 | + for (Map.Entry<UUID, GeyserSession> entry : GeyserImpl.getInstance().getSessionManager().getSessions().entrySet()) { |
| 84 | + Player player = level.getPlayerByUUID(entry.getKey()); |
| 85 | + //noinspection resource |
| 86 | + if (player == null || !player.level().equals(level)) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + GeyserSession session = entry.getValue(); |
| 90 | + |
| 91 | + int dX = Math.abs(blockPos.getX() - player.getBlockX()) >> 4; |
| 92 | + int dZ = Math.abs(blockPos.getZ() - player.getBlockZ()) >> 4; |
| 93 | + if ((dX * dX + dZ * dZ) > session.getServerRenderDistance() * session.getServerRenderDistance()) { |
| 94 | + // Ignore pistons outside the player's render distance |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + // Trying to grab the blocks from the world like other platforms would result in the moving piston block |
| 99 | + // being returned instead. |
| 100 | + if (!blocksFilled) { |
| 101 | + Map<BlockPos, net.minecraft.world.level.block.state.BlockState> blocks = localRef.get(); |
| 102 | + for (Map.Entry<BlockPos, BlockState> blockStateEntry : blocks.entrySet()) { |
| 103 | + int blockStateId = Block.BLOCK_STATE_REGISTRY.getId(blockStateEntry.getValue()); |
| 104 | + org.geysermc.geyser.level.block.type.BlockState state = org.geysermc.geyser.level.block.type.BlockState.of(blockStateId); |
| 105 | + attachedBlocks.put(geyser$fromBlockPos(blockStateEntry.getKey()), state); |
| 106 | + } |
| 107 | + blocksFilled = true; |
| 108 | + } |
| 109 | + |
| 110 | + org.geysermc.geyser.level.physics.Direction orientation = org.geysermc.geyser.level.physics.Direction.VALUES[direction.ordinal()]; |
| 111 | + |
| 112 | + Vector3i position = geyser$fromBlockPos(blockPos); |
| 113 | + session.executeInEventLoop(() -> { |
| 114 | + PistonCache pistonCache = session.getPistonCache(); |
| 115 | + PistonBlockEntity blockEntity = pistonCache.getPistons().computeIfAbsent(position, pos -> |
| 116 | + new PistonBlockEntity(session, position, orientation, sticky, !isExtending)); |
| 117 | + blockEntity.setAction(type, attachedBlocks); |
| 118 | + }); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Unique |
| 123 | + private static Vector3i geyser$fromBlockPos(BlockPos pos) { |
| 124 | + return Vector3i.from(pos.getX(), pos.getY(), pos.getZ()); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments