Remove config module

Is unused without pulsar
This commit is contained in:
KnightMiner committed 2020-08-03 00:25:56 -05:00
1 parent bf801e8f8c
commit f0e5bf0b81
7 files changed
+3 -624

No files matched your search

+3 -23
View File
@@ -22,13 +22,11 @@ buildscript {
}
dependencies {
classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true
classpath 'com.github.jengelman.gradle.plugins:shadow:5.2.0'
classpath 'io.freefair.gradle:lombok-plugin:4.1.6'
}
}
apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'maven'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
@@ -126,7 +124,7 @@ minecraft {
workingDirectory project.file('run')
// Recommended logging data for a userdev environment
property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP,CORE,PULSAR'
property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP,CORE'
// Recommended logging level for the console
property 'forge.logging.console.level', 'debug'
@@ -143,10 +141,9 @@ minecraft {
}
configurations {
shadow
compile.extendsFrom shadow
deployerJars
}
dependencies {
minecraft 'net.minecraftforge:forge:' + minecraft_version + '-' + forge_version
@@ -154,10 +151,6 @@ dependencies {
compileOnly fg.deobf("mezz.jei:jei-${minecraft_version}:${jei_version}:api")
// at runtime, use the full JEI jar
runtimeOnly fg.deobf("mezz.jei:jei-${minecraft_version}:${jei_version}")
// Configuration
shadow "ninja.leaping.configurate:configurate-hocon:${configurate_version}"
//shade "ninja.leaping.configurate:configurate-gson:${configurate_version}"
}
processResources {
@@ -187,23 +180,10 @@ jar {
"Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
])
}
dependsOn shadowJar
enabled = false
}
jar.finalizedBy('reobfJar')
shadowJar {
archiveName = tasks.jar.archiveName
configurations = [project.configurations.shadow]
relocate 'ninja.leaping.configurate', 'slimeknights.mantle.configurate'
relocate 'com.typesafe', 'slimeknights.mantle.typesafe'
dependencies {
exclude(dependency('com.google.guava:guava:'))
}
}
// because the normal output has been made to be obfuscated
task deobfJar(type: Jar) {
from sourceSets.main.output
@@ -1,85 +0,0 @@
package slimeknights.mantle.config;
import com.google.common.reflect.TypeToken;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.function.Function;
public abstract class AbstractConfig {
List<AbstractConfigFile> configFileList = new ArrayList<>();
private List<Function<?, ?>> configFiles = new ArrayList<>();
public void save() {
this.configFiles.forEach(function -> function.apply(null));
}
public <T extends AbstractConfigFile> T load(T configFile, Class<T> clazz) {
try {
CommentedConfigurationNode node = configFile.load();
T val = node.getValue(TypeToken.of(clazz), configFile);
val.loader = configFile.loader;
val.file = configFile.file;
val.insertDefaults();
val.setConfigVersion();
this.configFileList.add(val);
// note: this is a workaround for generics because the generic info has to match the same class
this.configFiles.add(configFile1 -> {
this.save(val, clazz);
return true;
});
return val;
}
catch (IOException | ObjectMappingException e) {
e.printStackTrace();
}
return configFile;
}
public <T extends AbstractConfigFile> void save(T configFile, Class<T> clazz) {
if (configFile.needsSaving()) {
try {
CommentedConfigurationNode node = configFile.load();
node.setValue(TypeToken.of(clazz), configFile);
configFile.save(node);
}
catch (IOException | ObjectMappingException e) {
e.printStackTrace();
}
}
configFile.clearNeedsSaving();
}
/**
* syncs the data to the current config
* returns true if data was changed
*/
public static boolean syncConfig(AbstractConfig config, List<AbstractConfigFile> files) {
boolean changed = false;
if (config.configFileList.size() != files.size()) {
return false;
}
Iterator<AbstractConfigFile> iterLocal = config.configFileList.iterator();
Iterator<AbstractConfigFile> iterRemote = files.iterator();
while (iterLocal.hasNext() && iterRemote.hasNext()) {
changed |= iterLocal.next().sync(iterRemote.next());
}
if (changed) {
config.save();
}
return changed;
}
}
@@ -1,243 +0,0 @@
package slimeknights.mantle.config;
import com.google.common.reflect.TypeToken;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IForgeRegistry;
import net.minecraftforge.registries.IForgeRegistryEntry;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.ConfigurationOptions;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import ninja.leaping.configurate.loader.AtomicFiles;
import ninja.leaping.configurate.loader.ConfigurationLoader;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
import javax.annotation.Nullable;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public abstract class AbstractConfigFile implements Serializable {
private static boolean initialized = false;
File file;
ConfigurationLoader<CommentedConfigurationNode> loader;
// default value is TRUE since most people will forget to set it to true when it needs saving anyway
// if you want to use it, set it to false in your constructor or something ;o
private boolean needsSaving = true;
@Setting("_VERSION")
private int configVersion;
// required constructor for deserialization
public AbstractConfigFile() {
this.file = null;
this.loader = null;
}
public AbstractConfigFile(File configFolder, String name) {
this(new File(configFolder, name + ".cfg"));
}
public AbstractConfigFile(File configFile) {
configFile.getParentFile().mkdirs();
this.file = configFile;
this.loader = HoconConfigurationLoader.builder().setFile(this.file).build();
}
public CommentedConfigurationNode load() throws IOException {
return this.loader.load(ConfigurationOptions.defaults().setShouldCopyDefaults(true));
}
public void save(ConfigurationNode node) throws IOException {
this.loader.save(node);
}
public String getName() {
return this.file.getName();
}
public abstract void insertDefaults();
/**
* Return a number for the config version.
* If the version number differs from the one in the file the file will be updated.
* So if you add new fields etc. update the number
*/
protected abstract int getConfigVersion();
void setConfigVersion() {
if (this.configVersion != this.getConfigVersion()) {
this.configVersion = this.getConfigVersion();
this.setNeedsSaving();
}
}
public AbstractConfigFile loadFromPacket(byte[] packetData) {
ConfigurationLoader<CommentedConfigurationNode> packetDataLoader = HoconConfigurationLoader
.builder()
.setSource(() -> new BufferedReader(new InputStreamReader(new ByteArrayInputStream(packetData))))
.setSink(AtomicFiles.createAtomicWriterFactory(this.file.toPath(), StandardCharsets.UTF_8))
.build();
try {
CommentedConfigurationNode node = packetDataLoader.load(ConfigurationOptions.defaults().setShouldCopyDefaults(true));
try {
return node.getValue(TypeToken.of(this.getClass()));
}
catch (ObjectMappingException e) {
e.printStackTrace();
}
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
if (type != AbstractConfigFile.class) {
fields.addAll(Arrays.asList(type.getDeclaredFields()));
if (type.getSuperclass() != null && AbstractConfigFile.class.isAssignableFrom(type.getSuperclass())) {
fields = getAllFields(fields, type.getSuperclass());
}
}
return fields;
}
@Nullable
public byte[] getPacketData() {
try {
return Files.readAllBytes(this.file.toPath());
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
public boolean sync(AbstractConfigFile other) {
return this.sync(other, this);
}
public boolean sync(Object other, Object that) {
if (other.getClass() != that.getClass()) {
return false;
}
List<Field> fieldsToProcess = new ArrayList<>();
getAllFields(fieldsToProcess, that.getClass());
for (Field field : fieldsToProcess) {
this.syncField(other, that, field);
}
return this.needsSaving();
}
private void syncField(Object other, Object that, Field field) {
try {
// don't sync transient fields
if (Modifier.isTransient(field.getModifiers())) {
return;
}
if (Modifier.isStatic(field.getModifiers())) {
return;
}
if (!field.isAccessible()) {
field.setAccessible(true);
}
Object original = field.get(that);
Object remote = field.get(other);
// is this a subclass that contains entries itself?
if (field.getType().isAnnotationPresent(ConfigSerializable.class)) {
this.sync(remote, original);
}
else {
if (!original.equals(remote)) {
field.set(that, remote);
this.setNeedsSaving();
}
}
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public void setNeedsSaving() {
this.needsSaving = true;
}
public boolean needsSaving() {
return this.needsSaving;
}
public void clearNeedsSaving() {
this.needsSaving = false;
}
public static void init() {
if (initialized) {
return;
}
// item and block serializer/deserializer
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(Block.class), new RegistrySerializer<Block>() {
@Override
IForgeRegistry<Block> getRegistry() {
return ForgeRegistries.BLOCKS;
}
});
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(Item.class), new RegistrySerializer<Item>() {
@Override
IForgeRegistry<Item> getRegistry() {
return ForgeRegistries.ITEMS;
}
});
TypeSerializers.getDefaultSerializers().registerType(TypeToken.of(BlockState.class), BlockState.SERIALIZER);
initialized = true;
}
private static abstract class RegistrySerializer<T extends IForgeRegistryEntry<T>> implements TypeSerializer<T> {
// done at runtime so registry changes from joining servers take effect
abstract IForgeRegistry<T> getRegistry();
@Override
public T deserialize(TypeToken<?> typeToken, ConfigurationNode configurationNode) throws ObjectMappingException {
return this.getRegistry().getValue(new ResourceLocation(configurationNode.getString()));
}
@Override
public void serialize(TypeToken<?> typeToken, T t, ConfigurationNode configurationNode) throws ObjectMappingException {
configurationNode.setValue(t.getRegistryName());
}
}
}
@@ -1,54 +0,0 @@
package slimeknights.mantle.config;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.network.NetworkEvent;
import slimeknights.mantle.network.packet.ISimplePacket;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
@SuppressWarnings("WeakerAccess")
public abstract class AbstractConfigSyncPacket implements ISimplePacket {
private List<AbstractConfigFile> config;
public AbstractConfigSyncPacket(PacketBuffer buffer) {
this.config = new ArrayList<>();
for (AbstractConfigFile configFile : this.getConfig().configFileList) {
int length = buffer.readInt();
byte[] data = new byte[length];
buffer.readBytes(data);
this.config.add(configFile.loadFromPacket(data));
}
}
public AbstractConfigSyncPacket() {}
protected boolean sync() {
return AbstractConfig.syncConfig(this.getConfig(), this.config);
}
protected abstract AbstractConfig getConfig();
@Override
public void encode(PacketBuffer buf) {
for (AbstractConfigFile configFile : this.getConfig().configFileList) {
byte[] data = configFile.getPacketData();
if (data != null) {
buf.writeInt(data.length);
buf.writeBytes(data);
}
}
}
@Override
public void handle(Supplier<NetworkEvent.Context> context) {
context.get().enqueueWork(() -> DistExecutor.unsafeRunForDist(() -> this::sync, () -> {
throw new UnsupportedOperationException("Trying to sync client configs to the server. You registered the packet for the wrong side.");
}));
context.get().setPacketHandled(true);
}
}
@@ -1,91 +0,0 @@
package slimeknights.mantle.config;
import com.google.common.reflect.TypeToken;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistries;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializer;
import java.util.Objects;
public class BlockState {
private static final BlockState MISSING = new BlockState(Blocks.AIR, "");
public transient static final TypeSerializer<BlockState> SERIALIZER = new TypeSerializer<BlockState>() {
@Override
public BlockState deserialize(TypeToken<?> typeToken, ConfigurationNode configurationNode) throws ObjectMappingException {
String val = configurationNode.getString();
String[] parts = val.split(":");
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(parts[0], parts[1]));
if (block == null || block == Blocks.AIR) {
return MISSING;
}
String state = "";
if (parts.length > 2) {
state = parts[2];
}
return new BlockState(block, state);
}
@Override
public void serialize(TypeToken<?> typeToken, BlockState blockMeta, ConfigurationNode configurationNode) throws ObjectMappingException {
configurationNode.setValue(blockMeta.toString());
}
};
public Block block;
public String state;
public BlockState() {
}
public BlockState(Block block, String state) {
this.block = block;
this.state = state;
}
public static BlockState of(net.minecraft.block.BlockState state) {
return new BlockState(state.getBlock(), state.toString());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || this.getClass() != o.getClass()) {
return false;
}
BlockState blockState = (BlockState) o;
if (!this.state.equalsIgnoreCase(blockState.state)) {
return false;
}
return this.block != null ? this.block.equals(blockState.block) : blockState.block == null;
}
@Override
public int hashCode() {
int result = this.block != null ? this.block.hashCode() : 0;
result = 31 * result + this.state.hashCode();
return result;
}
@Override
public String toString() {
String val = Objects.requireNonNull(this.block.getRegistryName()).toString();
if (!this.state.equals("")) {
val += ":" + this.state;
}
return val;
}
}
@@ -1,121 +0,0 @@
package slimeknights.mantle.config;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Util;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.loading.FMLPaths;
import net.minecraftforge.fml.network.NetworkDirection;
import slimeknights.mantle.network.NetworkWrapper;
import java.io.File;
class ExampleSync {
static NetworkWrapper networkWrapper;
static ExampleSync INSTANCE;
static void setup() {
networkWrapper = new NetworkWrapper(new ResourceLocation("mantle:example"));
networkWrapper.registerPacket(ExampleSyncPacketImpl.class, ExampleSyncPacketImpl::new, NetworkDirection.PLAY_TO_CLIENT);
INSTANCE = new ExampleSync();
}
@OnlyIn(Dist.CLIENT)
private static boolean needsRestart;
@SubscribeEvent
@OnlyIn(Dist.DEDICATED_SERVER)
public void playerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getPlayer() instanceof ServerPlayerEntity && FMLEnvironment.dist.isDedicatedServer()) {
ExampleSyncPacketImpl packet = new ExampleSyncPacketImpl();
networkWrapper.sendTo(packet, (ServerPlayerEntity) event.getPlayer());
}
}
@SubscribeEvent
@OnlyIn(Dist.CLIENT)
public void playerJoinedWorld(TickEvent.ClientTickEvent event) {
ClientPlayerEntity player = Minecraft.getInstance().player;
assert player != null;
if (needsRestart) {
player.sendMessage(new StringTextComponent("Configs synced with server. Configs require a restart"), Util.DUMMY_UUID);
}
else {
player.sendMessage(new StringTextComponent("Configs synced with server."), Util.DUMMY_UUID);
}
MinecraftForge.EVENT_BUS.unregister(this);
}
static class ExampleConfig extends AbstractConfig {
static ExampleConfig INSTANCE = new ExampleConfig();
ExampleConfigFile exampleConfigFile;
// call from preinit or something
public void onPreInit(final FMLCommonSetupEvent event) {
this.exampleConfigFile = this.load(new ExampleConfigFile(FMLPaths.CONFIGDIR.get().toFile()), ExampleConfigFile.class);
// register this serverside to sync
if (FMLEnvironment.dist.isDedicatedServer()) {
MinecraftForge.EVENT_BUS.register(INSTANCE);
}
}
}
static class ExampleConfigFile extends AbstractConfigFile {
public ExampleConfigFile(File configFolder) {
super(configFolder, "exampleconfigfile");
}
@Override
public void insertDefaults() {
// no default values that need to initialized dynamically
}
@Override
protected int getConfigVersion() {
return 1;
}
}
static class ExampleSyncPacketImpl extends AbstractConfigSyncPacket {
public ExampleSyncPacketImpl(PacketBuffer buffer) {
super(buffer);
}
public ExampleSyncPacketImpl() {
super();
}
@Override
protected AbstractConfig getConfig() {
return ExampleConfig.INSTANCE;
}
@Override
protected boolean sync() {
if (super.sync()) {
// clientside register only
MinecraftForge.EVENT_BUS.register(INSTANCE);
return true;
}
return false;
}
}
}
@@ -1,7 +0,0 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package slimeknights.mantle.config;
import mcp.MethodsReturnNonnullByDefault;
import javax.annotation.ParametersAreNonnullByDefault;