Skip to content

Commit

Permalink
Merge redstone lamp stats on 1.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed Nov 16, 2023
1 parent 9ac17f7 commit 3b7cb84
Show file tree
Hide file tree
Showing 78 changed files with 1,651 additions and 8 deletions.
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ dependencies {
include project(":versions:1.1.0-alpha.11w49a-to-1.1.0-alpha.11w50a-client")
include project(":versions:1.1.0-alpha.11w49a-to-1.1.0-alpha.11w50a-server")
// 12w01a -> Entities.register and MinecraftServer.loadWorld signature changed
include project(":versions:1.1.0-alpha.12w01a-to-1.2.5-client")
include project(":versions:1.1.0-alpha.12w01a-to-1.2.5-server")
include project(":versions:1.1.0-alpha.12w01a-to-1.2.0-alpha.12w06a-client")
include project(":versions:1.1.0-alpha.12w01a-to-1.2.0-alpha.12w06a-server")
// 12w07a -> added redstone lamp
include project(":versions:1.2.0-alpha.12w07a-to-1.2.5-client")
include project(":versions:1.2.0-alpha.12w07a-to-1.2.5-server")
// 1.3 snapshots are skipped for now because it's kinda broken (?)
// And I don't think these are as important as other in-between versions
include project(":versions:1.3.2-to-1.4.0-alpha.12w38b")
Expand Down
6 changes: 4 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ include(":versions:1.0.0-to-1.1.0-alpha.11w48a-client")
include(":versions:1.0.1-to-1.1.0-alpha.11w48a-server")
include(":versions:1.1.0-alpha.11w49a-to-1.1.0-alpha.11w50a-client")
include(":versions:1.1.0-alpha.11w49a-to-1.1.0-alpha.11w50a-server")
include(":versions:1.1.0-alpha.12w01a-to-1.2.5-client")
include(":versions:1.1.0-alpha.12w01a-to-1.2.5-server")
include(":versions:1.1.0-alpha.12w01a-to-1.2.0-alpha.12w06a-client")
include(":versions:1.1.0-alpha.12w01a-to-1.2.0-alpha.12w06a-server")
include(":versions:1.2.0-alpha.12w07a-to-1.2.5-client")
include(":versions:1.2.0-alpha.12w07a-to-1.2.5-server")
include(":versions:1.3.2-to-1.4.0-alpha.12w38b")
include(":versions:1.4.0-alpha.12w39a-to-1.4.7")
include(":versions:1.5.0-alpha.13w01a-to-1.5.0-alpha.13w01b")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ archives_base_name = server-stats-mixins-client

# Mod Properties
minecraft_version_min = 1.1.0-alpha.12w01a
minecraft_version_max = 1.2.5
minecraft_version_max = 1.2.0-alpha.12w06a

# Minecraft Properties
minecraft_version = 12w01a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"schema_version": 1,
"quilt_loader": {
"group": "net.lostluma",
"id": "server_stats_mixins",
"version": "${version}",
"metadata": {
"name": "Server Stats Mixins",
"description": "Implementation of server-side statistics for Minecraft 1.2.5 on the client.",
"contributors": {
"LostLuma": "Owner"
},
"contact": {
"homepage": "https://go.lostluma.net/server-stats",
"issues": "https://go.lostluma.net/server-stats-issues",
"sources": "https://go.lostluma.net/server-stats-source"
},
"license": {
"name": "LGPL License",
"id": "LGPL-3.0-or-later",
"url": "https://go.lostluma.net/server-stats-license"
},
"icon": "assets/server_stats_mixins/icon.png"
},
"intermediate_mappings": "net.fabricmc:intermediary",
"depends": [
{
"id": "minecraft",
"versions": {
"all": [">=1.1.0-alpha.12.1.a", "<=1.2.1-alpha.12.6.a"]
}
}
]
},
"minecraft": {
"environment": "client"
},
"mixin": "server_stats.mixins.json",
"access_widener" : "server_stats.accesswidener",
"quilt_loom": {
"injected_interfaces": {
"net/minecraft/unmapped/C_9590849": [
"net/lostluma/server_stats/types/StatsPlayer"
],
"net/minecraft/unmapped/C_2854223": [
"net/lostluma/server_stats/types/OverridableStats"
]
}
},
"modmenu": {
"parent": "server_stats"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ archives_base_name = server-stats-mixins-server

# Mod Properties
minecraft_version_min = 1.1.0-alpha.12w01a
minecraft_version_max = 1.2.5
minecraft_version_max = 1.2.0-alpha.12w06a

# Minecraft Properties
minecraft_version = 12w01a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"schema_version": 1,
"quilt_loader": {
"group": "net.lostluma",
"id": "server_stats_mixins",
"version": "${version}",
"metadata": {
"name": "Server Stats Mixins",
"description": "Implementation of server-side statistics for Minecraft 1.1.0.",
"contributors": {
"LostLuma": "Owner"
},
"contact": {
"homepage": "https://go.lostluma.net/server-stats",
"issues": "https://go.lostluma.net/server-stats-issues",
"sources": "https://go.lostluma.net/server-stats-source"
},
"license": {
"name": "LGPL License",
"id": "LGPL-3.0-or-later",
"url": "https://go.lostluma.net/server-stats-license"
},
"icon": "assets/server_stats_mixins/icon.png"
},
"intermediate_mappings": "net.fabricmc:intermediary",
"depends": [
{
"id": "minecraft",
"versions": {
"all": [">=1.1.0-alpha.12.1.a", "<=1.2.1-alpha.12.6.a"]
}
}
]
},
"minecraft": {
"environment": "dedicated_server"
},
"mixin": "server_stats.mixins.json",
"quilt_loom": {
"injected_interfaces": {
"net/minecraft/unmapped/C_9590849": [
"net/lostluma/server_stats/types/StatsPlayer"
]
}
},
"modmenu": {
"parent": "server_stats"
}
}
49 changes: 49 additions & 0 deletions versions/1.2.0-alpha.12w07a-to-1.2.5-client/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
plugins {
id 'maven-publish'
alias libs.plugins.shadow
alias libs.plugins.quilt.loom
alias libs.plugins.ploceus
}

apply from: "${rootProject.projectDir}/gradle/common.gradle"

group = project.maven_group
version = generateVersionWithMetadata()

base {
archivesName = project.archives_base_name
}

loom {
clientOnlyMinecraftJar()

accessWidenerPath = file("src/main/resources/server_stats.accesswidener")
}

ploceus {
clientOnlyMappings()
}

dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"

mappings loom.layered {
mappings "net.ornithemc:feather:${project.feather_version}:v2"
addLayer ploceus.nestedMappings() // Required for nests
}

nests "net.ornithemc:nests:${project.nests_version}"
modImplementation libs.quilt.loader

implementation libs.gson
}

shadowJar {
configurations = [project.configurations.shadow]
relocate "com.google", "net.lostluma.server_stats.external"
}

remapJar {
inputFile.set shadowJar.archiveFile
dependsOn shadowJar
}
12 changes: 12 additions & 0 deletions versions/1.2.0-alpha.12w07a-to-1.2.5-client/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Mod Properties
archives_base_name = server-stats-mixins-client

# Mod Properties
minecraft_version_min = 1.1.0-alpha.12w07a
minecraft_version_max = 1.2.5

# Minecraft Properties
minecraft_version = 12w07a

nests_version = 12w07a-client+build.1
feather_version = 12w07a-client+build.11
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.lostluma.server_stats;

public class Constants {
public static final String MOD_ID = "server_stats";
public static final String STATS_PACKET_CHANNEL = MOD_ID + "|s";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.lostluma.server_stats.mixin.client;

import net.lostluma.server_stats.stats.Stats;
import net.minecraft.client.entity.living.player.InputPlayerEntity;
import net.minecraft.client.entity.living.player.LocalPlayerEntity;
import net.minecraft.entity.living.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin({ InputPlayerEntity.class, LocalPlayerEntity.class })
public class LocalPlayerEntityMixin {
private PlayerEntity getPlayer() {
return (PlayerEntity) (Object) this;
}

@Inject(method = "incrementStat(Lnet/minecraft/stat/Stat;I)V", at = @At("HEAD"))
private void incrementStat(net.minecraft.stat.Stat vanillaStat, int amount, CallbackInfo callbackInfo) {
if (vanillaStat == null) {
return;
}

var stat = Stats.byVanillaId(vanillaStat.id);

if (stat != null) {
this.getPlayer().server_stats$incrementStat(stat, amount);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.lostluma.server_stats.mixin.client;

import net.lostluma.server_stats.stats.ServerPlayerStats;
import net.lostluma.server_stats.stats.Stats;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.living.player.InputPlayerEntity;
import net.minecraft.world.WorldSettings;
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;

@Mixin(Minecraft.class)
public class MinecraftMixin {
@Shadow
public InputPlayerEntity player;

@Inject(method = "<init>", at = @At("TAIL"))
private void onInit(CallbackInfo callbackInfo) {
Stats.init();
}

@Inject(method = "startGame", at = @At("HEAD"))
private void startGame(String worldDir, String worldName, WorldSettings worldSettings, CallbackInfo callbackInfo) {
ServerPlayerStats.setWorldDirectory(String.format("saves/%s", worldDir));
}

@Inject(method = "m_4977780", at = @At("TAIL"))
private void changeDimension(int dimension, CallbackInfo callbackInfo) {
this.player.server_stats$saveStats();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package net.lostluma.server_stats.mixin.client;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.lostluma.server_stats.Constants;
import net.minecraft.client.Minecraft;
import net.minecraft.client.network.handler.ClientNetworkHandler;
import net.minecraft.network.PacketHandler;
import net.minecraft.network.packet.CustomPayloadPacket;
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 java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.Map;

@Mixin(PacketHandler.class)
public class PacketHandlerMixin {
@Inject(method = "handleCustomPayload", at = @At("HEAD"), cancellable = true)
private void handleCustomPayload(CustomPayloadPacket packet, CallbackInfo callbackInfo) {
if (!packet.channel.equals(Constants.STATS_PACKET_CHANNEL)) {
return;
}

var data = new String(packet.data, StandardCharsets.UTF_8);

Type type = new TypeToken<Map<String, Integer>>(){}.getType();
Map<String, Integer> result = new Gson().fromJson(data, type);

Minecraft.INSTANCE.statHandler.player_stats$override(result);
callbackInfo.cancel();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.lostluma.server_stats.mixin.client;

import net.lostluma.server_stats.types.OverridableStats;
import net.minecraft.stat.PlayerStats;
import net.minecraft.stat.Stat;
import org.jetbrains.annotations.Nullable;
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 java.util.Collections;
import java.util.Map;

@Mixin(PlayerStats.class)
public class PlayerStatsMixin implements OverridableStats {
@Shadow
private Map<Stat, Integer> stats;

@Inject(method = "<init>", at = @At("TAIL"))
private void onInit(CallbackInfo callbackInfo) {
this.stats = Collections.synchronizedMap(this.stats);
}

@Override
public void player_stats$override(Map<String, Integer> override) {
// Reset all stats for the case where the world joined has some stats not set
this.stats.keySet().removeAll(
this.stats.keySet().stream().filter(integer -> !integer.isAchievement()).toList()
);

override.forEach((key, value) -> {
var stat = this.player_stats$getVanillaStat(key);

if (stat != null) {
this.stats.put(stat, value);
} else {
System.out.println("No client-side stat found for key " + key + ".");
}
});
}

private @Nullable Stat player_stats$getVanillaStat(String key) {
var stat = net.lostluma.server_stats.stats.Stats.byKey(key);

if (stat == null || stat.vanillaId == null) {
return null;
} else {
return net.minecraft.stat.Stats.byKey(stat.vanillaId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.lostluma.server_stats.mixin.common;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.lostluma.server_stats.stats.Stats;
import net.minecraft.entity.Entities;

@Mixin(Entities.class)
public class EntitiesMixin {
@Inject(method = "register", at = @At("TAIL"))
private static void registerWithSpawnEgg(Class<?> type, String key, int id, CallbackInfo callbackInfo) {
Stats.createEntityKillStat(key);
Stats.createKilledByEntityStat(key);
}
}
Loading

0 comments on commit 3b7cb84

Please sign in to comment.