Skip to content

Commit

Permalink
fix(delete): Fixed the problem that residence not removed after delet…
Browse files Browse the repository at this point in the history
…ed. #32
  • Loading branch information
CarmJos committed Mar 10, 2024
1 parent 3abb0ea commit 2cd0f6c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>residencelist-parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public interface ResidenceManager<D extends ResidenceData> {

void renameResidence(String oldName, String newName);

void removeResidence(@NotNull String name);

boolean updateResidence(@NotNull ResidenceData data, @NotNull Consumer<ResidenceData> dataConsumer);

default boolean updateResidence(@NotNull String name, @NotNull Consumer<ResidenceData> dataConsumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
Expand All @@ -19,8 +20,8 @@ public abstract class CombinedStorage<U extends UserListData, D extends Residenc
protected final @NotNull UserManager<U> userManager;
protected final @NotNull ResidenceManager<D> residenceManager;

public CombinedStorage(@NotNull UserManager<U> userManager,
@NotNull ResidenceManager<D> residenceManager) {
protected CombinedStorage(@NotNull UserManager<U> userManager,
@NotNull ResidenceManager<D> residenceManager) {
this.userManager = userManager;
this.residenceManager = residenceManager;
}
Expand Down Expand Up @@ -52,6 +53,21 @@ public void renameResidence(String oldName, String newName) {
this.residenceManager.renameResidence(oldName, newName);
}

@Override
public void removeResidence(@NotNull String name) {
this.residenceManager.removeResidence(name);
}

@Override
public @Nullable U getNullable(@NotNull UUID key) {
return null;
}

@Override
public @NotNull Optional<@Nullable U> getOptional(@NotNull UUID key) {
return Optional.empty();
}

@Override
public boolean updateResidence(@NotNull ResidenceData data, @NotNull Consumer<ResidenceData> dataConsumer) {
return this.residenceManager.updateResidence(data, dataConsumer);
Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>com.artformgames</groupId>
<artifactId>residencelist-parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</parent>
<properties>
<maven.compiler.source>${project.jdk.version}</maven.compiler.source>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.artformgames.plugin.residencelist.Main;
import com.artformgames.plugin.residencelist.api.ResidenceManager;
import com.bekvon.bukkit.residence.event.ResidenceDeleteEvent;
import com.bekvon.bukkit.residence.event.ResidenceRenameEvent;
import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import org.bukkit.event.EventHandler;
Expand All @@ -18,4 +19,12 @@ public void onRename(ResidenceRenameEvent event) {
manager.renameResidence(event.getOldResidenceName(), event.getNewResidenceName());
}

@EventHandler
public void onDelete(ResidenceDeleteEvent event) {
ClaimedResidence residence = event.getResidence();
if (residence.isSubzone()) return; // Only main residence can be stored.
ResidenceManager<?> manager = Main.getInstance().getResidenceManager();
manager.removeResidence(residence.getResidenceName());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ public void renameResidence(String oldName, String newName) {
YAMLResidenceData data = this.residences.remove(oldName);
if (data == null) return; // No data for this residence yet.

this.residences.remove(oldName);
this.residences.put(newName, data);

File n = new File(this.residenceDataFolder, newName + ".yaml");
Expand All @@ -189,6 +188,20 @@ public void renameResidence(String oldName, String newName) {
}
}

@Override
public void removeResidence(@NotNull String name) {
YAMLResidenceData data = this.residences.remove(name);
if (data == null) return; // No data for this residence yet.

try {
data.delete();
Main.debugging("Successfully removed residence data for '" + name + "' !");
} catch (Exception e) {
Main.severe("Error occurred when removing residence data for '" + name + "' !");
e.printStackTrace();
}
}

@Override
public boolean updateResidence(@NotNull ResidenceData data,
@NotNull Consumer<ResidenceData> dataConsumer) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ public void renameTo(@NotNull File newFile) throws Exception {
save();
}

public boolean delete() throws Exception {
return this.file.exists() && this.file.delete();
}

public void save() throws Exception {
if (!this.file.exists()) {
this.file.createNewFile();
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</properties>
<groupId>com.artformgames</groupId>
<artifactId>residencelist-parent</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
<packaging>pom</packaging>
<modules>
<module>api</module>
Expand Down

0 comments on commit 2cd0f6c

Please sign in to comment.