Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.modificationstation.sltest.item.ItemListener;
import net.modificationstation.stationapi.api.client.gui.screen.achievement.AchievementPage;
import net.modificationstation.stationapi.api.event.achievement.AchievementRegisterEvent;
import net.modificationstation.stationapi.api.template.achievement.TemplateAchievement;

import static net.modificationstation.sltest.SLTest.NAMESPACE;

Expand All @@ -18,8 +19,8 @@ public class AchievementListener {
@EventListener
public void registerAchievements(AchievementRegisterEvent event) {
testAchievementPage = new AchievementPageTest(NAMESPACE.id("testPage"));
testAchievement = new Achievement(69696969, "sltest.testAchievement", 0, 0, ItemListener.testItem, null);
testAchievementChild = new Achievement(69696970, "sltest.testAchievementChild", 0, 2, Item.GOLDEN_APPLE, testAchievement);
testAchievement = new TemplateAchievement(NAMESPACE.id("test_achievement"), "sltest.testAchievement", 0, 0, ItemListener.testItem, null);
testAchievementChild = new TemplateAchievement(NAMESPACE.id("test_achievement_child"), "sltest.testAchievementChild", 0, 2, Item.GOLDEN_APPLE, testAchievement);
event.achievements.add(testAchievement);
event.achievements.add(testAchievementChild);
testAchievementPage.addAchievements(testAchievement, testAchievementChild);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
"BlockMixin",
"FireBlockMixin",
"LeavesBlockMixin",
"SecondaryBlockItemMixin",
"StatsMixin"
"SecondaryBlockItemMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.modificationstation.stationapi.api.event.registry;

import net.mine_diver.unsafeevents.event.EventPhases;
import net.minecraft.stat.Stat;
import net.modificationstation.stationapi.api.StationAPI;
import net.modificationstation.stationapi.api.registry.StatRegistry;

@EventPhases(StationAPI.INTERNAL_PHASE)
public class StatRegistryEvent extends RegistryEvent.EntryTypeBound<Stat, StatRegistry> {
public StatRegistryEvent() {
super(StatRegistry.INSTANCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package net.modificationstation.stationapi.api.registry;

import com.mojang.serialization.Lifecycle;
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
import net.minecraft.stat.Stat;
import net.modificationstation.stationapi.api.event.registry.RegistryAttribute;
import net.modificationstation.stationapi.api.event.registry.RegistryAttributeHolder;

import static net.modificationstation.stationapi.api.StationAPI.NAMESPACE;

public class StatRegistry extends SimpleRegistry<Stat> {
public static final RegistryKey<Registry<Stat>> KEY = RegistryKey.ofRegistry(NAMESPACE.id("stats"));
public static final StatRegistry INSTANCE = Registries.create(KEY, new StatRegistry(), Lifecycle.experimental());

public static final int ACHIEVEMENT_ID_SHIFT = 0x500000;
public static final Int2IntFunction SHIFTED_ACHIEVEMENT_ID = id -> id - ACHIEVEMENT_ID_SHIFT;
public static final int ACHIEVEMENT_AUTO_ID = SHIFTED_ACHIEVEMENT_ID.applyAsInt(AUTO_ID);

private StatRegistry() {
super(KEY, Lifecycle.experimental(), true);
RegistryAttributeHolder.get(this).addAttribute(RegistryAttribute.SYNCED);
nextId = 0x1040000;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.modificationstation.stationapi.api.stat;

import net.minecraft.stat.Stat;
import net.modificationstation.stationapi.api.registry.RegistryEntry;
import net.modificationstation.stationapi.api.registry.RemappableRawIdHolder;
import net.modificationstation.stationapi.api.util.Util;

public interface StationFlatteningStat extends RemappableRawIdHolder {
@Override
default void setRawId(int rawId) {
Util.assertImpl();
}

default RegistryEntry.Reference<Stat> getRegistryEntry() {
return Util.assertImpl();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package net.modificationstation.stationapi.impl.stat;

import net.minecraft.stat.Stat;
import net.modificationstation.stationapi.api.util.Namespace;

import java.util.Iterator;
import java.util.Map;
import java.util.NoSuchElementException;

import static net.modificationstation.stationapi.api.StationAPI.NAMESPACE;

/**
* Iterates over a set of stats, filtering out modded ones and serializing them separately.
*/
public final class ModdedStatsSerializingIterator implements Iterator<Stat> {
public static final String STATIONAPI_STATS_CHANGE_KEY = NAMESPACE.id("stats-change").toString();

private final Iterator<Stat> source;
private final StringBuilder moddedStatsSerialized;
private final Map<Stat, Integer> statCounts;

private Stat nextVanillaStat;
private boolean firstModdedStat = true;

public ModdedStatsSerializingIterator(
Iterator<Stat> source, StringBuilder moddedStatsSerialized, Map<Stat, Integer> statCounts
) {
this.source = source;
this.moddedStatsSerialized = moddedStatsSerialized;
this.statCounts = statCounts;

moddedStatsSerialized.append(" \"").append(STATIONAPI_STATS_CHANGE_KEY).append("\":[");

advance();
}

/**
* Searches for the next vanilla stat in the set and serializes all modded stats found on the way.
*/
private void advance() {
nextVanillaStat = null;

// Searching for the next vanilla stat in the set
// and serializing all modded stats found on the way
while (source.hasNext()) {
Stat candidate = source.next();

if (candidate.getRegistryEntry().registryKey().getValue().namespace == Namespace.MINECRAFT) {
nextVanillaStat = candidate;
return;
}

serializeModded(candidate);
}
}

/**
* Serializes the given stat under the modded stats array.
*
* @param stat the stat to serialize under the modded stats array
*/
private void serializeModded(Stat stat) {
if (!firstModdedStat) moddedStatsSerialized.append("},");
else firstModdedStat = false;

moddedStatsSerialized.append("\r\n {\"");
moddedStatsSerialized.append(stat.getRegistryEntry().registryKey().getValue()).append("\":").append(statCounts.get(stat));
}

@Override
public boolean hasNext() {
if (nextVanillaStat != null)
return true;

// If there are no more stats from the source iterator,
// finish serializing modded stats and terminate the loop
if (!firstModdedStat)
moddedStatsSerialized.append("}");
moddedStatsSerialized.append("\r\n ],\r\n");
return false;
}

/**
* Returns the next vanilla {@link Stat} and advances the source iterator via {@link #advance()}.
*
* @return the next vanilla {@link Stat} from the source iterator
* @throws NoSuchElementException if there are no more vanilla stats
*/
@Override
public Stat next() {
if (nextVanillaStat == null) throw new NoSuchElementException();

final Stat stat = nextVanillaStat;
advance();
return stat;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.modificationstation.stationapi.mixin.flattening;

import net.minecraft.stat.Stat;
import net.modificationstation.stationapi.api.registry.RegistryEntry;
import net.modificationstation.stationapi.api.registry.StatRegistry;
import net.modificationstation.stationapi.api.stat.StationFlatteningStat;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(Stat.class)
class StatMixin implements StationFlatteningStat {
@Mutable
@Shadow @Final public int id;
@Unique
private RegistryEntry.Reference<Stat> stationapi_registryEntry;

@ModifyVariable(
method = "<init>(ILjava/lang/String;Lnet/minecraft/stat/StatFormatter;)V",
index = 1,
at = @At(
value = "CONSTANT",
args = "intValue=0",
ordinal = 0
),
argsOnly = true
)
private int stationapi_ensureCapacity(int rawId) {
return (stationapi_registryEntry = StatRegistry.INSTANCE.createReservedEntry(rawId, (Stat) (Object) this)).reservedRawId();
}

@SuppressWarnings("AddedMixinMembersNamePattern")
@Override
@Unique
public void setRawId(int rawId) {
id = rawId;
}

@SuppressWarnings("AddedMixinMembersNamePattern")
@Override
public RegistryEntry.Reference<Stat> getRegistryEntry() {
return stationapi_registryEntry;
}
}
Loading