Skip to content

Commit

Permalink
Added player.activePostShader = null/'shader id' method. Persists ove…
Browse files Browse the repository at this point in the history
…r deaths and dimension change but not when disconnected from world/server
  • Loading branch information
LatvianModder committed Jul 6, 2024
1 parent 77be99a commit 4d40014
Show file tree
Hide file tree
Showing 16 changed files with 180 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.latvian.mods.kubejs.core;

import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.mutable.Mutable;

public interface ClientPacketListenerKJS {
default Mutable<ResourceLocation> kjs$activePostShader() {
throw new NoMixinException();
}
}
7 changes: 7 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/core/LevelKJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ public interface LevelKJS extends WithAttachedData<Level>, ScriptTypeHolder {
}
}

@Override
default void kjs$setActivePostShader(@Nullable ResourceLocation id) {
for (var entity : kjs$self().players()) {
entity.kjs$setActivePostShader(id);
}
}

default ResourceLocation kjs$getDimension() {
return kjs$self().dimension().location();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import dev.latvian.mods.rhino.util.RemapPrefixForJS;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

@RemapPrefixForJS("kjs$")
public interface MessageSenderKJS {
Expand All @@ -26,4 +28,7 @@ public interface MessageSenderKJS {
default void kjs$runCommandSilent(String command) {
kjs$runCommand(command);
}

default void kjs$setActivePostShader(@Nullable ResourceLocation id) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.neoforged.neoforge.network.PacketDistributor;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -42,12 +43,17 @@ public interface MinecraftClientKJS extends MinecraftEnvironmentKJS {

@Override
default void kjs$runCommand(String command) {
kjs$self().player.connection.sendCommand(command);
kjs$self().player.kjs$runCommand(command);
}

@Override
default void kjs$runCommandSilent(String command) {
kjs$self().player.connection.sendCommand(command);
kjs$self().player.kjs$runCommandSilent(command);
}

@Override
default void kjs$setActivePostShader(@Nullable ResourceLocation id) {
kjs$self().player.kjs$setActivePostShader(id);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public interface MinecraftServerKJS extends WithAttachedData<MinecraftServer>, W
kjs$self().getCommands().performPrefixedCommand(kjs$self().createCommandSourceStack().withSuppressedOutput(), command);
}

@Override
default void kjs$setActivePostShader(@Nullable ResourceLocation id) {
for (var player : kjs$self().getPlayerList().getPlayers()) {
player.kjs$setActivePostShader(id);
}
}

default ServerLevel kjs$getLevel(ResourceLocation dimension) {
return kjs$self().getLevel(ResourceKey.create(Registries.DIMENSION, dimension));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import dev.latvian.mods.kubejs.level.BlockContainerJS;
import dev.latvian.mods.kubejs.net.NotificationPayload;
import dev.latvian.mods.kubejs.net.SendDataFromServerPayload;
import dev.latvian.mods.kubejs.net.SetActivePostShaderPayload;
import dev.latvian.mods.kubejs.player.PlayerStatsJS;
import dev.latvian.mods.kubejs.util.NotificationToastData;
import dev.latvian.mods.rhino.util.RemapPrefixForJS;
Expand All @@ -30,6 +31,7 @@

import java.util.Date;
import java.util.HashMap;
import java.util.Optional;
import java.util.function.Consumer;

@RemapPrefixForJS("kjs$")
Expand Down Expand Up @@ -234,4 +236,9 @@ public AbstractContainerMenu createMenu(int i, Inventory inventory, Player playe
kjs$self().heal(kjs$self().getMaxHealth());
kjs$self().getFoodData().eat(20, 1F);
}

@Override
default void kjs$setActivePostShader(@Nullable ResourceLocation id) {
PacketDistributor.sendToPlayer(kjs$self(), new SetActivePostShaderPayload(Optional.ofNullable(id)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.latvian.mods.kubejs.core.mixin;

import dev.latvian.mods.kubejs.core.ClientPacketListenerKJS;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.mutable.Mutable;
import org.apache.commons.lang3.mutable.MutableObject;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;

@Mixin(ClientPacketListener.class)
public class ClientPacketListenerMixin implements ClientPacketListenerKJS {
@Unique
private final Mutable<ResourceLocation> kjs$activePostShader = new MutableObject<>(null);

@Override
public Mutable<ResourceLocation> kjs$activePostShader() {
return kjs$activePostShader;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

@Mixin(DamageSource.class)
@RemapPrefixForJS("kjs$")
Expand All @@ -25,6 +26,7 @@ public abstract class DamageSourceMixin {
public abstract Entity getEntity();

@Nullable
@Unique
public Player kjs$getPlayer() {
return getEntity() instanceof Player p ? p : null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
Expand All @@ -28,6 +29,7 @@ public abstract class EntityMixin implements EntityKJS {
@Shadow
public abstract void playerTouch(Player arg);

@Unique
private CompoundTag kjs$persistentData;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.latvian.mods.kubejs.core.mixin;

import dev.latvian.mods.kubejs.core.ClientPacketListenerKJS;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.PostChain;
import net.minecraft.resources.ResourceLocation;
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;

import javax.annotation.Nullable;

@Mixin(GameRenderer.class)
public abstract class GameRendererMixin {
@Shadow
@Final
Minecraft minecraft;

@Shadow
@Nullable
PostChain postEffect;

@Shadow
public abstract void loadEffect(ResourceLocation resourceLocation);

@Inject(method = "checkEntityPostEffect", at = @At("HEAD"), cancellable = true)
private void kjs$checkEntityPostEffect(CallbackInfo ci) {
if (minecraft.getConnection() instanceof ClientPacketListenerKJS connection && connection.kjs$activePostShader().getValue() != null) {
if (postEffect != null) {
postEffect.close();
}

loadEffect(connection.kjs$activePostShader().getValue());
ci.cancel();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -17,10 +18,12 @@ public abstract class GameRulesMixin implements GameRulesKJS {
@Shadow
public abstract <T extends GameRules.Value<T>> T getRule(GameRules.Key<T> key);

@Unique
private Map<String, GameRules.Key<?>> kjs$keyCache;

@Nullable
private GameRules.Key<?> getKey(String rule) {
@Unique
private GameRules.Key<?> kjs$getKey(String rule) {
if (kjs$keyCache == null) {
kjs$keyCache = new HashMap<>();

Expand All @@ -38,13 +41,13 @@ public <T extends GameRules.Value<T>> void visit(GameRules.Key<T> key, GameRules
@Override
@Nullable
public GameRules.Value<?> kjs$get(String rule) {
var key = getKey(rule);
var key = kjs$getKey(rule);
return key == null ? null : getRule(key);
}

@Override
public void kjs$set(String rule, String value) {
var key = getKey(rule);
var key = kjs$getKey(rule);
var r = key == null ? null : getRule(key);

if (r != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package dev.latvian.mods.kubejs.core.mixin;

import com.mojang.authlib.GameProfile;
import dev.latvian.mods.kubejs.core.ClientPacketListenerKJS;
import dev.latvian.mods.rhino.util.RemapForJS;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.stats.StatsCounter;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

Expand All @@ -18,4 +24,28 @@ public LocalPlayerMixin(Level level, BlockPos blockPos, float f, GameProfile gam
@Shadow
@RemapForJS("getStatsCounter")
public abstract StatsCounter getStats();

@Shadow
@Final
public ClientPacketListener connection;

@Shadow
@Final
protected Minecraft minecraft;

@Override
public void kjs$runCommand(String command) {
connection.sendCommand(command);
}

@Override
public void kjs$runCommandSilent(String command) {
connection.sendCommand(command);
}

@Override
public void kjs$setActivePostShader(@Nullable ResourceLocation id) {
((ClientPacketListenerKJS) connection).kjs$activePostShader().setValue(id);
minecraft.gameRenderer.checkEntityPostEffect(minecraft.options.getCameraType().isFirstPerson() ? minecraft.getCameraEntity() : null);
}
}
2 changes: 2 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/net/KubeJSNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ private static <T extends CustomPacketPayload> CustomPacketPayload.Type<T> type(
CustomPacketPayload.Type<RequestBlockKubedexPayload> REQUEST_BLOCK_KUBEDEX = type("request_block_kubedex");
CustomPacketPayload.Type<RequestEntityKubedexPayload> REQUEST_ENTITY_KUBEDEX = type("request_entity_kubedex");
CustomPacketPayload.Type<SyncRecipeViewerDataPayload> SYNC_RECIPE_VIEWER = type("sync_recipe_viewer");
CustomPacketPayload.Type<SetActivePostShaderPayload> SET_ACTIVE_POST_SHADER = type("set_active_post_shader");

@SubscribeEvent
static void register(RegisterPayloadHandlersEvent event) {
Expand All @@ -47,5 +48,6 @@ static void register(RegisterPayloadHandlersEvent event) {
reg.playToServer(REQUEST_BLOCK_KUBEDEX, RequestBlockKubedexPayload.STREAM_CODEC, RequestBlockKubedexPayload::handle);
reg.playToServer(REQUEST_ENTITY_KUBEDEX, RequestEntityKubedexPayload.STREAM_CODEC, RequestEntityKubedexPayload::handle);
reg.playToClient(SYNC_RECIPE_VIEWER, SyncRecipeViewerDataPayload.STREAM_CODEC, SyncRecipeViewerDataPayload::handle);
reg.playToClient(SET_ACTIVE_POST_SHADER, SetActivePostShaderPayload.STREAM_CODEC, SetActivePostShaderPayload::handle);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package dev.latvian.mods.kubejs.net;

import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.neoforge.network.handling.IPayloadContext;

import java.util.Optional;

public record SetActivePostShaderPayload(Optional<ResourceLocation> id) implements CustomPacketPayload {
public static final StreamCodec<ByteBuf, SetActivePostShaderPayload> STREAM_CODEC = ByteBufCodecs.optional(ResourceLocation.STREAM_CODEC).map(SetActivePostShaderPayload::new, SetActivePostShaderPayload::id);

@Override
public Type<?> type() {
return KubeJSNet.SET_ACTIVE_POST_SHADER;
}

public void handle(IPayloadContext ctx) {
ctx.enqueueWork(() -> ctx.player().kjs$setActivePostShader(id.orElse(null)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.commands.arguments.selector.EntitySelector;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
Expand Down Expand Up @@ -79,6 +80,13 @@ public void addAllIterable(Iterable<? extends Entity> entities) {
}
}

@Override
public void kjs$setActivePostShader(@Nullable ResourceLocation id) {
for (var entity : this) {
entity.kjs$setActivePostShader(id);
}
}

public void kill() {
for (var entity : this) {
entity.kill();
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/kubejs.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"AbstractClientPlayerMixin",
"AbstractSelectionListMixin",
"ClientLevelMixin",
"ClientPacketListenerMixin",
"CreativeModeInventoryScreenMixin",
"GameRendererMixin",
"ItemStackClientMixin",
"KeyboardHandlerMixin",
"LevelRendererMixin",
Expand Down

0 comments on commit 4d40014

Please sign in to comment.