Skip to content
This repository has been archived by the owner on Apr 8, 2023. It is now read-only.

Commit

Permalink
deathindicators: Fix saving bones to config (#1896)
Browse files Browse the repository at this point in the history
* Don't use a permanently-empty map when adding bones

* Save bones when adding them, rather than on shutdown/region change
  • Loading branch information
Lucwousin authored Nov 6, 2019
1 parent 929727c commit 2aa13f3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.collect.ImmutableMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -22,7 +21,6 @@ public class Bones
private static final String BONES_PREFIX = "bones_";

private ImmutableMap<Integer, Map<WorldPoint, List<Bone>>> map;
private boolean changed = false;

void init(Client client, ConfigManager configManager)
{
Expand All @@ -31,14 +29,14 @@ void init(Client client, ConfigManager configManager)
Bone[][] bones = getBones(configManager, regions);
if (log.isDebugEnabled())
{
log.debug("Regions are now {}", Arrays.toString(regions));
log.trace("Regions are now {}", Arrays.toString(regions));

int n = 0;
for (Bone[] ar : bones)
{
n += ar.length;
}
log.debug("Loaded {} Bones", n);
log.debug("Loaded {} Bones", n);
}

initMap(regions, bones);
Expand Down Expand Up @@ -81,7 +79,7 @@ private void initMap(int[] regions, Bone[][] bones)
Bone[] boneA = bones[i];
if (boneA.length == 0)
{
builder.put(region, Collections.EMPTY_MAP);
builder.put(region, new HashMap<>());
continue;
}

Expand All @@ -103,35 +101,29 @@ private void showBones(Scene scene)
this.forEach(bone -> bone.addToScene(scene));
}

void save(ConfigManager configManager)
void save(ConfigManager configManager, int region)
{
if (this.map == null || !changed)
final Map<WorldPoint, List<Bone>> regionBones = this.map.get(region);
if (regionBones == null)
{
this.changed = false;
return;
}

for (Map.Entry<Integer, Map<WorldPoint, List<Bone>>> entry : this.map.entrySet())
{
final String key = BONES_PREFIX + entry.getKey();
final Map<WorldPoint, List<Bone>> map = entry.getValue();
if (map.size() == 0)
{
configManager.unsetConfiguration(CONFIG_GROUP, key);
continue;
}
final String key = BONES_PREFIX + region;

List<Bone> list = new ArrayList<>(map.values().size());
for (List<Bone> lb : map.values())
{
list.addAll(lb);
}
if (regionBones.size() == 0)
{
configManager.unsetConfiguration(CONFIG_GROUP, key);
}

String val = GSON.toJson(list.toArray(new Bone[0]));
configManager.setConfiguration(CONFIG_GROUP, key, val);
List<Bone> list = new ArrayList<>(regionBones.values().size());
for (List<Bone> lb : regionBones.values())
{
list.addAll(lb);
}

this.changed = false;
String val = GSON.toJson(list.toArray(new Bone[0]));
configManager.setConfiguration(CONFIG_GROUP, key, val);
}

public boolean add(Bone bone)
Expand All @@ -141,7 +133,6 @@ public boolean add(Bone bone)
return false;
}

this.changed = true;
final int region = bone.getLoc().getRegionID();
final Map<WorldPoint, List<Bone>> map = this.map.get(region);
final List<Bone> list = map.computeIfAbsent(bone.getLoc(), wp -> new ArrayList<>());
Expand All @@ -151,7 +142,6 @@ public boolean add(Bone bone)

public void remove(Bone bone)
{
this.changed = true;
final int region = bone.getLoc().getRegionID();
final Map<WorldPoint, List<Bone>> map = this.map.get(region);
final List<Bone> list = map.get(bone.getLoc());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,13 @@ protected void shutDown()
worldMapPointManager.removeIf(DeathWorldMapPoint.class::isInstance);

clientThread.invokeLater(this::clearBones);
saveBones();
}

private void initBones()
{
bones.init(client, configManager);
}

private void saveBones()
{
bones.save(configManager);
}

private void clearBones()
{
bones.clear(client.getScene());
Expand Down Expand Up @@ -236,19 +230,24 @@ private void onPostItemDefinition(PostItemDefinition def)

private void onPlayerDeath(PlayerDeath death)
{
Player p = death.getPlayer();
newBoneFor(death.getPlayer());
}

private void newBoneFor(Player player)
{
Bone b = new Bone();

b.setName(Text.sanitize(p.getName()));
b.setName(Text.sanitize(player.getName()));
b.setTime(Instant.now());
b.setLoc(p.getWorldLocation());
b.setLoc(player.getWorldLocation());

while (!bones.add(b))
{
initBones();
}

b.addToScene(client.getScene());
bones.save(configManager, b.getLoc().getRegionID());
}

private void onMenuEntryAdded(MenuEntryAdded event)
Expand Down Expand Up @@ -309,7 +308,13 @@ private void onLocalPlayerDeath(LocalPlayerDeath death)
return;
}

lastDeath = client.getLocalPlayer().getWorldLocation();
Player lp = client.getLocalPlayer();
if (config.permaBones())
{
newBoneFor(lp);
}

lastDeath = lp.getWorldLocation();
lastDeathWorld = client.getWorld();
lastDeathTime = Instant.now();
}
Expand Down Expand Up @@ -400,7 +405,6 @@ private void onConfigChanged(ConfigChanged event)
if (client.getGameState() == GameState.LOGGED_IN)
{
clientThread.invokeLater(this::clearBones);
saveBones();
}
}
return;
Expand Down Expand Up @@ -438,7 +442,6 @@ private void onGameStateChanged(GameStateChanged event)
{
case LOADING:
clearBones();
saveBones();
break;
case LOGGED_IN:
if (config.permaBones())
Expand Down

0 comments on commit 2aa13f3

Please sign in to comment.