Skip to content

Commit

Permalink
v3.2.1 - LeaderHeads support, revamped paged gui system, autosave, bu…
Browse files Browse the repository at this point in the history
…g fixes
  • Loading branch information
Realizedd committed Sep 15, 2018
1 parent f3f77ba commit 3880863
Show file tree
Hide file tree
Showing 32 changed files with 404 additions and 91 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ buildscript {

allprojects {
group 'me.realized'
version '3.2.1-SNAPSHOT'
version '3.2.1'
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package me.realized.duels.api.event.request;

import javax.annotation.Nonnull;
import me.realized.duels.api.request.Request;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;

/**
* Called when a {@link Player} accepts a {@link Request} from a {@link Player}.
*
* @since 3.2.1
*/
public class RequestAcceptEvent extends RequestEvent implements Cancellable {

private static final HandlerList handlers = new HandlerList();

private boolean cancelled;

/**
* @param source {@link Player} who is accepting this {@link Request}.
* @param target {@link Player} who sent this {@link Request}.
* @param request {@link Request} that is being handled.
*/
public RequestAcceptEvent(@Nonnull final Player source, @Nonnull final Player target, @Nonnull final Request request) {
super(source, target, request);
}

/**
* Whether or not this event has been cancelled.
*
* @return True if this event has been cancelled. False otherwise.
*/
@Override
public boolean isCancelled() {
return cancelled;
}

/**
* Whether or not to cancel this event.
* When cancelled, the request will not be removed and remain as unhandled.
*
* @param cancelled True to cancel this event.
*/
@Override
public void setCancelled(final boolean cancelled) {
this.cancelled = cancelled;
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package me.realized.duels.api.event.request;

import javax.annotation.Nonnull;
import me.realized.duels.api.request.Request;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;

/**
* Called when a {@link Player} denies a {@link Request} from a {@link Player}.
*
* @since 3.2.1
*/
public class RequestDenyEvent extends RequestEvent {

private static final HandlerList handlers = new HandlerList();

/**
* @param source {@link Player} who is denying this {@link Request}.
* @param target {@link Player} who sent this {@link Request}.
* @param request {@link Request} that is being handled.
*/
public RequestDenyEvent(@Nonnull final Player source, @Nonnull final Player target, @Nonnull final Request request) {
super(source, target, request);
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package me.realized.duels.api.event.request;

import java.util.Objects;
import javax.annotation.Nonnull;
import me.realized.duels.api.event.SourcedEvent;
import me.realized.duels.api.request.Request;
import org.bukkit.entity.Player;

/**
* Represents an event caused by a {@link Request}.
*
* @since 3.2.1
*/
public abstract class RequestEvent extends SourcedEvent {

private final Player source, target;
private final Request request;

RequestEvent(@Nonnull final Player source, @Nonnull final Player target, @Nonnull final Request request) {
super(source);
Objects.requireNonNull(source, "source");
Objects.requireNonNull(target, "target");
Objects.requireNonNull(request, "request");
this.source = source;
this.target = target;
this.request = request;
}

/**
* {@link Player} who is the source of this event.
*
* @return Never-null {@link Player} who is the source of this event.
*/
@Nonnull
@Override
public Player getSource() {
return source;
}

/**
* {@link Player} who is the target of this event.
*
* @return Never-null {@link Player} who is the target of this event.
*/
@Nonnull
public Player getTarget() {
return target;
}

/**
* {@link Request} instance associated with this event.
*
* @return Never-null {@link Request} instance associated with this event.
*/
@Nonnull
public Request getRequest() {
return request;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package me.realized.duels.api.event.request;

import java.util.Objects;
import javax.annotation.Nonnull;
import me.realized.duels.api.event.SourcedEvent;
import me.realized.duels.api.request.Request;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand All @@ -11,73 +9,42 @@
/**
* Called when a {@link Player} sends a {@link Request} to a {@link Player}.
*/
public class RequestSendEvent extends SourcedEvent implements Cancellable {
public class RequestSendEvent extends RequestEvent implements Cancellable {

private static final HandlerList handlers = new HandlerList();

private final Player source;
private final Player target;
private final Request request;

private boolean cancelled;

public RequestSendEvent(@Nonnull final Player source, @Nonnull final Player target, @Nonnull final Request request) {
super(source);
Objects.requireNonNull(source, "source");
Objects.requireNonNull(target, "target");
Objects.requireNonNull(request, "request");
this.source = source;
this.target = target;
this.request = request;
super(source, target, request);
}

/**
* {@link Player} who is sending the {@link Request}.
* Whether or not this event has been cancelled.
*
* @return Never-null {@link Player} who is sending the {@link Request}.
* @return True if this event has been cancelled. False otherwise.
*/
@Nonnull
@Override
public Player getSource() {
return source;
}

/**
* {@link Player} who is receiving the {@link Request}.
*
* @return Never-null {@link Player} who is receiving the {@link Request}.
*/
@Nonnull
public Player getTarget() {
return target;
public boolean isCancelled() {
return cancelled;
}

/**
* The {@link Request} that will be sent.
* Whether or not to cancel this event.
*
* @return Never-null {@link Request} that will be sent.
* @param cancelled True to cancel this event.
*/
@Nonnull
public Request getRequest() {
return request;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(final boolean cancelled) {
this.cancelled = cancelled;
}

public static HandlerList getHandlerList() {
@Override
public HandlerList getHandlers() {
return handlers;
}

@Override
public HandlerList getHandlers() {
public static HandlerList getHandlerList() {
return handlers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public UserCreateEvent(@Nonnull final User user) {
this.user = user;
}

/**
* The {@link User} that was created.
*
* @return Never-null {@link User} that was created.
*/
@Nonnull
public User getUser() {
return user;
Expand Down
1 change: 1 addition & 0 deletions duels-plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ dependencies {
compile name: 'BountyHunters'
compile name: 'SimpleClans'
compile name: 'CombatLogX'
compile name: 'LeaderHeadsAPI'

compile project(':duels-api')
compile 'com.google.code.gson:gson:2.8.2'
Expand Down
5 changes: 5 additions & 0 deletions duels-plugin/src/main/java/me/realized/duels/DuelsPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ private boolean load() {
loadable.handleLoad();
lastLoad = loadables.indexOf(loadable);
} catch (Exception ex) {
// Handles the case of exceptions from LogManager not being logged in file
if (loadable instanceof LogSource) {
ex.printStackTrace();
}

Log.error("There was an error while loading " + loadable.getClass().getSimpleName()
+ "! If you believe this is an issue from the plugin, please contact the developer.", ex);
return false;
Expand Down
10 changes: 5 additions & 5 deletions duels-plugin/src/main/java/me/realized/duels/api/DuelsAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

/**
* A static API for Duels.
* (This is an old API for Duels v2 and below.)
* (This is an old, deprecated API for Duels v2 and below.)
*
* @deprecated as of v3.0.0, use {@link Duels} instead.
* @deprecated As of v3.0.0, use {@link Duels} instead.
*/

public class DuelsAPI {
Expand All @@ -21,7 +21,7 @@ public class DuelsAPI {
* @deprecated As of v3.0.0, use {@link UserManager#get(UUID)} instead.
*/
@Deprecated
public static UserData getUser(UUID uuid, boolean force) {
public static UserData getUser(final UUID uuid, boolean force) {
return DuelsPlugin.getInstance().getUserManager().get(uuid);
}

Expand All @@ -30,7 +30,7 @@ public static UserData getUser(UUID uuid, boolean force) {
* @deprecated As of v3.0.0, use {@link UserManager#get(Player)} instead.
*/
@Deprecated
public static UserData getUser(Player player, boolean force) {
public static UserData getUser(final Player player, boolean force) {
return DuelsPlugin.getInstance().getUserManager().get(player);
}

Expand All @@ -39,7 +39,7 @@ public static UserData getUser(Player player, boolean force) {
* @deprecated As of v3.0.0, use {@link ArenaManager#isInMatch(Player)} instead.
*/
@Deprecated
public static boolean isInMatch(Player player) {
public static boolean isInMatch(final Player player) {
return DuelsPlugin.getInstance().getArenaManager().isInMatch(player);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class Arena extends BaseButton implements me.realized.duels.api.arena.Are
@Getter
private final String name;
@Getter
private Map<Integer, Location> positions = new HashMap<>();
private final Map<Integer, Location> positions = new HashMap<>();
@Getter
private boolean disabled;
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

public class ArenaManager implements Loadable, me.realized.duels.api.arena.ArenaManager, Listener {

private static final long AUTO_SAVE_INTERVAL = 20L * 60 * 5;

private final DuelsPlugin plugin;
private final Config config;
private final Lang lang;
Expand All @@ -52,6 +54,7 @@ public class ArenaManager implements Loadable, me.realized.duels.api.arena.Arena

@Getter
private MultiPageGui<DuelsPlugin> gui;
private int autoSaveTask;

public ArenaManager(final DuelsPlugin plugin) {
this.plugin = plugin;
Expand Down Expand Up @@ -91,14 +94,29 @@ public void handleLoad() throws IOException {

Log.info(this, "Loaded " + arenas.size() + " arena(s).");
gui.calculatePages();

this.autoSaveTask = plugin.doSyncRepeat(() -> {
try {
saveArenas();
} catch (IOException ex) {
Log.error(this, ex.getMessage(), ex);
}
}, AUTO_SAVE_INTERVAL, AUTO_SAVE_INTERVAL).getTaskId();
}

@Override
public void handleUnload() throws IOException {
plugin.cancelTask(autoSaveTask);

if (gui != null) {
plugin.getGuiListener().removeGui(gui);
}

saveArenas();
arenas.clear();
}

private void saveArenas() throws IOException {
final List<ArenaData> data = new ArrayList<>();

for (final Arena arena : arenas) {
Expand All @@ -110,11 +128,9 @@ public void handleUnload() throws IOException {
}

try (Writer writer = new OutputStreamWriter(new FileOutputStream(file))) {
writer.write(plugin.getGson().toJson(data));
plugin.getGson().toJson(data, writer);
writer.flush();
}

arenas.clear();
}

@Nullable
Expand Down
Loading

0 comments on commit 3880863

Please sign in to comment.