Skip to content

Commit

Permalink
Combatify 1.2.0 1.20.5-rc2 Beta 5 - Syncing prevents changing config,…
Browse files Browse the repository at this point in the history
… and begin Items Config GUI
  • Loading branch information
Alexandra-Myers committed Apr 21, 2024
1 parent 411015c commit 9ad3cd4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
50 changes: 38 additions & 12 deletions src/main/java/net/atlas/combatify/config/AtlasConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,19 @@ public final void load() {
configJsonObject = JsonParser.parseReader(new JsonReader(new FileReader(configFile))).getAsJsonObject();
for (EnumHolder<?> enumHolder : enumValues)
if (configJsonObject.has(enumHolder.heldValue.name))
enumHolder.setValue(getString(configJsonObject, enumHolder.heldValue.name));
enumHolder.setValueAndResetManaged(getString(configJsonObject, enumHolder.heldValue.name));
for (StringHolder stringHolder : stringValues)
if (configJsonObject.has(stringHolder.heldValue.name))
stringHolder.setValue(getString(configJsonObject, stringHolder.heldValue.name));
stringHolder.setValueAndResetManaged(getString(configJsonObject, stringHolder.heldValue.name));
for (BooleanHolder booleanHolder : booleanValues)
if (configJsonObject.has(booleanHolder.heldValue.name))
booleanHolder.setValue(getBoolean(configJsonObject, booleanHolder.heldValue.name));
booleanHolder.setValueAndResetManaged(getBoolean(configJsonObject, booleanHolder.heldValue.name));
for (IntegerHolder integerHolder : integerValues)
if (configJsonObject.has(integerHolder.heldValue.name))
integerHolder.setValue(getInt(configJsonObject, integerHolder.heldValue.name));
integerHolder.setValueAndResetManaged(getInt(configJsonObject, integerHolder.heldValue.name));
for (DoubleHolder doubleHolder : doubleValues)
if (configJsonObject.has(doubleHolder.heldValue.name))
doubleHolder.setValue(getDouble(configJsonObject, doubleHolder.heldValue.name));
doubleHolder.setValueAndResetManaged(getDouble(configJsonObject, doubleHolder.heldValue.name));
loadExtra(configJsonObject);
} catch (IOException | IllegalStateException e) {
e.printStackTrace();
Expand Down Expand Up @@ -282,6 +282,7 @@ public static abstract class ConfigHolder<T> {
public final StreamCodec<ByteBuf, T> codec;
public final BiConsumer<JsonWriter, T> update;
public boolean restartRequired = false;
public boolean serverManaged = false;
public Supplier<Optional<Component[]>> tooltip = Optional::empty;

public ConfigHolder(ConfigValue<T> value, StreamCodec<ByteBuf, T> codec, BiConsumer<JsonWriter, T> update) {
Expand All @@ -307,10 +308,15 @@ public void readFromBuf(FriendlyByteBuf buf) {
return;
heldValue.emitChanged(newValue);
value = newValue;
serverManaged = true;
}
public boolean isNotValid(T newValue) {
return !heldValue.isValid(newValue);
}
public void setValueAndResetManaged(T newValue) {
setValue(newValue);
serverManaged = false;
}
public void setValue(T newValue) {
if (isNotValid(newValue))
return;
Expand Down Expand Up @@ -363,7 +369,13 @@ public void encode(ByteBuf object, E object2) {
this.clazz = clazz;
this.names = names;
}
public void setValue(String name) {

public void setValueAndResetManaged(String name) {
setValue(name);
serverManaged = false;
}

public void setValue(String name) {
setValue(Enum.valueOf(clazz, name.toUpperCase(Locale.ROOT)));
}

Expand Down Expand Up @@ -463,20 +475,30 @@ public void reloadFromDefault() {
JsonObject configJsonObject = JsonParser.parseReader(new JsonReader(new InputStreamReader(getDefaultedConfig()))).getAsJsonObject();

for (EnumHolder<?> enumHolder : enumValues)
if (configJsonObject.has(enumHolder.heldValue.name))
if (configJsonObject.has(enumHolder.heldValue.name)) {
enumHolder.setValue(getString(configJsonObject, enumHolder.heldValue.name));
enumHolder.serverManaged = true;
}
for (StringHolder stringHolder : stringValues)
if (configJsonObject.has(stringHolder.heldValue.name))
if (configJsonObject.has(stringHolder.heldValue.name)) {
stringHolder.setValue(getString(configJsonObject, stringHolder.heldValue.name));
stringHolder.serverManaged = true;
}
for (BooleanHolder booleanHolder : booleanValues)
if (configJsonObject.has(booleanHolder.heldValue.name))
if (configJsonObject.has(booleanHolder.heldValue.name)) {
booleanHolder.setValue(getBoolean(configJsonObject, booleanHolder.heldValue.name));
booleanHolder.serverManaged = true;
}
for (IntegerHolder integerHolder : integerValues)
if (configJsonObject.has(integerHolder.heldValue.name))
if (configJsonObject.has(integerHolder.heldValue.name)) {
integerHolder.setValue(getInt(configJsonObject, integerHolder.heldValue.name));
integerHolder.serverManaged = true;
}
for (DoubleHolder doubleHolder : doubleValues)
if (configJsonObject.has(doubleHolder.heldValue.name))
if (configJsonObject.has(doubleHolder.heldValue.name)) {
doubleHolder.setValue(getDouble(configJsonObject, doubleHolder.heldValue.name));
doubleHolder.serverManaged = true;
}
loadExtra(configJsonObject);
}

Expand All @@ -496,7 +518,11 @@ public void addMember(ConfigHolder<?> member) {

public List<AbstractConfigListEntry<?>> membersAsCloth() {
List<AbstractConfigListEntry<?>> transformed = new ArrayList<>();
members.forEach(configHolder -> transformed.add(configHolder.transformIntoConfigEntry()));
members.forEach(configHolder -> {
AbstractConfigListEntry<?> entry = configHolder.transformIntoConfigEntry();
entry.setEditable(!configHolder.serverManaged);
transformed.add(entry);
});
return transformed;
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/net/atlas/combatify/config/ItemConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.google.common.collect.HashBiMap;
import com.google.gson.*;
import com.google.gson.stream.JsonWriter;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.gui.entries.MultiElementListEntry;
import net.atlas.combatify.Combatify;
import net.atlas.combatify.extensions.ExtendedTier;
import net.atlas.combatify.item.WeaponType;
Expand All @@ -17,6 +20,7 @@
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.damagesource.DamageSource;
Expand All @@ -25,6 +29,7 @@
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
Expand Down Expand Up @@ -692,7 +697,22 @@ public void handleExtraSync(NetworkingHandler.AtlasConfigPacket packet, LocalPla

@Override
public Screen createScreen(Screen prevScreen) {
return null;
ConfigBuilder builder = ConfigBuilder.create()
.setTitle(Component.translatable("text.config.combatify-items.title"))
.transparentBackground()
.setSavingRunnable(() -> {
try {
saveConfig();
} catch (IOException e) {
Combatify.LOGGER.error("Failed to save combatify:combatify-items config file!");
e.printStackTrace();
}
});
if (prevScreen != null) builder.setParentScreen(prevScreen);
ConfigCategory configCategory = builder.getOrCreateCategory(Component.translatable("text.config.combatify-items.title"));
builder.setFallbackCategory(configCategory);

return builder.build();
}

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/combatify/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
"options.swordBlockStyle" : "1.8 Sword Blocking Animation",
"options.fishingRodLegacy" : "1.7 Fishing Rod",
"options.shieldIndicator" : "Shield Indicator",
"text.config.combatify-items.title": "Combatify Items",
"text.config.combatify-beta.title": "Combatify",
"text.config.combatify-beta.reset": "Reset",
"text.config.combatify-beta.category.cts_booleans": "CTS Booleans",
Expand Down

0 comments on commit 9ad3cd4

Please sign in to comment.