Skip to content

Commit

Permalink
Merge pull request #19 from kyrptonaught/master
Browse files Browse the repository at this point in the history
Add support for syncing advancements
  • Loading branch information
MrNavaStar authored Mar 17, 2022
2 parents b850fbe + a82b60a commit 95d04de
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.10.64'
id 'fabric-loom' version '0.11-SNAPSHOT'
id 'maven-publish'
}

Expand Down
16 changes: 9 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
org.gradle.jvmargs=-Xmx4G
# Fabric Properties
# check these on https://modmuss50.me/fabric.html

# check these on https://fabricmc.net/develop/
minecraft_version=1.18.1
yarn_mappings=1.18.1+build.2
loader_version=0.12.12
yarn_mappings=1.18.1+build.22
loader_version=0.13.3

# Mod Properties
mod_version=v2.1.3
mod_version=v2.2.0
maven_group=mrnavastar
archives_base_name=invsync

# Dependencies
# check this on https://modmuss50.me/fabric.html
fabric_version=0.43.1+1.18
fabric_version=0.46.6+1.18
sqlib_version=v1.2.1
cloth_version=6.0.42
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
2 changes: 1 addition & 1 deletion src/main/java/mrnavastar/invsync/InvSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void onInitialize() {
} else log(Level.INFO, "Halting initialization! You need to change some settings in the InvSync config");
}

public static void log(Level level, String message){
public static void log(Level level, String message) {
LogManager.getLogger().log(level, "[" + MODID + "] " + message);
}
}
11 changes: 11 additions & 0 deletions src/main/java/mrnavastar/invsync/InvSyncPATAddon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package mrnavastar.invsync;

import com.google.gson.JsonElement;
import net.minecraft.server.ServerAdvancementLoader;

public interface InvSyncPATAddon {

void INVSYNC$load(ServerAdvancementLoader advancementLoader, JsonElement advancementData);

JsonElement INVSYNC$save();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package mrnavastar.invsync.mixin;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;
import mrnavastar.invsync.InvSyncPATAddon;
import net.minecraft.advancement.Advancement;
import net.minecraft.advancement.AdvancementProgress;
import net.minecraft.advancement.PlayerAdvancementTracker;
import net.minecraft.server.ServerAdvancementLoader;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

@Mixin(PlayerAdvancementTracker.class)
public abstract class PlayerAdvancementTrackerMixin implements InvSyncPATAddon {

@Shadow
public abstract void clearCriteria();

@Shadow
@Final
private Map<Advancement, AdvancementProgress> advancementToProgress;

@Shadow
@Final
private Set<Advancement> visibleAdvancements;

@Shadow
@Final
private Set<Advancement> visibilityUpdates;

@Shadow
@Final
private Set<Advancement> progressUpdates;

@Shadow
private boolean dirty;

@Shadow
private @Nullable Advancement currentDisplayTab;

@Shadow
protected abstract void initProgress(Advancement advancement, AdvancementProgress progress);

@Shadow
protected abstract void rewardEmptyAdvancements(ServerAdvancementLoader advancementLoader);

@Shadow
protected abstract void updateCompleted();

@Shadow
protected abstract void beginTrackingAllAdvancements(ServerAdvancementLoader advancementLoader);

@Shadow
@Final
private static Gson GSON;

@Shadow
@Final
private static TypeToken<Map<Identifier, AdvancementProgress>> JSON_TYPE;

@Override
public void INVSYNC$load(ServerAdvancementLoader advancementLoader, JsonElement advancementData) {
this.clearCriteria();
this.advancementToProgress.clear();
this.visibleAdvancements.clear();
this.visibilityUpdates.clear();
this.progressUpdates.clear();
this.dirty = true;
this.currentDisplayTab = null;

Map<Identifier, AdvancementProgress> map = GSON.getAdapter(JSON_TYPE).fromJsonTree(advancementData);
Stream<Map.Entry<Identifier, AdvancementProgress>> stream = map.entrySet().stream().sorted(Map.Entry.comparingByValue());
for (Map.Entry<Identifier, AdvancementProgress> entry : stream.toList()) {
Advancement advancement = advancementLoader.get(entry.getKey());
if (advancement == null) continue;
this.initProgress(advancement, entry.getValue());
}
this.rewardEmptyAdvancements(advancementLoader);
this.updateCompleted();
this.beginTrackingAllAdvancements(advancementLoader);
}

@Override
public JsonElement INVSYNC$save() {
HashMap<Identifier, AdvancementProgress> map = Maps.newHashMap();
for (Map.Entry<Advancement, AdvancementProgress> entry : this.advancementToProgress.entrySet()) {
AdvancementProgress advancementProgress = entry.getValue();
if (!advancementProgress.isAnyObtained()) continue;
map.put(entry.getKey().getId(), advancementProgress);
}
return GSON.toJsonTree(map);
}
}
33 changes: 26 additions & 7 deletions src/main/java/mrnavastar/invsync/util/Converter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package mrnavastar.invsync.util;

import com.google.gson.JsonElement;
import mrnavastar.invsync.InvSyncPATAddon;
import mrnavastar.sqlib.api.DataContainer;
import net.minecraft.advancement.PlayerAdvancementTracker;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.server.ServerAdvancementLoader;
import net.minecraft.server.network.ServerPlayerEntity;

import static mrnavastar.invsync.InvSync.playerData;
Expand All @@ -23,8 +26,10 @@ public static void updatePlayerData(ServerPlayerEntity player) {
player.getInventory().selectedSlot = playerDataContainer.getInt("SELECTED_SLOT");
}

if (settings.SYNC_ENDER_CHEST) player.getEnderChestInventory().readNbtList((NbtList) playerDataContainer.getNbt("ECHEST"));
if (settings.SYNC_FOOD_LEVEL) player.getHungerManager().readNbt((NbtCompound) playerDataContainer.getNbt("HUNGER"));
if (settings.SYNC_ENDER_CHEST)
player.getEnderChestInventory().readNbtList((NbtList) playerDataContainer.getNbt("ECHEST"));
if (settings.SYNC_FOOD_LEVEL)
player.getHungerManager().readNbt((NbtCompound) playerDataContainer.getNbt("HUNGER"));
if (settings.SYNC_HEALTH) player.setHealth(playerDataContainer.getFloat("HEALTH"));
if (settings.SYNC_SCORE) player.setScore(playerDataContainer.getInt("SCORE"));

Expand All @@ -42,12 +47,19 @@ public static void updatePlayerData(ServerPlayerEntity player) {
}
}
}

if (settings.SYNC_ADVANCEMENTS) {
PlayerAdvancementTracker playerAdvancementTracker = player.getAdvancementTracker();
ServerAdvancementLoader advancementLoader = player.getServer().getAdvancementLoader();
JsonElement jsonObject = playerDataContainer.getJson("ADVANCEMENTS");
((InvSyncPATAddon) playerAdvancementTracker).INVSYNC$load(advancementLoader, jsonObject);
}
}
}
}

public static void savePlayerData(PlayerEntity player) {
if ((settings.SYNC_CREATIVE_MODE) || (!settings.SYNC_CREATIVE_MODE && !player.isCreative())) {
public static void savePlayerData(ServerPlayerEntity player) {
if (settings.SYNC_CREATIVE_MODE || !player.isCreative()) {
DataContainer playerDataContainer = playerData.get(player.getUuid());
if (playerDataContainer == null) {
playerDataContainer = playerData.createDataContainer(player.getUuid());
Expand All @@ -59,7 +71,8 @@ public static void savePlayerData(PlayerEntity player) {
playerDataContainer.put("SELECTED_SLOT", player.getInventory().selectedSlot);
}

if (settings.SYNC_ENDER_CHEST) playerDataContainer.put("ECHEST", player.getEnderChestInventory().toNbtList());
if (settings.SYNC_ENDER_CHEST)
playerDataContainer.put("ECHEST", player.getEnderChestInventory().toNbtList());

if (settings.SYNC_FOOD_LEVEL) {
NbtCompound nbt = new NbtCompound();
Expand All @@ -69,7 +82,8 @@ public static void savePlayerData(PlayerEntity player) {

if (settings.SYNC_STATUS_EFFECTS) {
NbtList effects = new NbtList();
for (StatusEffectInstance effect : player.getStatusEffects()) effects.add(effect.writeNbt(new NbtCompound()));
for (StatusEffectInstance effect : player.getStatusEffects())
effects.add(effect.writeNbt(new NbtCompound()));
playerDataContainer.put("EFFECTS", effects);
}

Expand All @@ -81,6 +95,11 @@ public static void savePlayerData(PlayerEntity player) {
playerDataContainer.put("XP", player.experienceLevel);
playerDataContainer.put("XP_PROGRESS", player.experienceProgress);
}
if (settings.SYNC_ADVANCEMENTS) {//this is modified code from: net.minecraft.advancement.PlayerAdvancementTracker.save
PlayerAdvancementTracker playerAdvancementTracker = player.getAdvancementTracker();
JsonElement advancements = ((InvSyncPATAddon) playerAdvancementTracker).INVSYNC$save();
playerDataContainer.put("ADVANCEMENTS", advancements);
}
}
}
}
37 changes: 19 additions & 18 deletions src/main/java/mrnavastar/invsync/util/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@


public class Settings implements ConfigData {
@Comment("Allowed Values: \"SQLITE\" | \"MYSQL\"")
public String DATABASE_TYPE = "SQLITE";
public String DATABASE_NAME = "InvSync";
@Comment("Allowed Values: \"SQLITE\" | \"MYSQL\"")
public String DATABASE_TYPE = "SQLITE";
public String DATABASE_NAME = "InvSync";

public String SQLITE_DIRECTORY = "/path/to/folder";
public String SQLITE_DIRECTORY = "/path/to/folder";

public String MYSQL_ADDRESS = "127.0.0.1";
public String MYSQL_PORT = "3306";
@Comment("The mod will not start if you use these as your actual credentials - please keep your data secure")
public String MYSQL_USERNAME = "username";
public String MYSQL_PASSWORD = "password";
public String MYSQL_ADDRESS = "127.0.0.1";
public String MYSQL_PORT = "3306";
@Comment("The mod will not start if you use these as your actual credentials - please keep your data secure")
public String MYSQL_USERNAME = "username";
public String MYSQL_PASSWORD = "password";

@Comment("Sync Settings")
public boolean SYNC_CREATIVE_MODE = true;
public boolean SYNC_INVENTORY = true;
public boolean SYNC_ENDER_CHEST = true;
public boolean SYNC_HEALTH = true;
public boolean SYNC_FOOD_LEVEL = true;
public boolean SYNC_XP_LEVEL = true;
public boolean SYNC_SCORE = true;
public boolean SYNC_STATUS_EFFECTS = true;
@Comment("Sync Settings")
public boolean SYNC_CREATIVE_MODE = true;
public boolean SYNC_INVENTORY = true;
public boolean SYNC_ENDER_CHEST = true;
public boolean SYNC_HEALTH = true;
public boolean SYNC_FOOD_LEVEL = true;
public boolean SYNC_XP_LEVEL = true;
public boolean SYNC_SCORE = true;
public boolean SYNC_STATUS_EFFECTS = true;
public boolean SYNC_ADVANCEMENTS = true;
}
13 changes: 10 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"version": "${version}",
"name": "InvSync",
"description": "",
"authors": ["MrNavaStar"],
"authors": [
"MrNavaStar"
],
"contact": {},
"license": "MIT",
"environment": "*",
Expand All @@ -13,9 +15,14 @@
"mrnavastar.invsync.InvSync"
]
},
"mixins": [
{
"config": "invsync.mixins.json",
"environment": "*"
}
],
"depends": {
"fabricloader": ">=0.12.12",
"fabric": "*",
"minecraft": "1.18.x"
"fabric": "*"
}
}
11 changes: 11 additions & 0 deletions src/main/resources/invsync.mixins.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"required": true,
"package": "mrnavastar.invsync.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"PlayerAdvancementTrackerMixin"
],
"injectors": {
"defaultRequire": 1
}
}

0 comments on commit 95d04de

Please sign in to comment.