Skip to content

Commit

Permalink
Add API class / fix #23
Browse files Browse the repository at this point in the history
  • Loading branch information
kory33 committed Jun 11, 2017
1 parent 3a68ea8 commit 801f98b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
38 changes: 38 additions & 0 deletions src/main/java/com/github/kory33/signvote/api/SignVoteAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.github.kory33.signvote.api;

import com.github.kory33.signvote.core.SignVote;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;

/**
* An API of SignVote which may be openly accessed by addons.
* <p>
* Please note that you should only access the internals of SignVote plugin
* only if the desired functionality is not provided by this class or its subclasses.
* <p>
* Internal access may damage the data structure of SignVote plugin, hence
* manipulating SignVote's data should be done with a certain care.
*/
public class SignVoteAPI {
private final SignVote plugin;

public SignVoteAPI(SignVote plugin) {
this.plugin = plugin;
}

/**
* Checks if the given block is a SignVote's vote-point
* @param block target block
* @return true if the sign is a vote-point
*/
public boolean isSignVoteSign(Block block) {
BlockState blockState = block.getState();
if (!(blockState instanceof Sign)) {
return false;
}

Sign sign = (Sign) blockState;
return this.plugin.getVoteSessionManager().getVoteSession(sign).isPresent();
}
}
4 changes: 4 additions & 0 deletions src/main/java/com/github/kory33/signvote/core/SignVote.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.logging.Level;

import com.github.kory33.chatgui.command.RunnableInvoker;
import com.github.kory33.signvote.api.SignVoteAPI;
import org.bstats.Metrics;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class SignVote extends GithubUpdateNotifyPlugin {
@Getter private RunnableInvoker runnableInvoker;
@Getter private PlayerInteractiveInterfaceManager interfaceManager;
@Getter private PlayerChatInterceptor chatInterceptor;
@Getter private SignVoteAPI API;

private boolean isEnabled = false;

Expand Down Expand Up @@ -110,6 +112,8 @@ public void onEnable() {

this.enableMetrics();
this.isEnabled = true;

this.API = new SignVoteAPI(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.kory33.signvote.listeners;

import com.github.kory33.signvote.utils.BlockUtil;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
Expand All @@ -16,15 +18,21 @@
import com.github.kory33.signvote.manager.VoteSessionManager;
import com.github.kory33.signvote.model.VotePoint;
import com.github.kory33.signvote.session.VoteSession;
import org.bukkit.material.MaterialData;

import java.util.Optional;
import java.util.Set;

/**
* A Listener implementation which listens to player's interaction with sign.
*/
public class SignListener implements Listener {
private final JSONConfiguration messageConfig;
private final VoteSessionManager voteSessionManager;
private final SignVote plugin;

public SignListener(SignVote plugin) {
this.plugin = plugin;
plugin.getServer().getPluginManager().registerEvents(this, plugin);

this.messageConfig = plugin.getMessagesConfiguration();
Expand Down Expand Up @@ -86,17 +94,48 @@ public void onVotePointBreak(BlockBreakEvent event) {
}

Sign sign = (Sign)state;
VotePoint votePoint = this.voteSessionManager.getVotePoint(sign);

if (votePoint == null) {
Optional<VoteSession> optionalSession = this.voteSessionManager.getVoteSession(sign);
if (!optionalSession.isPresent()) {
return;
}

String sessionName = voteSessionManager.getVoteSession(sign).getName();
String votepointName = votePoint.getName();
VoteSession session = optionalSession.get();

String sessionName = session.getName();
String votepointName = session.getVotePoint(sign).getName();

event.setCancelled(true);
event.getPlayer().sendMessage(messageConfig.getFormatted(MessageConfigNodes.F_VOTEPOINT_BREAK,
sessionName, votepointName));
}

@EventHandler
public void onVotePointBaseBlockBreak(BlockBreakEvent event) {
Block brokenBlock = event.getBlock();

Set<Block> attachedVotePoints = BlockUtil.getBlocksAdjacentTo(brokenBlock);
attachedVotePoints.removeIf(block -> {
// ignore if the block is not a sign
MaterialData state = block.getState().getData();
if (!(state instanceof org.bukkit.material.Sign)) {
return true;
}

// ignore if the sign is not attached to the broken block
org.bukkit.material.Sign signMaterial = (org.bukkit.material.Sign) state;
Block attachedBlock = block.getRelative(signMaterial.getAttachedFace());
if (!attachedBlock.equals(brokenBlock)) {
return true;
}

// ignore if the sign is not a SignVote's vote-point
return !this.plugin.getAPI().isSignVoteSign(block);
});

if (attachedVotePoints.isEmpty()) {
return;
}

event.setCancelled(true);
}
}

0 comments on commit 801f98b

Please sign in to comment.