Skip to content

Commit

Permalink
World border utils
Browse files Browse the repository at this point in the history
  • Loading branch information
andantet committed Sep 17, 2024
1 parent c9fec2c commit d310ad9
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fabric_kotlin_version=1.10.20+kotlin.1.9.24
kache_version=1.0.5

# Mod Properties
mod_version=2.10.1
mod_version=2.11.0
maven_group=dev.andante
45 changes: 45 additions & 0 deletions src/main/java/dev/andante/audience/Audience.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.minecraft.network.packet.s2c.play.BundleS2CPacket;
import net.minecraft.network.packet.s2c.play.ClearTitleS2CPacket;
import net.minecraft.network.packet.s2c.play.PlaySoundS2CPacket;
import net.minecraft.network.packet.s2c.play.WorldBorderInitializeS2CPacket;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.network.ServerPlayNetworkHandler;
Expand All @@ -18,10 +19,14 @@
import net.minecraft.util.Pair;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.border.WorldBorder;

import java.util.function.Consumer;
import java.util.function.Function;

import static net.minecraft.util.math.MathHelper.clamp;
import static net.minecraft.util.math.MathHelper.floor;

@SuppressWarnings("unused")
public interface Audience {
/**
Expand Down Expand Up @@ -192,6 +197,46 @@ default void respawn() {
});
}

/**
* Sets all audience player's world borders to make a red tint on their vignette.
*/
default void setTintPercentage(float percentage) {
float clampedPercentage = clamp(percentage, 0.0f, 1.0f);
packet(player -> {
WorldBorder border = new WorldBorder();

border.setCenter(player.getX(), player.getZ());

double baseSize = 10_000_000;
border.setSize(baseSize);

double warningDistance = clampedPercentage * (baseSize * 10);
border.setWarningBlocks(floor(warningDistance));

border.setWarningTime(0);

return new WorldBorderInitializeS2CPacket(border);
});
}

/**
* Sets all audience players' world borders.
*/
default void setWorldBorder(WorldBorder border) {
packet(new WorldBorderInitializeS2CPacket(border));
}

/**
* Resets all audience players' world borders to that of their world.
*/
default void resetWorldBorder() {
packet(player -> {
ServerWorld world = player.getServerWorld();
WorldBorder border = world.getWorldBorder();
return new WorldBorderInitializeS2CPacket(border);
});
}

/**
* Sends the given packet to all audience players.
*/
Expand Down
14 changes: 13 additions & 1 deletion src/test/kotlin/dev/andante/audience/test/AudienceTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package dev.andante.audience.test

import com.google.gson.JsonParser
import com.mojang.serialization.JsonOps
import dev.andante.audience.Audience
import dev.andante.audience.player.PlayerSet
import dev.andante.audience.player.StandalonePlayerReference
import dev.andante.audience.resource.ResourcePack
import dev.andante.audience.resource.ResourcePackServer
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents
import org.slf4j.LoggerFactory
import java.nio.file.Path
import kotlin.io.path.readBytes
import kotlin.math.sin
import kotlin.time.measureTimedValue

object AudienceTest : ModInitializer {
Expand All @@ -19,7 +22,16 @@ object AudienceTest : ModInitializer {
private val logger = LoggerFactory.getLogger("Audience Test")

override fun onInitialize() {
LoggerFactory.getLogger("Audience Test").info("Initializing")
logger.info("Initializing")

ServerTickEvents.END_SERVER_TICK.register { server ->
val playerManager = server.playerManager
playerManager.playerList.forEach { player ->
val audience = player as Audience
val value = 1.0f - sin(player.age / 10.0f)
audience.setTintPercentage(value)
}
}

if (TEST_RESOURCE_PACK) {
val byteArray = try {
Expand Down

0 comments on commit d310ad9

Please sign in to comment.