Skip to content

Commit

Permalink
Add stats to bukkit registry (#270)
Browse files Browse the repository at this point in the history
  • Loading branch information
IzzelAliz committed Jun 16, 2021
1 parent 68ddb4a commit cd23381
Showing 1 changed file with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import net.minecraft.item.Item;
import net.minecraft.item.Items;
import net.minecraft.potion.Effect;
import net.minecraft.stats.StatType;
import net.minecraft.stats.Stats;
import net.minecraft.util.RegistryKey;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.Registry;
Expand All @@ -34,9 +36,11 @@
import org.bukkit.Art;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Statistic;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.craftbukkit.v.CraftCrashReport;
import org.bukkit.craftbukkit.v.CraftStatistic;
import org.bukkit.craftbukkit.v.util.CraftMagicNumbers;
import org.bukkit.craftbukkit.v.util.CraftNamespacedKey;
import org.bukkit.enchantments.Enchantment;
Expand All @@ -53,7 +57,7 @@
import java.util.Map;
import java.util.Set;

@SuppressWarnings({"unchecked", "ConstantConditions"})
@SuppressWarnings({"ConstantConditions", "deprecation"})
public class BukkitRegistry {

private static final List<Class<?>> MAT_CTOR = ImmutableList.of(int.class);
Expand All @@ -74,6 +78,7 @@ public class BukkitRegistry {
.build());
private static final Map<String, Art> ART_BY_NAME = Unsafe.getStatic(Art.class, "BY_NAME");
private static final Map<Integer, Art> ART_BY_ID = Unsafe.getStatic(Art.class, "BY_ID");
private static final BiMap<ResourceLocation, Statistic> STATS = HashBiMap.create(Unsafe.getStatic(CraftStatistic.class, "statistics"));

public static void registerAll() {
CrashReportExtender.registerCrashCallable("Arclight", () -> new CraftCrashReport().call().toString());
Expand All @@ -84,6 +89,51 @@ public static void registerAll() {
loadVillagerProfessions();
loadBiomes();
loadArts();
loadStats();
}

private static void loadStats() {
int i = Statistic.values().length;
List<Statistic> newTypes = new ArrayList<>();
Field key = Arrays.stream(Statistic.class.getDeclaredFields()).filter(it -> it.getName().equals("key")).findAny().orElse(null);
long keyOffset = Unsafe.objectFieldOffset(key);
for (StatType<?> statType : ForgeRegistries.STAT_TYPES) {
if (statType == Stats.CUSTOM) continue;
Statistic statistic = STATS.get(statType.getRegistryName());
if (statistic == null) {
String standardName = ResourceLocationUtil.standardize(statType.getRegistryName());
Statistic.Type type;
if (statType.getRegistry() == Registry.ENTITY_TYPE) {
type = Statistic.Type.ENTITY;
} else if (statType.getRegistry() == Registry.BLOCK) {
type = Statistic.Type.BLOCK;
} else if (statType.getRegistry() == Registry.ITEM) {
type = Statistic.Type.ITEM;
} else {
type = Statistic.Type.UNTYPED;
}
statistic = EnumHelper.makeEnum(Statistic.class, standardName, i, ImmutableList.of(Statistic.Type.class), ImmutableList.of(type));
Unsafe.putObject(statistic, keyOffset, statType.getRegistryName());
newTypes.add(statistic);
STATS.put(statType.getRegistryName(), statistic);
ArclightMod.LOGGER.debug("Registered {} as stats {}", statType.getRegistryName(), statistic);
i++;
}
}
for (ResourceLocation location : Registry.CUSTOM_STAT) {
Statistic statistic = STATS.get(location);
if (statistic == null) {
String standardName = ResourceLocationUtil.standardize(location);
statistic = EnumHelper.makeEnum(Statistic.class, standardName, i, ImmutableList.of(), ImmutableList.of());
Unsafe.putObject(statistic, keyOffset, location);
newTypes.add(statistic);
STATS.put(location, statistic);
ArclightMod.LOGGER.debug("Registered {} as custom stats {}", location, statistic);
i++;
}
}
EnumHelper.addEnums(Statistic.class, newTypes);
putStatic(CraftStatistic.class, "statistics", STATS);
}

private static void loadArts() {
Expand Down

0 comments on commit cd23381

Please sign in to comment.