| 1 | package com.jellypudding.offlineclient.modules.player;
|
| 2 |
|
| 3 | import com.jellypudding.offlineclient.event.Subscribe;
|
| 4 | import com.jellypudding.offlineclient.event.events.PacketReceiveEvent;
|
| 5 | import com.jellypudding.offlineclient.event.events.TickEvent;
|
| 6 | import com.jellypudding.offlineclient.module.Category;
|
| 7 | import com.jellypudding.offlineclient.module.Module;
|
| 8 | import com.jellypudding.offlineclient.setting.BoolSetting;
|
| 9 | import com.jellypudding.offlineclient.setting.RegistryListSetting;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.particles.ParticleOptions;
|
| 12 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 13 | import net.minecraft.network.protocol.game.ClientboundUpdateMobEffectPacket;
|
| 14 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 15 | import net.minecraft.world.effect.MobEffect;
|
| 16 | import net.minecraft.world.effect.MobEffectInstance;
|
| 17 | import net.minecraft.world.effect.MobEffects;
|
| 18 | import net.minecraft.world.entity.LivingEntity;
|
| 19 |
|
| 20 | import java.util.ArrayList;
|
| 21 | import java.util.List;
|
| 22 | import java.util.concurrent.atomic.AtomicInteger;
|
| 23 |
|
| 24 | // Drops the incoming effect packets for the effects you pick. A blocked effect
|
| 25 | // keeps running on the server copy of you. Only the display of it goes away.
|
| 26 | public final class NoStatusEffects extends Module {
|
| 27 |
|
| 28 | private final RegistryListSetting<MobEffect> effects = new RegistryListSetting<>("Effects",
|
| 29 | "The effects to block. Click to pick them.", BuiltInRegistries.MOB_EFFECT,
|
| 30 | List.of(MobEffects.LEVITATION.value(), MobEffects.JUMP_BOOST.value(),
|
| 31 | MobEffects.SLOW_FALLING.value(), MobEffects.DOLPHINS_GRACE.value()));
|
| 32 | private final BoolSetting blockAll = new BoolSetting("Block all",
|
| 33 | "Block every effect instead of the chosen ones.", false);
|
| 34 | private final BoolSetting selfOnly = new BoolSetting("You only",
|
| 35 | "Leave the effects on other entities alone.", true);
|
| 36 | private final BoolSetting clearActive = new BoolSetting("Clear active",
|
| 37 | "Strip a blocked effect that is already running on you.", true);
|
| 38 | private final BoolSetting hideParticles = new BoolSetting("Hide particles",
|
| 39 | "Stop every effect particle around you.", true);
|
| 40 |
|
| 41 | private final AtomicInteger blocked = new AtomicInteger();
|
| 42 |
|
| 43 | // Read on the network thread and written on the main thread.
|
| 44 | private volatile int localId = -1;
|
| 45 |
|
| 46 | // True whilst the tracked particle list is held empty.
|
| 47 | private boolean particlesHidden;
|
| 48 |
|
| 49 | public NoStatusEffects() {
|
| 50 | super("NoStatusEffects", "Hides your chosen effects on your client whilst the server keeps them.",
|
| 51 | Category.PLAYER);
|
| 52 | addSettings(effects, blockAll, selfOnly, clearActive, hideParticles);
|
| 53 | searchTags("no levitation", "anti effect", "potion block", "effect particles");
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public String getSuffix() {
|
| 58 | int count = blocked.get();
|
| 59 | return count == 0 ? null : count + " blocked";
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | protected void onEnable() {
|
| 64 | blocked.set(0);
|
| 65 | localId = mc.player == null ? -1 : mc.player.getId();
|
| 66 | }
|
| 67 |
|
| 68 | @Override
|
| 69 | protected void onDisable() {
|
| 70 | if (particlesHidden) {
|
| 71 | restoreParticles();
|
| 72 | }
|
| 73 | localId = -1;
|
| 74 | }
|
| 75 |
|
| 76 | @Subscribe
|
| 77 | private void onTick(TickEvent event) {
|
| 78 | if (!inGame()) {
|
| 79 | return;
|
| 80 | }
|
| 81 | localId = mc.player.getId();
|
| 82 | if (hideParticles.isOn()) {
|
| 83 | clearParticles();
|
| 84 | } else if (particlesHidden) {
|
| 85 | restoreParticles();
|
| 86 | }
|
| 87 | if (!clearActive.isOn()) {
|
| 88 | return;
|
| 89 | }
|
| 90 | List<Holder<MobEffect>> stale = null;
|
| 91 | for (MobEffectInstance instance : mc.player.getActiveEffects()) {
|
| 92 | if (isBlocked(instance.getEffect())) {
|
| 93 | if (stale == null) {
|
| 94 | stale = new ArrayList<>();
|
| 95 | }
|
| 96 | stale.add(instance.getEffect());
|
| 97 | }
|
| 98 | }
|
| 99 | if (stale == null) {
|
| 100 | return;
|
| 101 | }
|
| 102 | // Removing inside the loop above would break the iterator.
|
| 103 | for (Holder<MobEffect> effect : stale) {
|
| 104 | mc.player.removeEffect(effect);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | // The client spawns the swirls from this tracked list and never from its own effect map.
|
| 109 | // Dropping the effect packet leaves the list alone. Emptying it here is what stops the swirls.
|
| 110 | private void clearParticles() {
|
| 111 | particlesHidden = true;
|
| 112 | SynchedEntityData data = mc.player.getEntityData();
|
| 113 | if (!data.get(LivingEntity.DATA_EFFECT_PARTICLES).isEmpty()) {
|
| 114 | data.set(LivingEntity.DATA_EFFECT_PARTICLES, List.of());
|
| 115 | }
|
| 116 | }
|
| 117 |
|
| 118 | // Nothing else refills the list until the server sends the next effect packet.
|
| 119 | private void restoreParticles() {
|
| 120 | particlesHidden = false;
|
| 121 | if (mc.player == null) {
|
| 122 | return;
|
| 123 | }
|
| 124 | List<ParticleOptions> particles = new ArrayList<>();
|
| 125 | for (MobEffectInstance instance : mc.player.getActiveEffects()) {
|
| 126 | if (instance.isVisible()) {
|
| 127 | particles.add(instance.getParticleOptions());
|
| 128 | }
|
| 129 | }
|
| 130 | mc.player.getEntityData().set(LivingEntity.DATA_EFFECT_PARTICLES, particles);
|
| 131 | }
|
| 132 |
|
| 133 | @Subscribe(priority = 200)
|
| 134 | private void onPacketReceive(PacketReceiveEvent event) {
|
| 135 | if (!(event.getPacket() instanceof ClientboundUpdateMobEffectPacket packet)) {
|
| 136 | return;
|
| 137 | }
|
| 138 | if (selfOnly.isOn() && packet.getEntityId() != localId) {
|
| 139 | return;
|
| 140 | }
|
| 141 | if (isBlocked(packet.getEffect())) {
|
| 142 | event.cancel();
|
| 143 | blocked.incrementAndGet();
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | // Called from the network thread as well as the main thread.
|
| 148 | private boolean isBlocked(Holder<MobEffect> effect) {
|
| 149 | if (blockAll.isOn()) {
|
| 150 | return true;
|
| 151 | }
|
| 152 | return effect.isBound() && effects.contains(effect.value());
|
| 153 | }
|
| 154 | }
|
| 155 |
|