Giter Site home page Giter Site logo

[feature requests] option for rolling limit, change what happens when cap is reached, and how the cap is counted. about limitedspawnerfabric HOT 3 OPEN

strikerrockers-mods avatar strikerrockers-mods commented on June 18, 2024
[feature requests] option for rolling limit, change what happens when cap is reached, and how the cap is counted.

from limitedspawnerfabric.

Comments (3)

strikerrocker avatar strikerrocker commented on June 18, 2024 1

The limit is for the no of mob spawned.

from limitedspawnerfabric.

strikerrocker avatar strikerrocker commented on June 18, 2024

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)

from limitedspawnerfabric.

slidedrum avatar slidedrum commented on June 18, 2024

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;
    }
}

from limitedspawnerfabric.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.