spawn tracking

This commit is contained in:
gnembon committed 2019-05-19 02:01:27 -03:00
1 parent d970f7e4dd
commit 5ca22d94e2
10 files changed
+328 -111

No files matched your search

@@ -0,0 +1,41 @@
package carpet.mixins;
import carpet.CarpetSettings;
import com.mojang.bridge.game.GameVersion;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.DebugHud;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(DebugHud.class)
public class DebugHudMixin
{
@Redirect(method = "getLeftText", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/MinecraftClient;getGameVersion()Ljava/lang/String;"
))
private String getCarpetClient(MinecraftClient minecraftClient)
{
return "vanilla enough";
}
@Redirect(method = "getLeftText", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/ClientBrandRetriever;getClientModName()Ljava/lang/String;"
))
private String getModName()
{
return "don't even...";
}
@Redirect(method = "getLeftText", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/MinecraftClient;getVersionType()Ljava/lang/String;"
))
private String getVersionType(MinecraftClient minecraftClient)
{
return "release";
}
}
@@ -1,8 +1,10 @@
package carpet.mixins;
import carpet.CarpetServer;
import carpet.CarpetSettings;
import net.minecraft.client.MinecraftClient;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@@ -18,4 +20,13 @@ public class MinecraftClientMixin
//CM start game hook
CarpetServer.onGameStarted();
}
/**
* @author gnembon
*/
@Overwrite
public String getVersionType()
{
return "carpet "+ CarpetSettings.carpetVersion;
}
}
@@ -7,21 +7,32 @@ import net.minecraft.entity.EntityCategory;
import net.minecraft.server.world.ServerChunkManager;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.SpawnHelper;
import net.minecraft.world.World;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.level.LevelProperties;
import org.apache.commons.lang3.tuple.Pair;
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.ModifyConstant;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
import java.util.HashMap;
import java.util.HashSet;
@Mixin(ServerChunkManager.class)
public abstract class ServerChunkManagerMixin
{
@Shadow @Final private ServerWorld world;
@Shadow @Final private static int CHUNKS_ELIGIBLE_FOR_SPAWNING;
@Inject(
method = "tickChunks",
locals = LocalCapture.CAPTURE_FAILHARD,
@@ -32,6 +43,7 @@ public abstract class ServerChunkManagerMixin
ordinal = 0
))
//this runs once per world spawning cycle. Allows to grab mob counts and count spawn ticks
private void grabMobcaps(CallbackInfo ci,
long long_1,
long long_2,
@@ -48,6 +60,119 @@ public abstract class ServerChunkManagerMixin
DimensionType dim = this.world.dimension.getType();
SpawnReporter.mobCounts.put(dim, (Object2IntMap<EntityCategory>)object2IntMap_1);
SpawnReporter.chunkCounts.put(dim, int_2);
if (SpawnReporter.track_spawns > 0L)
{
//local spawns now need to be tracked globally cause each calll is just for chunk
SpawnReporter.local_spawns = new HashMap<>();
SpawnReporter.first_chunk_marker = new HashSet<>();
for (EntityCategory cat : EntityCategory.values())
{
Pair key = Pair.of(dim, cat);
SpawnReporter.overall_spawn_ticks.put(key,
SpawnReporter.overall_spawn_ticks.get(key)+
SpawnReporter.spawn_tries.get(cat));
}
}
}
@Redirect(method = "tickChunks", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/SpawnHelper;spawnEntitiesInChunk(Lnet/minecraft/entity/EntityCategory;Lnet/minecraft/world/World;Lnet/minecraft/world/chunk/WorldChunk;Lnet/minecraft/util/math/BlockPos;)V"
))
// inject our repeat of spawns if more spawn ticks per tick are chosen.
private void spawnMultipleTimes(EntityCategory entityCategory_1, World world_1, WorldChunk worldChunk_1, BlockPos blockPos_1)
{
for (int i = 0; i < SpawnReporter.spawn_tries.get(entityCategory_1); i++)
{
SpawnHelper.spawnEntitiesInChunk(entityCategory_1, world_1, worldChunk_1, blockPos_1);
}
}
@Redirect(method = "tickChunks", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/entity/EntityCategory;getSpawnCap()I"
))
// allows to change mobcaps and captures each category try per dimension before it fails due to full mobcaps.
private int getNewMobcaps(EntityCategory entityCategory)
{
DimensionType dim = this.world.dimension.getType();
int newCap = entityCategory.getSpawnCap()*(int)(Math.pow(2.0,(SpawnReporter.mobcap_exponent/4)));
if (SpawnReporter.track_spawns > 0L)
{
int int_2 = SpawnReporter.chunkCounts.get(dim); // eligible chunks for spawning
int int_3 = newCap * int_2 / CHUNKS_ELIGIBLE_FOR_SPAWNING; //current spawning limits
int mobCount = SpawnReporter.mobCounts.get(dim).getInt(entityCategory);
if (SpawnReporter.track_spawns > 0L && !SpawnReporter.first_chunk_marker.contains(entityCategory))
{
SpawnReporter.first_chunk_marker.add(entityCategory);
//first chunk with spawn eligibility for that category
Pair key = Pair.of(dim, entityCategory);
int spawnTries = SpawnReporter.spawn_tries.get(entityCategory);
SpawnReporter.spawn_attempts.put(key,
SpawnReporter.spawn_attempts.get(key) + spawnTries);
SpawnReporter.spawn_cap_count.put(key,
SpawnReporter.spawn_cap_count.get(key) + mobCount);
}
if (mobCount <= int_3)
{
//place 0 to indicate there were spawn attempts for a category
//if (entityCategory != EntityCategory.CREATURE || world.getServer().getTicks() % 400 == 0)
// this will only be called once every 400 ticks anyways
SpawnReporter.local_spawns.putIfAbsent(entityCategory, 0L);
//else
//full mobcaps - and key in local_spawns will be missing
}
}
return SpawnReporter.mock_spawns?Integer.MAX_VALUE:newCap;
}
@Inject(method = "tickChunks", at = @At("RETURN"))
private void onFinishSpawnWorldCycle(CallbackInfo ci)
{
if (SpawnReporter.track_spawns > 0L && SpawnReporter.local_spawns != null)
{
for (EntityCategory cat: EntityCategory.values())
{
DimensionType dim = world.dimension.getType();
Pair key = Pair.of(world.dimension.getType(), cat);
int spawnTries = SpawnReporter.spawn_tries.get(cat);
if (!SpawnReporter.local_spawns.containsKey(cat))
{
// fill mobcaps for that category so spawn got cancelled
SpawnReporter.spawn_ticks_full.put(key,
SpawnReporter.spawn_ticks_full.get(key)+ spawnTries);
}
else if (SpawnReporter.local_spawns.get(cat) > 0)
{
// tick spawned mobs for that type
SpawnReporter.spawn_ticks_succ.put(key,
SpawnReporter.spawn_ticks_succ.get(key)+spawnTries);
SpawnReporter.spawn_ticks_spawns.put(key,
SpawnReporter.spawn_ticks_spawns.get(key)+
SpawnReporter.local_spawns.get(cat));
// this will be off comparing to 1.13 as that would succeed if
// ANY tries in that round were successful.
// there will be much more difficult to mix in
// considering spawn tries to remove, as with warp
// there is little need for them anyways.
}
else // spawn no mobs despite trying
{
//tick didn's spawn mobs of that type
SpawnReporter.spawn_ticks_fail.put(key,
SpawnReporter.spawn_ticks_fail.get(key)+spawnTries);
}
}
}
SpawnReporter.local_spawns = null;
}
}
@@ -0,0 +1,5 @@
package carpet.mixins;
public class ServerChunkManager_spawnMixin
{
}
@@ -0,0 +1,36 @@
package carpet.mixins;
import carpet.CarpetSettings;
import carpet.utils.SpawnReporter;
import net.minecraft.entity.Entity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.world.SpawnHelper;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
@Mixin(SpawnHelper.class)
public abstract class SpawnHelperMixin
{
@Redirect(method = "spawnEntitiesInChunk", at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/World;spawnEntity(Lnet/minecraft/entity/Entity;)Z"
))
private static boolean catchEntitySpawn(World world, Entity entity_1)
{
if (SpawnReporter.track_spawns > 0L && SpawnReporter.local_spawns != null)
{
SpawnReporter.registerSpawn(
world.dimension.getType(),
(MobEntity) entity_1,
entity_1.getType().getCategory(),
entity_1.getBlockPos());
}
if (!SpawnReporter.mock_spawns)
return world.spawnEntity(entity_1);
return false;
}
}