StrikerRockers-Mods / LimitedSpawnerFabric

MIT License
0 stars 1 forks source link

[feature requests] option for rolling limit, change what happens when cap is reached, and how the cap is counted. #2

Open slidedrum opened 3 years ago

slidedrum commented 3 years ago

I just found this mod, and this is EXACTLY what I was looking for to add to my server to stop players from turning modded dungeons into megafarms.

Three things I would really like to see, is one: an option to make the limit rolling, what I mean by that is, make it so that if the spawner has spawned more than the limit within the past X minutes, it no longer spawns. But after that time is up, it starts spawning again. But not as a full reset. Just like how rolling averages work. Hopefully that makes sense.

secondly, an option to choose what happens when the limit is capped. The options I would like to see are: remove the spawner, stop the fire particles and spinning, or replace the spawner with a "broken spawner" block.

Lastly, a question, is the limit the amount of mobs spawned, or the amount of times a group of mobs is spawned? Either way, an option to choose how you would like to count it would be useful.

strikerrocker commented 3 years ago

The limit is for the no of mob spawned.

strikerrocker commented 3 years ago

Can you explain more on how the 1st feature request would work. How is rolling average relevant here? What will be the usecase for this feature?

Regarding the second request I don't want to have a custom broken block nor just removing the spawning particles(Requires quite a bit of mixins which I don't want todo as I have this mod for both forge and fabric. Forge version doesn't use mixins)

slidedrum commented 3 years ago

I am relatively inexperienced with github so I need to figure out how to make this a pull request, but this is how I implemented a rolling count. Basically, check if it has spawned more than the limit, within the last n seconds. So it doesn't get disabled forever.

MixinSpawnerLogic.java


package io.github.strikerrocker.mixin;

import io.github.strikerrocker.LimitedSpawner;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.MobSpawnerLogic;
import net.minecraft.world.World;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.LinkedList;

@Mixin(MobSpawnerLogic.class)
public abstract class MixinSpawnerLogic{
    public Long rollingTime = Long.valueOf(LimitedSpawner.config.time);
    public LinkedList<Long> spawnedCount=new LinkedList<Long>();
    @Shadow
    private int spawnDelay;
    @Shadow
    private int maxSpawnDelay;
    @Shadow protected abstract void updateSpawns(World world, BlockPos pos);
    @Inject(method = "serverTick", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ServerWorld;syncWorldEvent(ILnet/minecraft/util/math/BlockPos;I)V"))
    public void updateCount(CallbackInfo ci) {
        Long unixTime = System.currentTimeMillis();
        rollingTime = Long.valueOf(LimitedSpawner.config.time);
        this.spawnedCount.add(unixTime);
    }

    @Inject(method = "clientTick",at=@At("HEAD"),cancellable = true)
    public void clientTick(World world, BlockPos pos,CallbackInfo ci) {

    }

    @Inject(method = "writeNbt", at = @At(value = "INVOKE", target = "Lnet/minecraft/nbt/NbtCompound;putShort(Ljava/lang/String;S)V", ordinal = 1))
    public void writeNbt(World world, BlockPos pos, NbtCompound nbt, CallbackInfoReturnable<NbtCompound> cir) {
        //System.out.println("Writing spawned count as: "+this.spawnedCount);
        nbt.putLongArray("spawnerCount", this.spawnedCount);
    }

    @Inject(method = "serverTick", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/entity/EntityType;fromNbt(Lnet/minecraft/nbt/NbtCompound;)Ljava/util/Optional;"), cancellable = true)
    public void controlSpawn(ServerWorld world, BlockPos pos,CallbackInfo ci) {

        Long unixTime = System.currentTimeMillis();
        if (LimitedSpawner.config.debug)
            System.out.println("\n\n\nCurrent Time: "+unixTime);
        LinkedList<Long> temp = new LinkedList<>();
        for (Long i:this.spawnedCount ){
            if (i < unixTime - (rollingTime * 1000L) && this.spawnedCount.contains(i)){
                if (LimitedSpawner.config.debug)
                    System.out.println("time "+i+" is more than "+LimitedSpawner.config.time+" second(s) from "+unixTime+" ("+((unixTime-i)/1000L)+") removing");
            }
            else {
                temp.add(i);
                if (LimitedSpawner.config.debug)
                    System.out.println("time " + i + " is less than " + LimitedSpawner.config.time + " second(s) from " + unixTime + " (" + ((unixTime - i) / 1000L) + ") keeping");
            }
        }
        this.spawnedCount = temp;
        if (LimitedSpawner.config.debug)
            System.out.println("Spawner has spawned "+this.spawnedCount.size()+" times");
        if (this.spawnedCount.size() >= LimitedSpawner.config.limit) {
            if (LimitedSpawner.config.debug)
                System.out.println("too many spawns!");
            this.spawnDelay = this.maxSpawnDelay;
            this.updateSpawns(world, pos);
            ci.cancel();
        }
        else{
            this.updateSpawns(world, pos);
            if (LimitedSpawner.config.debug)
                System.out.println("allowing spawns "+ this.spawnedCount.size() +" is smaller than "+LimitedSpawner.config.limit);
        }
    }

    @Inject(method = "readNbt", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/nbt/NbtCompound;getShort(Ljava/lang/String;)S", ordinal = 0))
    public void readNbt(World world, BlockPos pos, NbtCompound nbt, CallbackInfo ci) {
        LinkedList<Long> temp = new LinkedList();
        for (Long i:nbt.getLongArray("spawnerCount"))
            temp.add(i);
        //System.out.println("Reading spawned count as: "+temp);
        this.spawnedCount = temp;
    }
}