Skip to content

Commit 0432dcf

Browse files
committed
feat: refactor flag management by introducing FlagService and removing DBFunc methods
1 parent 895ca03 commit 0432dcf

File tree

6 files changed

+77
-42
lines changed

6 files changed

+77
-42
lines changed

Core/src/main/java/com/plotsquared/core/database/DBFunc.java

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -54,44 +54,6 @@ public class DBFunc {
5454
public static final UUID EVERYONE = UUID.fromString("1-1-3-3-7");
5555
public static final UUID SERVER = UUID.fromString("00000000-0000-0000-0000-000000000000");
5656

57-
public static void setFlag(Plot plot, PlotFlag<?, ?> flag) {
58-
if (plot == null || flag == null) {
59-
return;
60-
}
61-
PlotRepository plotRepo = PlotSquared.platform().injector().getInstance(PlotRepository.class);
62-
PlotFlagRepository flagRepo = PlotSquared.platform().injector().getInstance(PlotFlagRepository.class);
63-
Optional<PlotEntity> pe = plotRepo.findByWorldAndId(plot.getWorldName(), plot.getId().getX(), plot.getId().getY());
64-
pe.ifPresent(entity -> {
65-
long plotId = entity.getId();
66-
String name = flag.getName();
67-
String value = flag.toString();
68-
var existing = flagRepo.findByPlotAndName(plotId, name);
69-
if (existing.isPresent()) {
70-
var e = existing.get();
71-
e.setFlagValue(value);
72-
flagRepo.save(e);
73-
} else {
74-
PlotFlagEntity e = new PlotFlagEntity();
75-
PlotEntity pref = new PlotEntity();
76-
pref.setId(entity.getId());
77-
e.setPlot(pref);
78-
e.setFlag(name);
79-
e.setFlagValue(value);
80-
flagRepo.save(e);
81-
}
82-
});
83-
}
84-
85-
public static void removeFlag(Plot plot, PlotFlag<?, ?> flag) {
86-
if (plot == null || flag == null) {
87-
return;
88-
}
89-
PlotRepository plotRepo = PlotSquared.platform().injector().getInstance(PlotRepository.class);
90-
PlotFlagRepository flagRepo = PlotSquared.platform().injector().getInstance(PlotFlagRepository.class);
91-
Optional<PlotEntity> pe = plotRepo.findByWorldAndId(plot.getWorldName(), plot.getId().getX(), plot.getId().getY());
92-
pe.ifPresent(entity -> flagRepo.deleteByPlotAndName(entity.getId(), flag.getName()));
93-
}
94-
9557
/**
9658
* @param plot
9759
* @param comment

Core/src/main/java/com/plotsquared/core/plot/Plot.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import com.plotsquared.core.plot.schematic.Schematic;
5454
import com.plotsquared.core.plot.world.SinglePlotArea;
5555
import com.plotsquared.core.queue.QueueCoordinator;
56+
import com.plotsquared.core.services.api.FlagService;
5657
import com.plotsquared.core.services.api.PlotService;
5758
import com.plotsquared.core.util.EventDispatcher;
5859
import com.plotsquared.core.util.MathMan;
@@ -1171,7 +1172,7 @@ public <V> boolean setFlag(final @NonNull PlotFlag<V, ?> flag) {
11711172
for (final Plot plot : this.getConnectedPlots()) {
11721173
plot.getFlagContainer().addFlag(flag);
11731174
plot.reEnter();
1174-
DBFunc.setFlag(plot, flag);
1175+
PlotSquared.platform().injector().getInstance(FlagService.class).setFlag(plot, flag);
11751176
}
11761177
return true;
11771178
}
@@ -1261,7 +1262,7 @@ public boolean removeFlag(final @NonNull PlotFlag<?, ?> flag) {
12611262
continue;
12621263
}
12631264
plot.reEnter();
1264-
DBFunc.removeFlag(plot, flag);
1265+
PlotSquared.platform().injector().getInstance(FlagService.class).removeFlag(plot, flag);
12651266
removed = true;
12661267
}
12671268
return removed;

Core/src/main/java/com/plotsquared/core/plot/PlotModificationManager.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.plotsquared.core.player.PlotPlayer;
3939
import com.plotsquared.core.plot.flag.PlotFlag;
4040
import com.plotsquared.core.queue.QueueCoordinator;
41+
import com.plotsquared.core.services.api.FlagService;
4142
import com.plotsquared.core.services.api.PlotService;
4243
import com.plotsquared.core.util.task.TaskManager;
4344
import com.plotsquared.core.util.task.TaskTime;
@@ -125,9 +126,9 @@ public CompletableFuture<Boolean> copy(final @NonNull Plot destination, @Nullabl
125126
for (final PlotFlag<?, ?> flag : existingFlags) {
126127
final PlotFlag<?, ?> newFlag = other.getFlagContainer().queryLocal(flag.getClass());
127128
if (other.getFlagContainer().queryLocal(flag.getClass()) == null) {
128-
DBFunc.removeFlag(other, flag);
129+
PlotSquared.platform().injector().getInstance(FlagService.class).removeFlag(other, flag);
129130
} else {
130-
DBFunc.setFlag(other, newFlag);
131+
PlotSquared.platform().injector().getInstance(FlagService.class).setFlag(other, newFlag);
131132
}
132133
}
133134
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.plotsquared.core.services.api;
2+
3+
import com.plotsquared.core.plot.Plot;
4+
import com.plotsquared.core.plot.flag.PlotFlag;
5+
6+
public interface FlagService {
7+
8+
void setFlag(Plot plot, PlotFlag<?, ?> flag);
9+
10+
void removeFlag(Plot plot, PlotFlag<?, ?> flag);
11+
}

Core/src/main/java/com/plotsquared/core/services/config/ServiceModule.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@
2020

2121
import com.google.inject.AbstractModule;
2222
import com.plotsquared.core.services.api.ClusterService;
23+
import com.plotsquared.core.services.api.FlagService;
2324
import com.plotsquared.core.services.api.PlayerMetaService;
2425
import com.plotsquared.core.services.api.PlotService;
2526
import com.plotsquared.core.services.impl.ClusterDefaultService;
27+
import com.plotsquared.core.services.impl.FlagDefaultService;
2628
import com.plotsquared.core.services.impl.PlayerMetaDefaultService;
2729
import com.plotsquared.core.services.impl.PlotDefaultService;
2830

@@ -33,6 +35,7 @@ protected void configure() {
3335
bind(PlayerMetaService.class).to(PlayerMetaDefaultService.class);
3436
bind(PlotService.class).to(PlotDefaultService.class);
3537
bind(ClusterService.class).to(ClusterDefaultService.class);
38+
bind(FlagService.class).to(FlagDefaultService.class);
3639
}
3740

3841
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.plotsquared.core.services.impl;
2+
3+
import com.plotsquared.core.PlotSquared;
4+
import com.plotsquared.core.persistence.entity.PlotEntity;
5+
import com.plotsquared.core.persistence.entity.PlotFlagEntity;
6+
import com.plotsquared.core.persistence.repository.api.PlotFlagRepository;
7+
import com.plotsquared.core.persistence.repository.api.PlotRepository;
8+
import com.plotsquared.core.plot.Plot;
9+
import com.plotsquared.core.plot.flag.PlotFlag;
10+
import com.plotsquared.core.services.api.FlagService;
11+
import jakarta.inject.Inject;
12+
13+
import java.util.Optional;
14+
15+
public class FlagDefaultService implements FlagService {
16+
17+
private final PlotRepository plotRepository;
18+
private final PlotFlagRepository flagRepository;
19+
20+
@Inject
21+
public FlagDefaultService(final PlotRepository repository, final PlotFlagRepository flagRepository) {
22+
this.plotRepository = repository;
23+
this.flagRepository = flagRepository;
24+
}
25+
26+
@Override
27+
public void setFlag(final Plot plot, final PlotFlag<?, ?> flag) {
28+
Optional<PlotEntity> pe = this.plotRepository.findByWorldAndId(plot.getWorldName(), plot.getId().getX(), plot.getId().getY());
29+
pe.ifPresent(entity -> {
30+
long plotId = entity.getId();
31+
String name = flag.getName();
32+
String value = flag.toString();
33+
var existing = this.flagRepository.findByPlotAndName(plotId, name);
34+
if (existing.isPresent()) {
35+
var e = existing.get();
36+
e.setFlagValue(value);
37+
this.flagRepository.save(e);
38+
} else {
39+
PlotFlagEntity e = new PlotFlagEntity();
40+
PlotEntity pref = new PlotEntity();
41+
pref.setId(entity.getId());
42+
e.setPlot(pref);
43+
e.setFlag(name);
44+
e.setFlagValue(value);
45+
this.flagRepository.save(e);
46+
}
47+
});
48+
}
49+
50+
@Override
51+
public void removeFlag(final Plot plot, final PlotFlag<?, ?> flag) {
52+
Optional<PlotEntity> pe = this.plotRepository.findByWorldAndId(plot.getWorldName(), plot.getId().getX(),
53+
plot.getId().getY());
54+
pe.ifPresent(entity -> this.flagRepository.deleteByPlotAndName(entity.getId(), flag.getName()));
55+
}
56+
57+
}

0 commit comments

Comments
 (0)