Skip to content

Commit

Permalink
Make plot groups extend townyobject (#7141)
Browse files Browse the repository at this point in the history
* Make plot groups extend townyobject

* Save metadata
  • Loading branch information
Warriorrrr authored Jan 3, 2024
1 parent e791331 commit 2368e4f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ private static List<String> getPlotGroupColumns() {
columns.add("`groupName` mediumtext NOT NULL");
columns.add("`groupPrice` float DEFAULT NULL");
columns.add("`town` VARCHAR(32) NOT NULL");
columns.add("`metadata` text DEFAULT NULL");
return columns;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,10 @@ public boolean loadPlotGroup(PlotGroup group) {
line = keys.get("groupPrice");
if (line != null && !line.isEmpty())
group.setPrice(Double.parseDouble(line.trim()));

line = keys.get("metadata");
if (line != null)
MetadataLoader.getInstance().deserializeMetadata(group, line.trim());

} catch (Exception e) {
TownyMessaging.sendErrorMsg(Translation.of("flatfile_err_exception_reading_group_file_at_line", path, line));
Expand Down Expand Up @@ -2081,6 +2085,7 @@ public boolean savePlotGroup(PlotGroup group) {
list.add("groupName=" + group.getName());
list.add("groupPrice=" + group.getPrice());
list.add("town=" + group.getTown().getName());
list.add("metadata=" + serializeMetadata(group));
} catch (Exception e) {
logger.warn("An exception occurred while saving plot group " + Optional.ofNullable(group).map(g -> g.getUUID().toString()).orElse("null") + ": ", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1997,6 +1997,11 @@ private boolean loadPlotGroup(ResultSet rs) {
group.setPrice(Float.parseFloat(line.trim()));
} catch (Exception ignored) {}
}

line = rs.getString("metadata");
if (line != null) {
MetadataLoader.getInstance().deserializeMetadata(group, line);
}
} catch (SQLException e) {
plugin.getLogger().log(Level.WARNING, "Loading Error: Exception while reading plot group: " + uuid
+ " at line: " + line + " in the sql database", e);
Expand Down Expand Up @@ -2267,6 +2272,7 @@ public synchronized boolean savePlotGroup(PlotGroup group) {
pltgrp_hm.put("groupName", group.getName());
pltgrp_hm.put("groupPrice", group.getPrice());
pltgrp_hm.put("town", group.getTown().getName());
pltgrp_hm.put("metadata", serializeMetadata(group));

updateDB("PLOTGROUPS", pltgrp_hm, Collections.singletonList("groupID"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
*
* @author Suneet Tipirneni (Siris)
*/
public abstract class ObjectGroup implements Nameable, Identifiable {
public abstract class ObjectGroup extends TownyObject implements Identifiable {
private UUID uuid;
private String name;

/**
* The constructor for the Group object.
* @param id A unique identifier for the group id.
* @param name An alias for the id used for player in-game interaction via commands.
*/
public ObjectGroup(UUID id, String name) {
this.uuid = id;
this.name = name;
super(name);
this.uuid = id;
}

public UUID getUUID() {
Expand All @@ -28,13 +27,6 @@ public UUID getUUID() {
public void setUUID(UUID id) {
this.uuid = id;
}

@Override
public String getName() {
return name;
}

public void setName(String name) { this.name = name; }

/**
* Determines whether a group is equivalent or not.
Expand All @@ -56,7 +48,7 @@ public boolean equals(Object obj) {
*/
@Override
public String toString() {
return name + "," + uuid;
return getName() + "," + uuid;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public String toString() {
return super.toString() + "," + getTown().toString() + "," + getPrice();
}

@Override
public boolean exists() {
return this.town != null && this.town.exists() && this.town.hasPlotGroupName(getName());
}

/**
* Override the name change method to internally rehash the plot group map.
* @param name The name of the group.
Expand Down

0 comments on commit 2368e4f

Please sign in to comment.