Commit 4acf2cc6b8328f64873992657179297efb01ae17

Authored by Mumfrey
1 parent e9bc0b2b

Example of replacing local mod config with cloud-based config

src/main/java/com/examplemod/ClockCloudConfig.java 0 → 100644
  1 +package com.examplemod;
  2 +
  3 +import com.mumfrey.liteloader.modconfig.CloudConfig;
  4 +import com.mumfrey.webprefs.WebPreferencesManager;
  5 +
  6 +public class ClockCloudConfig extends CloudConfig
  7 +{
  8 + /**
  9 + * Replicated variable, this will be synchronised with the cloud service
  10 + */
  11 + public int size = 64;
  12 +
  13 + /**
  14 + * Replicated variable, this will also be synchronised with the cloud
  15 + * service
  16 + */
  17 + public boolean visible = true;
  18 +
  19 + /**
  20 + * Cached so we can apply values to the clock when received, we could also
  21 + * have this object call back against the mod for example but this is
  22 + * simpler
  23 + */
  24 + private transient Clock clock;
  25 +
  26 + public ClockCloudConfig(Clock clock)
  27 + {
  28 + super(WebPreferencesManager.getDefault(), true);
  29 + this.clock = clock;
  30 + }
  31 +
  32 + /* (non-Javadoc)
  33 + * @see com.mumfrey.liteloader.modconfig.CloudConfig#onUpdated()
  34 + */
  35 + @Override
  36 + protected void onUpdated()
  37 + {
  38 + this.clock.setVisible(this.visible);
  39 + this.clock.setSize(this.size);
  40 + }
  41 +}
... ...
src/main/java/com/examplemod/LiteModExample.java
1 1 package com.examplemod;
2 2  
3   -import com.google.gson.annotations.Expose;
4   -import com.google.gson.annotations.SerializedName;
5 3 import com.mumfrey.liteloader.PreRenderListener;
6 4 import com.mumfrey.liteloader.Tickable;
7 5 import com.mumfrey.liteloader.core.LiteLoader;
8   -import com.mumfrey.liteloader.modconfig.ConfigStrategy;
9   -import com.mumfrey.liteloader.modconfig.ExposableOptions;
10 6 import net.minecraft.client.Minecraft;
11 7 import net.minecraft.client.renderer.RenderGlobal;
12 8 import net.minecraft.client.settings.KeyBinding;
... ... @@ -20,7 +16,6 @@ import java.io.File;
20 16 *
21 17 * @author Adam Mummery-Smith
22 18 */
23   -@ExposableOptions(strategy = ConfigStrategy.Versioned, filename="examplemod.json")
24 19 public class LiteModExample implements Tickable, PreRenderListener
25 20 {
26 21 /**
... ... @@ -35,13 +30,7 @@ public class LiteModExample implements Tickable, PreRenderListener
35 30 */
36 31 private static KeyBinding clockKeyBinding = new KeyBinding("key.clock.toggle", Keyboard.KEY_F12, "key.categories.litemods");
37 32  
38   - @Expose
39   - @SerializedName("clock_size")
40   - private int clockSize = 64;
41   -
42   - @Expose
43   - @SerializedName("clock_visible")
44   - private boolean clockVisible = true;
  33 + private ClockCloudConfig preferences = new ClockCloudConfig(this.clock);
45 34  
46 35 /**
47 36 * Default constructor. All LiteMods must have a default constructor. In general you should do very little
... ... @@ -88,8 +77,8 @@ public class LiteModExample implements Tickable, PreRenderListener
88 77 // a convenience method for this
89 78 LiteLoader.getInput().registerKeyBinding(LiteModExample.clockKeyBinding);
90 79  
91   - this.clock.setSize(this.clockSize);
92   - this.clock.setVisible(this.clockVisible);
  80 + this.clock.setSize(this.preferences.size);
  81 + this.clock.setVisible(this.preferences.visible);
93 82 }
94 83  
95 84 /**
... ... @@ -113,14 +102,14 @@ public class LiteModExample implements Tickable, PreRenderListener
113 102 {
114 103 if (Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT))
115 104 {
116   - this.clockSize = (this.clockSize << 1) & 0x1FF;
117   - this.clock.setSize(this.clockSize);
118   - this.clockSize = this.clock.getSize();
  105 + this.preferences.size = (this.preferences.size << 1) & 0x1FF;
  106 + this.clock.setSize(this.preferences.size);
  107 + this.preferences.size = this.clock.getSize();
119 108 }
120 109 else
121 110 {
122 111 this.clock.setVisible(!this.clock.isVisible());
123   - this.clockVisible = this.clock.isVisible();
  112 + this.preferences.visible = this.clock.isVisible();
124 113 }
125 114  
126 115 // Our @Expose annotations control what properties get saved, this tells liteloader to
... ...