Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Jan 4, 2016
1 parent d63b033 commit 339c8fa
Show file tree
Hide file tree
Showing 21 changed files with 290 additions and 882 deletions.
17 changes: 2 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>com.mengcraft</groupId>
<artifactId>playersql</artifactId>
<version>1.18</version>
<version>2.0-SNAPSHOT</version>
<name>PlayerSQL</name>

<properties>
Expand All @@ -13,7 +13,7 @@

<repositories>
<repository>
<id>ci.mengcraft.com</id>
<id>ci-mengcraft</id>
<url>http://ci.mengcraft.com:8080/plugin/repository/everything/</url>
</repository>
<repository>
Expand Down Expand Up @@ -44,19 +44,6 @@
</resource>
</resources>
<plugins>
<plugin>
<groupId>net.orfjackal.retrolambda</groupId>
<artifactId>retrolambda-maven-plugin</artifactId>
<version>2.1.0</version>
<executions>
<execution>
<goals>
<goal>process-main</goal>
<goal>process-test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
Expand Down
40 changes: 14 additions & 26 deletions src/main/java/com/mengcraft/playersql/Config.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,34 @@
package com.mengcraft.playersql;

import java.io.File;

import org.bukkit.ChatColor;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;

public class Config {

public static final Configuration CONF;

public static final boolean SYN_CHEST;
public static final boolean SYN_EFFECT;
public static final boolean SYN_EXP;
public static final boolean SYN_FOOD;
public static final boolean SYN_HEALTH;
public static final boolean SYN_INVENTORY;

public static final int SYN_DELAY;

public static final boolean MSG_ENABLE;
public static final String MSG_LOADING;

public static final String MSG_SYNCHRONIZED;
public static final File FILE;
public static final Configuration CONF;

public static final boolean DEBUG;
public static final int SYN_DELAY;

static {
FILE = new File("plugins/PlayerSQL/config.yml");
YamlConfiguration yml = YamlConfiguration.loadConfiguration(FILE);
SYN_HEALTH = yml.getBoolean("sync.health", true);
SYN_CHEST = yml.getBoolean("sync.chest", true);
SYN_INVENTORY = yml.getBoolean("sync.inventory", true);
SYN_EFFECT = yml.getBoolean("sync.potion", true);
SYN_EXP = yml.getBoolean("sync.exp", true);
SYN_FOOD = yml.getBoolean("sync.food", true);
SYN_DELAY = yml.getInt("sync.delay", 30);
MSG_ENABLE = yml.getBoolean("plugin.messages", false);
MSG_LOADING = ChatColor.translateAlternateColorCodes('&', yml.getString("messages.dataLoading", "&aPlease wait while your data is being loaded :)"));
MSG_SYNCHRONIZED = ChatColor.translateAlternateColorCodes('&', yml.getString("messages.dataSynchronized", "&aEnjoy! All your data has been synchronized."));
DEBUG = yml.getBoolean("plugin.debug", false);
CONF = yml;
CONF = YamlConfiguration.loadConfiguration(new File("plugins/PlayerSQL/config.yml"));
SYN_HEALTH = CONF.getBoolean("sync.health", true);
SYN_CHEST = CONF.getBoolean("sync.chest", true);
SYN_INVENTORY = CONF.getBoolean("sync.inventory", true);
SYN_EFFECT = CONF.getBoolean("sync.potion", true);
SYN_EXP = CONF.getBoolean("sync.exp", true);
SYN_FOOD = CONF.getBoolean("sync.food", true);
SYN_DELAY = CONF.getInt("plugin.delay", 30);
DEBUG = CONF.getBoolean("plugin.debug", false);
}

}
56 changes: 0 additions & 56 deletions src/main/java/com/mengcraft/playersql/DataCompound.java

This file was deleted.

104 changes: 94 additions & 10 deletions src/main/java/com/mengcraft/playersql/EventExecutor.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package com.mengcraft.playersql;

import com.mengcraft.playersql.task.DailySaveTask;
import com.mengcraft.playersql.task.FetchUserTask;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.player.*;
import org.bukkit.scheduler.BukkitTask;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created on 16-1-2.
*/
public class EventExecutor implements Listener {

private final Map<UUID, BukkitTask> taskMap = new ConcurrentHashMap<>();

private UserManager userManager;
private PluginMain main;

@EventHandler
public void handle(PlayerLoginEvent event) {
LOCKED.add(event.getPlayer().getUniqueId());
this.userManager.lockUser(event.getPlayer().getUniqueId());
}

@EventHandler
Expand All @@ -32,6 +39,71 @@ public void handle(PlayerJoinEvent event) {
}
}

@EventHandler
public void handle(PlayerQuitEvent event) {
if (!this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
User user = this.userManager.getUser(event.getPlayer().getUniqueId());
this.taskMap.remove(event.getPlayer().getUniqueId()).cancel();
this.userManager.syncUser(user);
this.main.runTaskAsynchronously(() -> {
this.userManager.saveUser(user, false);
});
}
}

@EventHandler(ignoreCancelled = true)
public void handle(PlayerMoveEvent event) {
if (this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
Location from = event.getFrom();
Location to = event.getTo();
from.setYaw(to.getYaw());
from.setPitch(to.getPitch());
event.setTo(from);
}
}

@EventHandler(ignoreCancelled = true)
public void handle(EntityDamageEvent event) {
if (event.getEntityType() == EntityType.PLAYER && this.userManager.isUserLocked(event.getEntity().getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true)
public void handle(PlayerPickupItemEvent event) {
if (this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true)
public void handle(PlayerDropItemEvent event) {
if (this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true)
public void handle(PlayerInteractEntityEvent event) {
if (this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true)
public void handle(PlayerInteractEvent event) {
if (this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
}
}

@EventHandler(ignoreCancelled = true)
public void handle(PlayerCommandPreprocessEvent event) {
if (this.userManager.isUserLocked(event.getPlayer().getUniqueId())) {
event.setCancelled(true);
}
}

public void cancelTask(int i) {
this.main.getServer().getScheduler().cancelTask(i);
}
Expand All @@ -52,12 +124,24 @@ public PluginMain getMain() {
return this.main;
}

public void unlock(UUID uuid) {
synchronized (LOCKED) {
LOCKED.remove(uuid);
public void createTask(UUID uuid) {
if (Config.DEBUG) {
this.main.logMessage("Scheduling daily save task for user " + uuid + '.');
}
DailySaveTask saveTask = new DailySaveTask();
BukkitTask task = this.main.runTaskTimer(saveTask, 6000);
synchronized (saveTask) {
saveTask.setUuid(uuid);
saveTask.setExecutor(this);
saveTask.setTaskId(task.getTaskId());
}
BukkitTask old = this.taskMap.put(uuid, task);
if (old != null) {
if (Config.DEBUG) {
this.main.logException(new PluginException("Already schedule task for user " + uuid + '!'));
}
old.cancel();
}
}

public final static List<UUID> LOCKED = new ArrayList<>();

}
Loading

0 comments on commit 339c8fa

Please sign in to comment.