1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-26 04:25:15 +01:00

Better block ownership saving

Better feedback on ownership manual removal
This commit is contained in:
Zrips 2024-10-17 12:14:25 +03:00
parent 0c0c0ff9ba
commit 4a2862281b
6 changed files with 98 additions and 95 deletions

View File

@ -936,7 +936,7 @@ public final class Jobs extends JavaPlugin {
if (dao != null && Jobs.getGeneralConfigManager().ExploreSaveIntoDatabase)
dao.saveExplore();
blockOwnerShipsMaterial.values().forEach(BlockOwnerShip::save);
BlockOwnerShip.save(blockOwnerShipsMaterial);
if (saveTask != null)
saveTask.shutdown();

View File

@ -4,11 +4,13 @@ import java.util.Map;
import java.util.UUID;
import java.util.WeakHashMap;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.commands.JobsCommands;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.container.blockOwnerShip.BlockTypes;
import com.gamingmesh.jobs.i18n.Language;
@ -65,8 +67,11 @@ public class clearownership implements Cmd {
}
}
Language.sendMessage(sender,"command.clearownership.output.cleared", "[furnaces]", amounts.getOrDefault(BlockTypes.FURNACE, 0), "[brewing]", amounts.getOrDefault(
Language.sendMessage(sender, "command.clearownership.output.cleared", "[furnaces]", amounts.getOrDefault(BlockTypes.FURNACE, 0), "[brewing]", amounts.getOrDefault(
BlockTypes.BREWING_STAND, 0), "[smoker]", amounts.getOrDefault(BlockTypes.SMOKER, 0), "[blast]", amounts.getOrDefault(BlockTypes.BLAST_FURNACE, 0));
Bukkit.dispatchCommand(sender, JobsCommands.LABEL + " " + ownedblocks.class.getSimpleName() + (sender.getName().equals(jPlayer.getName()) ? "" : jPlayer.getName()));
return true;
}
}

View File

@ -870,10 +870,10 @@ public class GeneralConfigManager {
FurnacesMaxDefault = c.get("ExploitProtections.Furnaces.MaxDefaultAvailable", 20);
if (Version.isCurrentEqualOrHigher(Version.v1_14_R1)) {
BlastFurnacesReassign = c.get("ExploitProtections.BlastFurnaces.Reassign", false);
BlastFurnacesReassign = c.get("ExploitProtections.BlastFurnaces.Reassign", true);
BlastFurnacesMaxDefault = c.get("ExploitProtections.BlastFurnaces.MaxDefaultAvailable", 15);
SmokerReassign = c.get("ExploitProtections.Smokers.Reassign", false);
SmokerReassign = c.get("ExploitProtections.Smokers.Reassign", true);
SmokersMaxDefault = c.get("ExploitProtections.Smokers.MaxDefaultAvailable", 15);
}
@ -881,6 +881,7 @@ public class GeneralConfigManager {
"When enabled, players interacted brewing stands will be saved into file and will be reassigned after restart to keep giving out money",
"Players will no longer need to click on brewing stand to get paid from it after server restart");
BrewingStandsReassign = c.get("ExploitProtections.BrewingStands.Reassign", true);
c.addComment("ExploitProtections.BrewingStands.MaxDefaultAvailable",
"Defines max available brewing stands each player can have to get paid from",
"Set to 0 if you want to disable this limitation",

View File

@ -495,7 +495,7 @@ public class LanguageManager {
c.get("command.clearownership.help.info", "Clear block ownership");
c.get("command.clearownership.help.args", "[playername]");
Jobs.getGCManager().getCommandArgs().put("clearownership", Arrays.asList("[playername]"));
c.get("command.clearownership.output.cleared", "&2Removed &7[furnaces] &2furnaces, &7[brewing] &2brewing stands, &7[smoker]&2 smokers and &7[blast]&2 blast furnaces.");
c.get("command.clearownership.output.cleared", "&2Removed &7[furnaces] &2furnaces &7[brewing] &2brewing &7[smoker]&2 smokers &7[blast]&2 blast");
c.get("command.clearownership.output.lost", "&cLost ownership of &7[type] &cat [location]");
c.get("command.skipquest.help.info", "Skip defined quest and get new one");

View File

@ -9,7 +9,6 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import com.gamingmesh.jobs.api.JobsBlockOwnershipRegisterEvent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
@ -19,12 +18,15 @@ import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.api.JobsBlockOwnershipRegisterEvent;
import com.gamingmesh.jobs.config.YmlMaker;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.stuff.blockLoc;
import net.Zrips.CMILib.Container.CMILocation;
import net.Zrips.CMILib.FileHandler.ConfigReader;
import net.Zrips.CMILib.Items.CMIMaterial;
import net.Zrips.CMILib.Logs.CMIDebug;
import net.Zrips.CMILib.Messages.CMIMessages;
public class BlockOwnerShip {
@ -290,8 +292,9 @@ public class BlockOwnerShip {
public int remove(UUID uuid, String location) {
HashMap<String, blockLoc> ls = blockOwnerShips.get(uuid);
int ret = 0;
if (ls == null)
return 0;
return ret;
for (Entry<String, blockLoc> one : new HashMap<String, blockLoc>(ls).entrySet()) {
@ -303,11 +306,13 @@ public class BlockOwnerShip {
ls.remove(one.getKey());
Map<String, UUID> oldRecord = ownerMapByLocation.get(one.getValue().getWorldName());
if (oldRecord != null)
if (oldRecord != null) {
oldRecord.remove(one.getValue().toVectorString());
ret++;
}
}
return 1;
return ret;
}
public List<MetadataValue> getBlockMetadatas(Block block) {
@ -320,34 +325,12 @@ public class BlockOwnerShip {
}
public void load() {
YmlMaker f = new YmlMaker(Jobs.getFolder(), "furnaceBrewingStands.yml");
YmlMaker f2 = new YmlMaker(Jobs.getFolder(), "blockOwnerShips.yml");
if (!f.exists() && !f2.exists())
YmlMaker f = new YmlMaker(Jobs.getFolder(), "blockOwnerShips.yml");
if (!f.exists())
return;
if (f.exists()) {
f.getConfigFile().renameTo(f2.getConfigFile());
}
f = f2;
String path = "";
switch (type) {
case BLAST_FURNACE:
path = "BlastFurnace";
break;
case BREWING_STAND:
path = "Brewing";
break;
case FURNACE:
path = "Furnace";
break;
case SMOKER:
path = "Smoker";
break;
default:
break;
}
String path = type.getPath();
if (isReassignDisabled())
return;
@ -375,17 +358,24 @@ public class BlockOwnerShip {
}
HashMap<String, blockLoc> blist = new HashMap<String, blockLoc>();
boolean informed = false;
for (String oneL : ls) {
blockLoc bl = new blockLoc(oneL);
CMILocation cmil = CMILocation.fromString(oneL, ":");
try {
blockLoc bl = new blockLoc(oneL);
blist.put(CMILocation.toString(cmil, ":", true, true), bl);
blist.put(oneL, bl);
Map<String, UUID> oldRecord = ownerMapByLocation.getOrDefault(bl.getWorldName(), new HashMap<String, UUID>());
oldRecord.put(bl.toVectorString(), uuid);
ownerMapByLocation.put(bl.getWorldName(), oldRecord);
Map<String, UUID> oldRecord = ownerMapByLocation.getOrDefault(bl.getWorldName(), new HashMap<String, UUID>());
oldRecord.put(bl.toVectorString(), uuid);
ownerMapByLocation.put(bl.getWorldName(), oldRecord);
total++;
total++;
} catch (Throwable e) {
if (!informed)
e.printStackTrace();
informed = true;
}
}
if (!blist.isEmpty()) {
@ -398,46 +388,45 @@ public class BlockOwnerShip {
}
}
public void save() {
YmlMaker f = new YmlMaker(Jobs.getFolder(), "furnaceBrewingStands.yml");
if (f.exists()) {
f.getConfigFile().renameTo(new File(Jobs.getFolder(), "blockOwnerShips.yml"));
public static void save(HashMap<CMIMaterial, BlockOwnerShip> blockOwnerShipsMaterial) {
File f = new File(Jobs.getInstance().getDataFolder(), "blockOwnerShips.yml");
ConfigReader cfg = null;
try {
cfg = new ConfigReader(f);
} catch (Exception e) {
e.printStackTrace();
}
f = new YmlMaker(Jobs.getFolder(), "blockOwnerShips.yml");
if (blockOwnerShips.isEmpty() && f.getConfigFile().length() == 0L) {
f.getConfigFile().delete();
if (cfg == null)
return;
}
f.createNewFile();
f.saveDefaultConfig();
cfg.getC().options().copyDefaults(true);
if (isReassignDisabled()) {
return;
}
String path = (type == BlockTypes.FURNACE ? "Furnace"
: type == BlockTypes.BLAST_FURNACE ? "BlastFurnace"
: type == BlockTypes.BREWING_STAND ? "Brewing" : type == BlockTypes.SMOKER ? "Smoker" : "");
f.getConfig().set(path, null);
for (Entry<UUID, HashMap<String, blockLoc>> one : blockOwnerShips.entrySet()) {
StringBuilder full = new StringBuilder();
for (String oneL : one.getValue().keySet()) {
if (!full.toString().isEmpty())
full.append(";");
full.append(oneL);
for (BlockOwnerShip ownership : blockOwnerShipsMaterial.values()) {
if (ownership.isReassignDisabled()) {
return;
}
String path = ownership.getType().getPath();
if (!full.toString().isEmpty())
f.getConfig().set(path + "." + one.getKey().toString(), full.toString());
cfg.getC().set(path, null);
for (Entry<UUID, HashMap<String, blockLoc>> one : ownership.blockOwnerShips.entrySet()) {
StringBuilder full = new StringBuilder();
for (String oneL : one.getValue().keySet()) {
if (!full.toString().isEmpty())
full.append(";");
full.append(oneL);
}
if (!full.toString().isEmpty())
cfg.get(path + "." + one.getKey().toString(), full.toString());
}
}
f.saveConfig();
cfg.save();
}
public boolean isReassignDisabled() {

View File

@ -6,39 +6,47 @@ import net.Zrips.CMILib.Items.CMIMaterial;
public enum BlockTypes {
BREWING_STAND("BREWING_STAND", "LEGACY_BREWING_STAND"),
FURNACE("FURNACE", "LEGACY_BURNING_FURNACE"),
SMOKER,
BLAST_FURNACE;
BREWING_STAND("Brewing", "BREWING_STAND", "LEGACY_BREWING_STAND"),
FURNACE("Furnace", "FURNACE", "LEGACY_BURNING_FURNACE"),
SMOKER("Smoker"),
BLAST_FURNACE("BlastFurnace");
private String[] names;
private String path = "_";
private static HashMap<CMIMaterial, BlockTypes> cache = new HashMap<>();
static {
for (CMIMaterial one : CMIMaterial.values()) {
for (BlockTypes b : values()) {
for (String name : b.names) {
if (name.equals(one.toString())) {
cache.put(one, b);
}
}
}
}
for (CMIMaterial one : CMIMaterial.values()) {
for (BlockTypes b : values()) {
for (String name : b.names) {
if (name.equals(one.toString())) {
cache.put(one, b);
}
}
}
}
}
BlockTypes() {
names = new String[] { toString() };
BlockTypes(String path) {
this.path = path;
names = new String[] { toString() };
}
BlockTypes(String... names) {
this.names = names;
BlockTypes(String path, String... names) {
this.path = path;
this.names = names;
}
public String[] getNames() {
return names;
return names;
}
public static BlockTypes getFromCMIMaterial(CMIMaterial type) {
return cache.get(type);
return cache.get(type);
}
public String getPath() {
return path;
}
}