Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/sh/okx/railswitch/RailSwitch.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package sh.okx.railswitch;

import com.google.common.base.CharMatcher;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import sh.okx.railswitch.database.ConnectionPool;
import sh.okx.railswitch.database.MySQLConnectionPool;
import sh.okx.railswitch.database.RailSwitchDatabase;
import sh.okx.railswitch.database.SQLiteConnectionPool;
import sh.okx.railswitch.listener.DectorRailActivateListener;
import sh.okx.railswitch.listener.SignInteractListener;

public class RailSwitch extends JavaPlugin {
private boolean timings;
Expand All @@ -27,6 +30,7 @@ public void onEnable() {
PluginManager pm = getServer().getPluginManager();

pm.registerEvents(new DectorRailActivateListener(this), this);
pm.registerEvents(new SignInteractListener(this), this);
getCommand("setdestination").setExecutor(new SetDestinationCommand(this));
}

Expand Down Expand Up @@ -67,6 +71,17 @@ public boolean isValidDestination(String message) {
.or(CharMatcher.anyOf("!\"#$%&'()*+,-./;:<=>?@[]\\^_`{|}~")).matchesAllOf(message);
}

public boolean setDestination(Player player, String dest) {
if (!isValidDestination(dest)) {
player.sendMessage(ChatColor.RED + "Destinations can not be more than 40 characters and may only use alphanumerical characters and ASCII symbols.");
return true;
}

getDatabase().setPlayerDestination(player, dest);
player.sendMessage(ChatColor.GREEN + "Set your rail destination to: " + dest);
return false;
}

public boolean isTimings() {
return timings;
}
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/sh/okx/railswitch/SetDestinationCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}

String dest = args[0];
if (!plugin.isValidDestination(dest)) {
player.sendMessage(ChatColor.RED + "Destinations can not be more than 40 characters and may only use alphanumerical characters and ASCII symbols.");
return true;
}

plugin.getDatabase().setPlayerDestination(player, dest);
player.sendMessage(ChatColor.GREEN + "Set your rail destination to: " + dest);
plugin.setDestination(player, dest);

return true;
}
}
45 changes: 45 additions & 0 deletions src/main/java/sh/okx/railswitch/listener/SignInteractListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package sh.okx.railswitch.listener;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import sh.okx.railswitch.RailSwitch;
public class SignInteractListener implements Listener {
private final RailSwitch plugin;

public SignInteractListener(RailSwitch plugin) {
this.plugin = plugin;
}

@EventHandler
public void on(PlayerInteractEvent e) {
if(e.getAction() == Action.RIGHT_CLICK_BLOCK) {
Block b = e.getClickedBlock();
if(b.getType() == Material.SIGN_POST || b.getType() == Material.WALL_SIGN) {
Sign s = (Sign) b.getState();
for(int i = 0; i < 4; i++) {
String line = s.getLine(i);
if(line.toLowerCase().startsWith("/dest ")) {
Player player = e.getPlayer();
String dest = line.substring(6);

if(!dest.isEmpty())
plugin.setDestination(player, dest);
} else if(i < 3 && line.toLowerCase().endsWith("/dest")) {
Player player = e.getPlayer();
String dest = s.getLine(i + 1);

if(!dest.isEmpty())
plugin.setDestination(player, dest);
}
}
}
}
}
}