Files
VisualOverhaul/src/main/java/eu/midnightdust/visualoverhaul/mixin/MixinBrewingStandBlockEntity.java
Motschen 1bcba33aaa VisualOverhaul 3.3.0 - 1.17 and small Refactor
Update to 1.17-pre1 and Java 16
Upgrade to MidnightLib, making the config more clean and accessible without modmenu
(Please ignore the commits before)
2021-05-30 13:25:44 +02:00

57 lines
2.6 KiB
Java
Executable File

package eu.midnightdust.visualoverhaul.mixin;
import eu.midnightdust.visualoverhaul.VisualOverhaul;
import io.netty.buffer.Unpooled;
import net.fabricmc.fabric.api.server.PlayerStream;
import net.fabricmc.fabric.impl.networking.ServerSidePacketRegistryImpl;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.BrewingStandBlockEntity;
import net.minecraft.block.entity.LockableContainerBlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.util.stream.Stream;
@Mixin(BrewingStandBlockEntity.class)
public abstract class MixinBrewingStandBlockEntity extends LockableContainerBlockEntity {
private static boolean invUpdate = true;
private static int playerUpdate = -1;
protected MixinBrewingStandBlockEntity(BlockEntityType<?> blockEntityType, BlockPos blockPos, BlockState blockState) {
super(blockEntityType, blockPos, blockState);
}
@Inject(at = @At("TAIL"), method = "tick")
private static void tick(World world, BlockPos pos, BlockState state, BrewingStandBlockEntity blockEntity, CallbackInfo ci) {
if (!world.isClient && (invUpdate || world.getPlayers().size() == playerUpdate)) {
Stream<PlayerEntity> watchingPlayers = PlayerStream.watching(world, pos);
PacketByteBuf passedData = new PacketByteBuf(Unpooled.buffer());
passedData.writeBlockPos(pos);
passedData.writeItemStack(blockEntity.getStack(0));
passedData.writeItemStack(blockEntity.getStack(1));
passedData.writeItemStack(blockEntity.getStack(2));
passedData.writeItemStack(blockEntity.getStack(3));
passedData.writeItemStack(blockEntity.getStack(4));
watchingPlayers.forEach(player -> ServerSidePacketRegistryImpl.INSTANCE.sendToPlayer(player, VisualOverhaul.UPDATE_POTION_BOTTLES, passedData));
invUpdate = false;
}
playerUpdate = world.getPlayers().size();
}
@Inject(at = @At("RETURN"), method = "getStack", cancellable = true)
public void getStack(int slot, CallbackInfoReturnable<ItemStack> cir) {
invUpdate = true;
}
}