0
0
mirror of https://github.com/Wurst-Imperium/Wurst7.git synced 2024-09-19 17:02:13 +02:00

Add settings & fix enabled hacks not loading

This commit is contained in:
Alexander 2019-07-14 16:44:24 +02:00
parent c89c300b97
commit abc3cb7ab4
7 changed files with 268 additions and 25 deletions

View File

@ -7,14 +7,22 @@
*/
package net.wurstclient;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import net.minecraft.client.MinecraftClient;
import net.wurstclient.hack.HackCategory;
import net.wurstclient.settings.Setting;
public abstract class Feature
{
protected static final WurstClient WURST = WurstClient.INSTANCE;
protected static final MinecraftClient MC = WurstClient.MC;
private final LinkedHashMap<String, Setting> settings =
new LinkedHashMap<>();
public abstract String getName();
public abstract String getDescription();
@ -33,4 +41,20 @@ public abstract class Feature
{
return false;
}
public final Map<String, Setting> getSettings()
{
return Collections.unmodifiableMap(settings);
}
protected final void addSetting(Setting setting)
{
String key = setting.getName().toLowerCase();
if(settings.containsKey(key))
throw new IllegalArgumentException(
"Duplicate setting: " + getName() + " " + key);
settings.put(key, setting);
}
}

View File

@ -22,6 +22,7 @@ import net.wurstclient.events.KeyPressListener;
import net.wurstclient.hack.HackList;
import net.wurstclient.keybinds.KeybindList;
import net.wurstclient.keybinds.KeybindProcessor;
import net.wurstclient.settings.SettingsFile;
public enum WurstClient
{
@ -31,6 +32,7 @@ public enum WurstClient
private WurstAnalytics analytics;
private EventManager eventManager;
private SettingsFile settingsFile;
private HackList hax;
private CmdList cmds;
private ClickGui gui;
@ -52,11 +54,15 @@ public enum WurstClient
eventManager = new EventManager(this);
Path enabledHacksFile = wurstFolder.resolve("enabled-hacks.json");
Path settingsFile = wurstFolder.resolve("settings.json");
hax = new HackList(enabledHacksFile, settingsFile);
hax = new HackList(enabledHacksFile);
hax.loadEnabledHacks();
cmds = new CmdList();
Path settingsFile = wurstFolder.resolve("settings.json");
this.settingsFile = new SettingsFile(settingsFile, hax, cmds);
this.settingsFile.load();
Path keybindsFile = wurstFolder.resolve("keybinds.json");
KeybindList keybinds = new KeybindList(keybindsFile);
@ -102,6 +108,11 @@ public enum WurstClient
return eventManager;
}
public void saveSettings()
{
settingsFile.save();
}
public HackList getHax()
{
return hax;

View File

@ -17,10 +17,6 @@ public abstract class Hack extends Feature
private final String description;
private HackCategory category;
// TODO
// private final LinkedHashMap<String, Setting> settings =
// new LinkedHashMap<>();
private boolean enabled;
private final boolean stateSaved =
!getClass().isAnnotationPresent(DontSaveState.class);
@ -59,23 +55,6 @@ public abstract class Hack extends Feature
this.category = category;
}
// TODO
// public final Map<String, Setting> getSettings()
// {
// return Collections.unmodifiableMap(settings);
// }
//
// protected final void addSetting(Setting setting)
// {
// String key = setting.getName().toLowerCase();
//
// if(settings.containsKey(key))
// throw new IllegalArgumentException(
// "Duplicate setting: " + name + " " + key);
//
// settings.put(key, setting);
// }
@Override
public final boolean isEnabled()
{

View File

@ -156,10 +156,9 @@ public final class HackList
new TreeMap<>((o1, o2) -> o1.compareToIgnoreCase(o2));
private final EnabledHacksFile enabledHacksFile;
public HackList(Path enabledHacksFile, Path settingsFile)
public HackList(Path enabledHacksFile)
{
this.enabledHacksFile = new EnabledHacksFile(enabledHacksFile);
this.enabledHacksFile.load(this);
try
{
@ -180,6 +179,11 @@ public final class HackList
}
}
public void loadEnabledHacks()
{
enabledHacksFile.load(this);
}
public void saveEnabledHax()
{
enabledHacksFile.save(this);

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.settings;
import java.util.Objects;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.wurstclient.clickgui.Component;
public abstract class Setting
{
private final String name;
private final String description;
public Setting(String name, String description)
{
this.name = Objects.requireNonNull(name);
this.description = Objects.requireNonNull(description);
}
public final String getName()
{
return name;
}
public final String getDescription()
{
return description;
}
public abstract Component getComponent();
public abstract void fromJson(JsonElement json);
public abstract JsonElement toJson();
public void legacyFromJson(JsonObject json)
{
}
public void update()
{
}
// public void addToFeatureScreen(NavigatorFeatureScreen featureScreen)
// {
//
// }
//
// public abstract ArrayList<PossibleKeybind> getPossibleKeybinds(
// String featureName);
}

View File

@ -0,0 +1,148 @@
/*
* Copyright (C) 2014 - 2019 | Wurst-Imperium | All rights reserved.
*
* This source code is subject to the terms of the GNU General Public
* License, version 3. If a copy of the GPL was not distributed with this
* file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt
*/
package net.wurstclient.settings;
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.wurstclient.Feature;
import net.wurstclient.command.CmdList;
import net.wurstclient.command.Command;
import net.wurstclient.hack.Hack;
import net.wurstclient.hack.HackList;
import net.wurstclient.util.json.JsonException;
import net.wurstclient.util.json.JsonUtils;
import net.wurstclient.util.json.WsonObject;
public final class SettingsFile
{
private final Path path;
private final Map<String, Feature> featuresWithSettings;
private boolean disableSaving;
public SettingsFile(Path path, HackList hax, CmdList cmds)
{
this.path = path;
featuresWithSettings = createFeatureMap(hax, cmds);
}
private Map<String, Feature> createFeatureMap(HackList hax, CmdList cmds)
{
LinkedHashMap<String, Feature> map = new LinkedHashMap<>();
for(Hack hack : hax.getAllHax())
if(!hack.getSettings().isEmpty())
map.put(hack.getName(), hack);
for(Command cmd : cmds.getAllCmds())
if(!cmd.getSettings().isEmpty())
map.put("." + cmd.getName(), cmd);
return Collections.unmodifiableMap(map);
}
public void load()
{
try
{
WsonObject wson = JsonUtils.parseWsonObject(path);
loadSettings(wson);
}catch(NoSuchFileException e)
{
// The file doesn't exist yet. No problem, we'll create it later.
}catch(IOException | JsonException e)
{
System.out.println("Couldn't load " + path.getFileName());
e.printStackTrace();
}
save();
}
private void loadSettings(WsonObject wson)
{
try
{
disableSaving = true;
for(Entry<String, JsonObject> e : wson.getAllJsonObjects()
.entrySet())
{
Feature feature = featuresWithSettings.get(e.getKey());
if(feature == null)
continue;
loadSettings(feature, e.getValue());
}
}finally
{
disableSaving = false;
}
}
private void loadSettings(Feature feature, JsonObject json)
{
Map<String, Setting> settings = feature.getSettings();
for(Entry<String, JsonElement> e : json.entrySet())
{
String key = e.getKey().toLowerCase();
if(!settings.containsKey(key))
continue;
settings.get(key).fromJson(e.getValue());
}
}
public void save()
{
if(disableSaving)
return;
JsonObject json = createJson();
try
{
JsonUtils.toJson(json, path);
}catch(IOException | JsonException e)
{
System.out.println("Couldn't save " + path.getFileName());
e.printStackTrace();
}
}
private JsonObject createJson()
{
JsonObject json = new JsonObject();
for(Feature feature : featuresWithSettings.values())
{
Collection<Setting> settings = feature.getSettings().values();
JsonObject jsonSettings = new JsonObject();
settings.forEach(s -> jsonSettings.add(s.getName(), s.toJson()));
json.add(feature.getName(), jsonSettings);
}
return json;
}
}

View File

@ -54,6 +54,22 @@ public final class WsonObject
return map;
}
public LinkedHashMap<String, JsonObject> getAllJsonObjects()
{
LinkedHashMap<String, JsonObject> map = new LinkedHashMap<>();
for(Entry<String, JsonElement> entry : json.entrySet())
{
JsonElement value = entry.getValue();
if(!value.isJsonObject())
continue;
map.put(entry.getKey(), value.getAsJsonObject());
}
return map;
}
public JsonObject toJsonObject()
{
return json;