Skip to content

Commit

Permalink
Dynmap support, cross world screw up.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Jul 25, 2017
1 parent 596a704 commit 2b62dae
Show file tree
Hide file tree
Showing 16 changed files with 229 additions and 80 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Spigot plugin for 1.12+ allowing the claiming of chunks.

*(Should work with Spigot 1.11+ with titles enabled, if they're disabled, should work back a few versions)*

Page on SpigotMC can be found [HERE](https://www.spigotmc.org/resources/claimchunk.44458/).

Current version: **0.0.1** for Minecraft **1.12**. Requires [Vault](https://www.spigotmc.org/resources/vault.41918/).
Current version: **0.0.2** for Minecraft **1.12**. Requires [Vault](https://www.spigotmc.org/resources/vault.41918/).
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<id>natrolite-repo</id>
<url>https://repo.natrolite.org/repository/maven-public/</url>
</repository>
<repository>
<id>dynmap-rep</id>
<url>http://repo.mikeprimm.com/</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand All @@ -19,5 +23,15 @@
<version>1.6.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>us.dynmap</groupId>
<artifactId>dynmap</artifactId>
<version>2.6-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.12-R0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
74 changes: 44 additions & 30 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,33 @@
import java.io.IOException;
import java.util.UUID;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import com.cjburkey.claimchunk.chunk.AccessHandler;
import com.cjburkey.claimchunk.chunk.ChunkHandler;
import com.cjburkey.claimchunk.cmd.CmdAccessChunks;
import com.cjburkey.claimchunk.cmd.CmdClaimChunk;
import com.cjburkey.claimchunk.cmd.CmdUnclaimChunk;
import com.cjburkey.claimchunk.dynmap.ClaimChunkDynmap;
import com.cjburkey.claimchunk.event.CancellableChunkEvents;
import com.cjburkey.claimchunk.event.PlayerJoinHandler;
import com.cjburkey.claimchunk.event.PlayerMovementHandler;
import net.milkbowl.vault.permission.Permission;

public final class ClaimChunk extends JavaPlugin {

private static ClaimChunk instance;

private boolean useEcon = false;
private boolean useDynmap = false;

private File dataFile;
private File plyFile;
private File accessFile;

private Econ economy;
private Permission perms;
private ClaimChunkDynmap map;
private Cacher cacher;
private ChunkHandler chunkHandler;
private AccessHandler accessHandler;
Expand All @@ -38,26 +41,39 @@ public void onEnable() {
plyFile = new File(getDataFolder(), "/data/playerCache.dat");
accessFile = new File(getDataFolder(), "/data/grantedAccess.dat");
economy = new Econ();
map = new ClaimChunkDynmap();
cacher = new Cacher();
chunkHandler = new ChunkHandler();
accessHandler = new AccessHandler();

if (!economy.setupEconomy(this)) {
Utils.err("Economy could not be setup. Make sure that you have an economy plugin (like Essentials) installed. ClaimChunk has been disabled.");
disable();
return;
}
Utils.log("Economy set up.");

setupConfig();
Utils.log("Config set up.");

if (!setupPermissions()) {
Utils.err("Permissions could not be initialized. ClaimChunk has been disabled.");
disable();
return;
useEcon = ((getServer().getPluginManager().getPlugin("Vault") != null) && getConfig().getBoolean("useEconomy"));
useDynmap = ((getServer().getPluginManager().getPlugin("dynmap") != null) && getConfig().getBoolean("useDynmap"));
if (useEcon) {
if (!economy.setupEconomy(this)) {
Utils.err("Economy could not be setup. Make sure that you have an economy plugin (like Essentials) installed. ClaimChunk has been disabled.");
disable();
return;
}
Utils.log("Economy set up.");
getServer().getScheduler().scheduleSyncDelayedTask(this, () -> Utils.log("Money Format: " + economy.format(99132.76)), 0l); // Once everything is loaded.
} else {
Utils.log("Economy not enabled. Either it was disabled with config or Vault was not found.");
}

if (useDynmap) {
if (!map.registerAndSuch()) {
Utils.log("There was an error while enabling Dynmap support.");
disable();
return;
} else {
Utils.log("Dynmap support enabled.");
}
} else {
Utils.log("Dynmap support not enabled. Either it was disabled with config or Dynmap was not found.");
}
Utils.log("Permissions set up.");

setupCommands();
Utils.log("Commands set up.");
Expand All @@ -77,14 +93,14 @@ public void onEnable() {
Utils.log("Initialization complete.");
}

public boolean canEdit(int x, int z, UUID player) {
if (!chunkHandler.isClaimed(x, z)) {
public boolean canEdit(World world, int x, int z, UUID player) {
if (!chunkHandler.isClaimed(world, x, z)) {
return true;
}
if (chunkHandler.isOwner(x, z, player)) {
if (chunkHandler.isOwner(world, x, z, player)) {
return true;
}
if (accessHandler.hasAccess(chunkHandler.getOwner(x, z), player)) {
if (accessHandler.hasAccess(chunkHandler.getOwner(world, x, z), player)) {
return true;
}
return false;
Expand All @@ -93,7 +109,7 @@ public boolean canEdit(int x, int z, UUID player) {
public void cancelEventIfNotOwned(Player ply, Chunk chunk, Cancellable e) {
if (getConfig().getBoolean("blockInteractionInOtherPlayersChunks")) {
if (!e.isCancelled()) {
if (!canEdit(chunk.getX(), chunk.getZ(), ply.getUniqueId())) {
if (!canEdit(chunk.getWorld(), chunk.getX(), chunk.getZ(), ply.getUniqueId())) {
e.setCancelled(true);
Utils.toPlayer(ply, Utils.getConfigColor("errorColor"), Utils.getLang("CannotEditThisChunk"));
}
Expand All @@ -118,12 +134,6 @@ private void setupCommands() {
getCommand("accesschunks").setExecutor(new CmdAccessChunks());
}

private boolean setupPermissions() {
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
perms = rsp.getProvider();
return perms != null;
}

public void onDisable() {
try {
cacher.write(plyFile);
Expand All @@ -143,10 +153,6 @@ public Econ getEconomy() {
return economy;
}

public Permission getPermission() {
return perms;
}

public Cacher getPlayers() {
return cacher;
}
Expand All @@ -171,6 +177,14 @@ public File getAccessFile() {
return accessFile;
}

public boolean useEconomy() {
return useEcon;
}

public boolean useDynmap() {
return useDynmap;
}

public static ClaimChunk getInstance() {
return instance;
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/com/cjburkey/claimchunk/Econ.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.cjburkey.claimchunk;

import java.text.NumberFormat;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;

public final class Econ {

Expand All @@ -19,6 +24,64 @@ public boolean setupEconomy(ClaimChunk instance) {
return econ != null;
}

public double getMoney(UUID ply) {
OfflinePlayer op = getOfflinePlayer(ply);
if (op != null && op.hasPlayedBefore()) {
return econ.getBalance(op);
}
return -1.0d;
}

/**
* Take money from the player.
* @param ply Player purchasing.
* @param cost The cost of the purchase.
* @return Whether or not the transaction was successful.
*/
public boolean buy(UUID ply, double cost) {
if (getMoney(ply) >= cost) {
EconomyResponse r = takeMoney(ply, cost);
if (r.type.equals(EconomyResponse.ResponseType.SUCCESS)) {
return true;
}
}
return false;
}

public void setMoney(UUID ply, double amt) {
double current = getMoney(ply);
double toAdd = amt - current;
if (toAdd < 0) {
takeMoney(ply, Math.abs(toAdd));
return;
}
addMoney(ply, toAdd);
}

public EconomyResponse addMoney(UUID ply, double amt) {
OfflinePlayer op = getOfflinePlayer(ply);
if (op != null) {
return econ.depositPlayer(op, Math.abs(amt));
}
return null;
}

public EconomyResponse takeMoney(UUID ply, double amt) {
OfflinePlayer op = getOfflinePlayer(ply);
if (op != null) {
return econ.withdrawPlayer(op, Math.abs(amt));
}
return null;
}

public String format(double amt) {
return NumberFormat.getCurrencyInstance().format(amt);
}

private OfflinePlayer getOfflinePlayer(UUID id) {
return Bukkit.getOfflinePlayer(id);
}

public Economy getEconomy() {
return econ;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/cjburkey/claimchunk/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static void toPlayer(Player ply, ChatColor color, String msg) {
}

public static boolean hasPerm(CommandSender sender, String perm) {
return ClaimChunk.getInstance().getPermission().has(sender, perm);
return sender.hasPermission(perm);
}

private static String prepMsg(Object msg) {
Expand Down
34 changes: 18 additions & 16 deletions src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import org.bukkit.World;
import org.bukkit.entity.Player;
import com.cjburkey.claimchunk.ClaimChunk;
import com.cjburkey.claimchunk.Utils;
Expand All @@ -24,49 +25,51 @@ public final class ChunkHandler {

/**
* Claims a specific chunk for a player if that chunk is not already owned.
* @param world The current world.
* @param x The chunk x-coord.
* @param z The chunk z-coord.
* @param player The player for whom to claim the chunk.
* @return Whether or not the chunk was claimed.
*/
public boolean claimChunk(int x, int z, Player player) {
if (isClaimed(x, z)) {
public boolean claimChunk(World world, int x, int z, Player player) {
if (isClaimed(world, x, z)) {
return false;
}
claimed.put(new ChunkPos(x, z), player.getUniqueId());
claimed.put(new ChunkPos(world.getName(), x, z), player.getUniqueId());
reload();
return true;
}

/**
* Unclaims a specific chunk if that chunk is currently owned.
* @param world The current world.
* @param x The chunk x-coord.
* @param z The chunk z-coord.
* @return Whether or not the chunk was unclaimed.
*/
public boolean unclaimChunk(int x, int z) {
if (!isClaimed(x, z)) {
public boolean unclaimChunk(World world, int x, int z) {
if (!isClaimed(world, x, z)) {
return false;
}
claimed.remove(new ChunkPos(x, z));
claimed.remove(new ChunkPos(world.getName(), x, z));
reload();
return true;
}

public boolean isClaimed(int x, int z) {
return claimed.containsKey(new ChunkPos(x, z));
public boolean isClaimed(World world, int x, int z) {
return claimed.containsKey(new ChunkPos(world.getName(), x, z));
}

public boolean isOwner(int x, int z, UUID uuid) {
return claimed.get(new ChunkPos(x, z)).equals(uuid);
public boolean isOwner(World world, int x, int z, UUID uuid) {
return claimed.get(new ChunkPos(world.getName(), x, z)).equals(uuid);
}

public boolean isOwner(int x, int z, Player ply) {
return isOwner(x, z, ply.getUniqueId());
public boolean isOwner(World world, int x, int z, Player ply) {
return isOwner(world, x, z, ply.getUniqueId());
}

public UUID getOwner(int x, int z) {
return claimed.get(new ChunkPos(x, z));
public UUID getOwner(World world, int x, int z) {
return claimed.get(new ChunkPos(world.getName(), x, z));
}

public void reload() {
Expand All @@ -87,15 +90,14 @@ public void writeToDisk(File file) throws IOException {
}
FileWriter writer = null;
StringBuilder out = new StringBuilder();
out.append(";;-cab91ae58cc9464e1fbeda05024bed39"); // MD5
out.append(";;-This is a comment, it is not read. It doesn't matter, though, this file is reset every time.");
out.append("\n");
for (Entry<ChunkPos, UUID> entry : claimed.entrySet()) {
out.append(entry.getKey().toString());
out.append(';');
out.append(entry.getValue().toString());
out.append('\n');
}
out.append(";;-f957097d7a22775f6bea41b46cae4d14274dac9f"); // SHA-1
try {
writer = new FileWriter(file, false);
writer.write(out.toString());
Expand Down
Loading

0 comments on commit 2b62dae

Please sign in to comment.