Restructures the plugin and starts work on cleaning and commenting the code

This commit is contained in:
2021-02-07 03:37:25 +01:00
parent 2480a432a4
commit 6ff998ac3b
46 changed files with 4918 additions and 4690 deletions

View File

@ -0,0 +1,199 @@
package net.knarcraft.stargate;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Sign;
import org.bukkit.block.data.type.WallSign;
/*
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011 Shaun (sturmeh)
* Copyright (C) 2011 Dinnerbone
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This class represents a block location
*/
public class BlockLocation {
private final int x;
private final int y;
private final int z;
private final World world;
private BlockLocation parent = null;
/**
* Creates a new block
* @param world <p>The world the block exists in</p>
* @param x <p>The x coordinate of the block</p>
* @param y <p>The y coordinate of the block</p>
* @param z <p>The z coordinate of the block</p>
*/
public BlockLocation(World world, int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
this.world = world;
}
/**
* Copies a craftbukkit block
* @param block <p>The block to </p>
*/
public BlockLocation(Block block) {
this.x = block.getX();
this.y = block.getY();
this.z = block.getZ();
this.world = block.getWorld();
}
/**
* Creates a new block from a location
* @param location <p>The location the block exists in</p>
*/
public BlockLocation(Location location) {
this.x = location.getBlockX();
this.y = location.getBlockY();
this.z = location.getBlockZ();
this.world = location.getWorld();
}
/**
* Gets a block from a string
* @param world <p>The world the block exists in</p>
* @param string <p>A comma separated list of z, y and z coordinates as integers</p>
*/
public BlockLocation(World world, String string) {
String[] split = string.split(",");
this.x = Integer.parseInt(split[0]);
this.y = Integer.parseInt(split[1]);
this.z = Integer.parseInt(split[2]);
this.world = world;
}
/**
* Makes a new block in a relative position to this block
* @param x <p>The x position relative to this block's position</p>
* @param y <p>The y position relative to this block's position</p>
* @param z <p>The z position relative to this block's position</p>
* @return <p>A new block </p>
*/
public BlockLocation makeRelative(int x, int y, int z) {
return new BlockLocation(this.world, this.x + x, this.y + y, this.z + z);
}
public Location makeRelativeLoc(double x, double y, double z, float rotX, float rotY) {
return new Location(this.world, (double) this.x + x, (double) this.y + y, (double) this.z + z, rotX, rotY);
}
public BlockLocation modRelative(int right, int depth, int distance, int modX, int modY, int modZ) {
return makeRelative(-right * modX + distance * modZ, -depth * modY, -right * modZ + -distance * modX);
}
public Location modRelativeLoc(double right, double depth, double distance, float rotX, float rotY, int modX, int modY, int modZ) {
return makeRelativeLoc(0.5 + -right * modX + distance * modZ, depth, 0.5 + -right * modZ + -distance * modX, rotX, 0);
}
public void setType(Material type) {
world.getBlockAt(x, y, z).setType(type);
}
public Material getType() {
return world.getBlockAt(x, y, z).getType();
}
public Block getBlock() {
return world.getBlockAt(x, y, z);
}
public Location getLocation() {
return new Location(world, x, y, z);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
public World getWorld() {
return world;
}
public Block getParent() {
if (parent == null) findParent();
if (parent == null) return null;
return parent.getBlock();
}
private void findParent() {
int offsetX = 0;
int offsetY = 0;
int offsetZ = 0;
BlockData blk = getBlock().getBlockData();
if (blk instanceof WallSign) {
BlockFace facing = ((WallSign) blk).getFacing().getOppositeFace();
offsetX = facing.getModX();
offsetZ = facing.getModZ();
} else if (blk instanceof Sign) {
offsetY = -1;
} else {
return;
}
parent = new BlockLocation(world, getX() + offsetX, getY() + offsetY, getZ() + offsetZ);
}
@Override
public String toString() {
return String.valueOf(x) + ',' + y + ',' + z;
}
@Override
public int hashCode() {
int result = 18;
result = result * 27 + x;
result = result * 27 + y;
result = result * 27 + z;
result = result * 27 + world.getName().hashCode();
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass()) return false;
BlockLocation blockLocation = (BlockLocation) obj;
return (x == blockLocation.x) && (y == blockLocation.y) && (z == blockLocation.z) && (world.getName().equals(blockLocation.world.getName()));
}
}

View File

@ -0,0 +1,48 @@
package net.knarcraft.stargate;
import org.bukkit.Axis;
import org.bukkit.Material;
public class BloxPopulator {
private BlockLocation blockLocation;
private Material nextMat;
private Axis nextAxis;
public BloxPopulator(BlockLocation b, Material m) {
blockLocation = b;
nextMat = m;
nextAxis = null;
}
public BloxPopulator(BlockLocation b, Material m, Axis a) {
blockLocation = b;
nextMat = m;
nextAxis = a;
}
public void setBlockLocation(BlockLocation b) {
blockLocation = b;
}
public void setMat(Material m) {
nextMat = m;
}
public void setAxis(Axis a) {
nextAxis = a;
}
public BlockLocation getBlockLocation() {
return blockLocation;
}
public Material getMat() {
return nextMat;
}
public Axis getAxis() {
return nextAxis;
}
}

View File

@ -0,0 +1,58 @@
package net.knarcraft.stargate;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
public class BungeeCoordListener implements PluginMessageListener {
@Override
public void onPluginMessageReceived(String channel, Player unused, byte[] message) {
if (!Stargate.enableBungee || !channel.equals("BungeeCord")) return;
// Get data from message
String inChannel;
byte[] data;
try {
DataInputStream in = new DataInputStream(new ByteArrayInputStream(message));
inChannel = in.readUTF();
short len = in.readShort();
data = new byte[len];
in.readFully(data);
} catch (IOException ex) {
Stargate.log.severe("[stargate] Error receiving BungeeCord message");
ex.printStackTrace();
return;
}
// Verify that it's an SGBungee packet
if (!inChannel.equals("SGBungee")) {
return;
}
// Data should be player name, and destination gate name
String msg = new String(data);
String[] parts = msg.split("#@#");
String playerName = parts[0];
String destination = parts[1];
// Check if the player is online, if so, teleport, otherwise, queue
Player player = Stargate.server.getPlayer(playerName);
if (player == null) {
Stargate.bungeeQueue.put(playerName.toLowerCase(), destination);
} else {
Portal dest = Portal.getBungeeGate(destination);
// Specified an invalid gate. For now we'll just let them connect at their current location
if (dest == null) {
Stargate.log.info("[stargate] Bungee gate " + destination + " does not exist");
return;
}
dest.teleport(player, dest, null);
}
}
}

View File

@ -0,0 +1,43 @@
package net.knarcraft.stargate;
import java.io.InputStream;
/*
* stargate - A portal plugin for Bukkit
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* A holding class for methods shared between classes
*
* @author Kristian Knarvik <kristian.knarvik@knett.no>
*/
public final class CommonFunctions {
private CommonFunctions() {}
/**
* Gets a resource as an InputStream
*
* @param resourceName <p>The name of the resource you want to readFromServer</p>
* @return <p>An input stream which can be used to access the resource</p>
*/
public static InputStream getResourceAsStream(String resourceName) {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
return classloader.getResourceAsStream(resourceName);
}
}

View File

@ -0,0 +1,106 @@
package net.knarcraft.stargate;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import java.util.UUID;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class EconomyHandler {
public static boolean economyEnabled = false;
public static Economy economy = null;
public static Plugin vault = null;
public static int useCost = 0;
public static int createCost = 0;
public static int destroyCost = 0;
public static boolean toOwner = false;
public static boolean chargeFreeDestination = true;
public static boolean freeGatesGreen = false;
public static double getBalance(Player player) {
if (!economyEnabled) return 0;
return economy.getBalance(player);
}
public static boolean chargePlayer(Player player, String target, double amount) {
if (!economyEnabled) return true;
if (player.getName().equals(target)) return true;
if (economy != null) {
if (!economy.has(player, amount)) return false;
economy.withdrawPlayer(player, amount);
economy.depositPlayer(target, amount);
}
return true;
}
public static boolean chargePlayer(Player player, UUID target, double amount) {
if (!economyEnabled) return true;
if (player.getUniqueId().compareTo(target) == 0) return true;
if (economy != null) {
if (!economy.has(player, amount)) return false;
economy.withdrawPlayer(player, amount);
economy.depositPlayer(Bukkit.getOfflinePlayer(target), amount);
}
return true;
}
public static boolean chargePlayer(Player player, double amount) {
if (!economyEnabled) return true;
if (economy != null) {
if (!economy.has(player, amount)) return false;
economy.withdrawPlayer(player, amount);
}
return true;
}
public static String format(int amt) {
if (economyEnabled) {
return economy.format(amt);
}
return "";
}
public static boolean setupEconomy(PluginManager pm) {
if (!economyEnabled) return false;
// Check for Vault
Plugin p = pm.getPlugin("Vault");
if (p != null && p.isEnabled()) {
RegisteredServiceProvider<Economy> economyProvider = Stargate.server.getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
if (economyProvider != null) {
economy = economyProvider.getProvider();
vault = p;
return true;
}
}
economyEnabled = false;
return false;
}
public static boolean useEconomy() {
return economyEnabled && economy != null;
}
}

View File

@ -0,0 +1,516 @@
package net.knarcraft.stargate;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.Block;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.logging.Level;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011 Shaun (sturmeh)
* Copyright (C) 2011 Dinnerbone
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class Gate {
private static final Character ANYTHING = ' ';
private static final Character ENTRANCE = '.';
private static final Character EXIT = '*';
private static final HashMap<String, Gate> gates = new HashMap<>();
private static final HashMap<Material, ArrayList<Gate>> controlBlocks = new HashMap<>();
private static final HashSet<Material> frameBlocks = new HashSet<>();
private final String filename;
private final Character[][] layout;
private final HashMap<Character, Material> types;
private RelativeBlockVector[] entrances = new RelativeBlockVector[0];
private RelativeBlockVector[] border = new RelativeBlockVector[0];
private RelativeBlockVector[] controls = new RelativeBlockVector[0];
private RelativeBlockVector exitBlock = null;
private final HashMap<RelativeBlockVector, Integer> exits = new HashMap<>();
private Material portalBlockOpen = Material.NETHER_PORTAL;
private Material portalBlockClosed = Material.AIR;
private Material button = Material.STONE_BUTTON;
// Economy information
private int useCost = -1;
private int createCost = -1;
private int destroyCost = -1;
private boolean toOwner = false;
public Gate(String filename, Character[][] layout, HashMap<Character, Material> types) {
this.filename = filename;
this.layout = layout;
this.types = types;
populateCoordinates();
}
private void populateCoordinates() {
ArrayList<RelativeBlockVector> entranceList = new ArrayList<>();
ArrayList<RelativeBlockVector> borderList = new ArrayList<>();
ArrayList<RelativeBlockVector> controlList = new ArrayList<>();
RelativeBlockVector[] relativeExits = new RelativeBlockVector[layout[0].length];
int[] exitDepths = new int[layout[0].length];
RelativeBlockVector lastExit = null;
for (int y = 0; y < layout.length; y++) {
for (int x = 0; x < layout[y].length; x++) {
Character key = layout[y][x];
if (key.equals('-')) {
controlList.add(new RelativeBlockVector(x, y, 0));
}
if (key.equals(ENTRANCE) || key.equals(EXIT)) {
entranceList.add(new RelativeBlockVector(x, y, 0));
exitDepths[x] = y;
if (key.equals(EXIT)) {
this.exitBlock = new RelativeBlockVector(x, y, 0);
}
} else if (!key.equals(ANYTHING)) {
borderList.add(new RelativeBlockVector(x, y, 0));
}
}
}
for (int x = 0; x < exitDepths.length; x++) {
relativeExits[x] = new RelativeBlockVector(x, exitDepths[x], 0);
}
for (int x = relativeExits.length - 1; x >= 0; x--) {
if (relativeExits[x] != null) {
lastExit = relativeExits[x];
} else {
relativeExits[x] = lastExit;
}
if (exitDepths[x] > 0) this.exits.put(relativeExits[x], x);
}
this.entrances = entranceList.toArray(this.entrances);
this.border = borderList.toArray(this.border);
this.controls = controlList.toArray(this.controls);
}
public void save(String gateFolder) {
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(gateFolder + filename));
writeConfig(bw, "portal-open", portalBlockOpen.name());
writeConfig(bw, "portal-closed", portalBlockClosed.name());
writeConfig(bw, "button", button.name());
if (useCost != -1)
writeConfig(bw, "usecost", useCost);
if (createCost != -1)
writeConfig(bw, "createcost", createCost);
if (destroyCost != -1)
writeConfig(bw, "destroycost", destroyCost);
writeConfig(bw, "toowner", toOwner);
for (Map.Entry<Character, Material> entry : types.entrySet()) {
Character type = entry.getKey();
Material value = entry.getValue();
// Skip control values
if (type.equals(ANYTHING) || type.equals(ENTRANCE) || type.equals(EXIT)) {
continue;
}
bw.append(type);
bw.append('=');
if (value != null) {
bw.append(value.toString());
}
bw.newLine();
}
bw.newLine();
for (Character[] aLayout : layout) {
for (Character symbol : aLayout) {
bw.append(symbol);
}
bw.newLine();
}
bw.close();
} catch (IOException ex) {
Stargate.log.log(Level.SEVERE, "Could not save Gate " + filename + " - " + ex.getMessage());
}
}
private void writeConfig(BufferedWriter bw, String key, int value) throws IOException {
bw.append(String.format("%s=%d", key, value));
bw.newLine();
}
private void writeConfig(BufferedWriter bw, String key, boolean value) throws IOException {
bw.append(String.format("%s=%b", key, value));
bw.newLine();
}
private void writeConfig(BufferedWriter bw, String key, String value) throws IOException {
bw.append(String.format("%s=%s", key, value));
bw.newLine();
}
public Character[][] getLayout() {
return layout;
}
public HashMap<Character, Material> getTypes() {
return types;
}
public RelativeBlockVector[] getEntrances() {
return entrances;
}
public RelativeBlockVector[] getBorder() {
return border;
}
public RelativeBlockVector[] getControls() {
return controls;
}
public HashMap<RelativeBlockVector, Integer> getExits() {
return exits;
}
public RelativeBlockVector getExit() {
return exitBlock;
}
public Material getControlBlock() {
return types.get('-');
}
public String getFilename() {
return filename;
}
public Material getPortalBlockOpen() {
return portalBlockOpen;
}
public void setPortalBlockOpen(Material type) {
portalBlockOpen = type;
}
public Material getPortalBlockClosed() {
return portalBlockClosed;
}
public void setPortalBlockClosed(Material type) {
portalBlockClosed = type;
}
public Material getButton() {
return button;
}
public int getUseCost() {
if (useCost < 0) return EconomyHandler.useCost;
return useCost;
}
public Integer getCreateCost() {
if (createCost < 0) return EconomyHandler.createCost;
return createCost;
}
public Integer getDestroyCost() {
if (destroyCost < 0) return EconomyHandler.destroyCost;
return destroyCost;
}
public Boolean getToOwner() {
return toOwner;
}
public boolean matches(BlockLocation topleft, int modX, int modZ) {
return matches(topleft, modX, modZ, false);
}
public boolean matches(BlockLocation topleft, int modX, int modZ, boolean onCreate) {
HashMap<Character, Material> portalTypes = new HashMap<>(types);
for (int y = 0; y < layout.length; y++) {
for (int x = 0; x < layout[y].length; x++) {
Character key = layout[y][x];
if (key.equals(ENTRANCE) || key.equals(EXIT)) {
if (Stargate.ignoreEntrance) continue;
Material type = topleft.modRelative(x, y, 0, modX, 1, modZ).getType();
// Ignore entrance if it's air and we're creating a new gate
if (onCreate && type == Material.AIR) continue;
if (type != portalBlockClosed && type != portalBlockOpen) {
Stargate.debug("Gate::Matches", "Entrance/Exit Material Mismatch: " + type);
return false;
}
} else if (!key.equals(ANYTHING)) {
Material id = portalTypes.get(key);
if (id == null) {
portalTypes.put(key, topleft.modRelative(x, y, 0, modX, 1, modZ).getType());
} else if (topleft.modRelative(x, y, 0, modX, 1, modZ).getType() != id) {
Stargate.debug("Gate::Matches", "Block Type Mismatch: " + topleft.modRelative(x, y, 0, modX, 1, modZ).getType() + " != " + id);
return false;
}
}
}
}
return true;
}
public static void registerGate(Gate gate) {
gates.put(gate.getFilename(), gate);
Material blockID = gate.getControlBlock();
if (!controlBlocks.containsKey(blockID)) {
controlBlocks.put(blockID, new ArrayList<>());
}
controlBlocks.get(blockID).add(gate);
}
public static Gate loadGate(File file) {
Scanner scanner = null;
boolean designing = false;
ArrayList<ArrayList<Character>> design = new ArrayList<>();
HashMap<Character, Material> types = new HashMap<>();
HashMap<String, String> config = new HashMap<>();
HashSet<Material> frameTypes = new HashSet<>();
int cols = 0;
// Init types map
types.put(ENTRANCE, Material.AIR);
types.put(EXIT, Material.AIR);
types.put(ANYTHING, Material.AIR);
try {
scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
if (designing) {
ArrayList<Character> row = new ArrayList<>();
if (line.length() > cols) {
cols = line.length();
}
for (Character symbol : line.toCharArray()) {
if ((symbol.equals('?')) || (!types.containsKey(symbol))) {
Stargate.log.log(Level.SEVERE, "Could not load Gate " + file.getName() + " - Unknown symbol '" + symbol + "' in diagram");
return null;
}
row.add(symbol);
}
design.add(row);
} else {
if ((line.isEmpty()) || (!line.contains("="))) {
designing = true;
} else {
String[] split = line.split("=");
String key = split[0].trim();
String value = split[1].trim();
if (key.length() == 1) {
Character symbol = key.charAt(0);
Material id = Material.getMaterial(value);
if (id == null) {
throw new Exception("Invalid material in line: " + line);
}
types.put(symbol, id);
frameTypes.add(id);
} else {
config.put(key, value);
}
}
}
}
} catch (Exception ex) {
Stargate.log.log(Level.SEVERE, "Could not load Gate " + file.getName() + " - " + ex.getMessage());
return null;
} finally {
if (scanner != null) scanner.close();
}
Character[][] layout = new Character[design.size()][cols];
for (int y = 0; y < design.size(); y++) {
ArrayList<Character> row = design.get(y);
Character[] result = new Character[cols];
for (int x = 0; x < cols; x++) {
if (x < row.size()) {
result[x] = row.get(x);
} else {
result[x] = ' ';
}
}
layout[y] = result;
}
Gate gate = new Gate(file.getName(), layout, types);
gate.portalBlockOpen = readConfig(config, gate, file, "portal-open", gate.portalBlockOpen);
gate.portalBlockClosed = readConfig(config, gate, file, "portal-closed", gate.portalBlockClosed);
gate.button = readConfig(config, gate, file, "button", gate.button);
gate.useCost = readConfig(config, gate, file, "usecost", -1);
gate.destroyCost = readConfig(config, gate, file, "destroycost", -1);
gate.createCost = readConfig(config, gate, file, "createcost", -1);
gate.toOwner = (config.containsKey("toowner") ? Boolean.valueOf(config.get("toowner")) : EconomyHandler.toOwner);
if (gate.getControls().length != 2) {
Stargate.log.log(Level.SEVERE, "Could not load Gate " + file.getName() + " - Gates must have exactly 2 control points.");
return null;
}
if (!Tag.BUTTONS.isTagged(gate.button)) {
Stargate.log.log(Level.SEVERE, "Could not load Gate " + file.getName() + " - Gate button must be a type of button.");
return null;
}
// Merge frame types, add open mat to list
frameBlocks.addAll(frameTypes);
gate.save(file.getParent() + "/"); // Updates format for version changes
return gate;
}
private static int readConfig(HashMap<String, String> config, Gate gate, File file, String key, int def) {
if (config.containsKey(key)) {
try {
return Integer.parseInt(config.get(key));
} catch (NumberFormatException ex) {
Stargate.log.log(Level.WARNING, String.format("%s reading %s: %s is not numeric", ex.getClass().getName(), file, key));
}
}
return def;
}
private static Material readConfig(HashMap<String, String> config, Gate gate, File file, String key, Material def) {
if (config.containsKey(key)) {
Material mat = Material.getMaterial(config.get(key));
if (mat != null) {
return mat;
}
Stargate.log.log(Level.WARNING, String.format("Error reading %s: %s is not a material", file, key));
}
return def;
}
public static void loadGates(String gateFolder) {
File dir = new File(gateFolder);
File[] files;
if (dir.exists()) {
files = dir.listFiles(new StargateFilenameFilter());
} else {
files = new File[0];
}
if (files == null || files.length == 0) {
if (dir.mkdir()) {
populateDefaults(gateFolder);
}
} else {
for (File file : files) {
Gate gate = loadGate(file);
if (gate != null) registerGate(gate);
}
}
}
public static void populateDefaults(String gateFolder) {
Character[][] layout = new Character[][]{
{' ', 'X', 'X', ' '},
{'X', '.', '.', 'X'},
{'-', '.', '.', '-'},
{'X', '*', '.', 'X'},
{' ', 'X', 'X', ' '},
};
HashMap<Character, Material> types = new HashMap<>();
types.put(ENTRANCE, Material.AIR);
types.put(EXIT, Material.AIR);
types.put(ANYTHING, Material.AIR);
types.put('X', Material.OBSIDIAN);
types.put('-', Material.OBSIDIAN);
Gate gate = new Gate("nethergate.gate", layout, types);
gate.save(gateFolder);
registerGate(gate);
}
public static Gate[] getGatesByControlBlock(Block block) {
return getGatesByControlBlock(block.getType());
}
public static Gate[] getGatesByControlBlock(Material type) {
Gate[] result = new Gate[0];
ArrayList<Gate> lookup = controlBlocks.get(type);
if (lookup != null) result = lookup.toArray(result);
return result;
}
public static Gate getGateByName(String name) {
return gates.get(name);
}
public static int getGateCount() {
return gates.size();
}
public static boolean isGateBlock(Material type) {
return frameBlocks.contains(type);
}
static class StargateFilenameFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return name.endsWith(".gate");
}
}
public static void clearGates() {
gates.clear();
controlBlocks.clear();
frameBlocks.clear();
}
}

View File

@ -0,0 +1,311 @@
package net.knarcraft.stargate;
import org.bukkit.ChatColor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/*
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* This class is responsible for loading all strings which are translated into several languages
*/
public class LanguageLoader {
// Variables
private final String languageFolder;
private String chosenLanguage;
private Map<String, String> loadedStringTranslations;
private final Map<String, String> loadedBackupStrings;
/**
* Instantiates a new language loader
* @param languageFolder <p>The folder containing the language files</p>
* @param chosenLanguage <p>The chosen plugin language</p>
*/
public LanguageLoader(String languageFolder, String chosenLanguage) {
this.chosenLanguage = chosenLanguage;
this.languageFolder = languageFolder;
File tmp = new File(languageFolder, chosenLanguage + ".txt");
if (!tmp.exists()) {
tmp.getParentFile().mkdirs();
}
updateLanguage(chosenLanguage);
loadedStringTranslations = load(chosenLanguage);
// We have a default hashMap used for when new text is added.
InputStream inputStream = getClass().getResourceAsStream("/lang/" + chosenLanguage + ".txt");
if (inputStream != null) {
loadedBackupStrings = load("en", inputStream);
} else {
loadedBackupStrings = null;
Stargate.log.severe("[stargate] Error loading backup language. There may be missing text in-game");
}
}
/**
* Reloads languages from the files on disk
*/
public void reload() {
// This extracts/updates the language as needed
updateLanguage(chosenLanguage);
loadedStringTranslations = load(chosenLanguage);
}
/**
* Gets the string to display given its name/key
* @param name <p>The name/key of the string to display</p>
* @return <p>The string in the user's preferred language</p>
*/
public String getString(String name) {
String val = loadedStringTranslations.get(name);
if (val == null && loadedBackupStrings != null) val = loadedBackupStrings.get(name);
if (val == null) return "";
return val;
}
/**
* Sets the chosen plugin language
* @param chosenLanguage <p>The new plugin language</p>
*/
public void setChosenLanguage(String chosenLanguage) {
this.chosenLanguage = chosenLanguage;
}
/**
* Updates files in the plugin directory with contents from the compiled .jar
* @param language <p>The language to update</p>
*/
private void updateLanguage(String language) {
// Load the current language file
ArrayList<String> keyList = new ArrayList<>();
ArrayList<String> valueList = new ArrayList<>();
Map<String, String> currentLanguageValues = load(language);
InputStream inputStream = getClass().getResourceAsStream("/lang/" + language + ".txt");
if (inputStream == null) return;
boolean updated = false;
FileOutputStream fileOutputStream = null;
try {
if (readChangedLanguageStrings(inputStream, keyList, valueList, currentLanguageValues)) {
updated = true;
}
// Save file
fileOutputStream = new FileOutputStream(languageFolder + language + ".txt");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
BufferedWriter bufferedWriter = new BufferedWriter(outputStreamWriter);
// Output normal Language data
for (int i = 0; i < keyList.size(); i++) {
bufferedWriter.write(keyList.get(i) + valueList.get(i));
bufferedWriter.newLine();
}
bufferedWriter.newLine();
// Output any custom language strings the user had
if (currentLanguageValues != null) {
for (String key : currentLanguageValues.keySet()) {
bufferedWriter.write(key + "=" + currentLanguageValues.get(key));
bufferedWriter.newLine();
}
}
bufferedWriter.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (Exception e) {
//Ignored
}
}
}
if (updated) {
Stargate.log.info("[stargate] Your language file (" + language + ".txt) has been updated");
}
}
/**
* Reads language strings
* @param inputStream <p>The input stream to read from</p>
* @param keyList <p>The key list to add keys to</p>
* @param valueList <p>The value list to add values to</p>
* @param currentLanguageValues <p>The current values of the loaded/processed language</p>
* @return <p>True if at least one line was updated</p>
* @throws IOException <p>if unable to read a language file</p>
*/
private boolean readChangedLanguageStrings(InputStream inputStream, List<String> keyList, List<String> valueList,
Map<String, String> currentLanguageValues) throws IOException {
boolean updated = false;
// Input stuff
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = bufferedReader.readLine();
boolean firstLine = true;
while (line != null) {
// Strip UTF BOM
if (firstLine) {
line = removeUTF8BOM(line);
firstLine = false;
}
// Split at first "="
int equalSignIndex = line.indexOf('=');
if (equalSignIndex == -1) {
keyList.add("");
valueList.add("");
line = bufferedReader.readLine();
continue;
}
String key = line.substring(0, equalSignIndex);
String value = line.substring(equalSignIndex);
if (currentLanguageValues == null || currentLanguageValues.get(key) == null) {
keyList.add(key);
valueList.add(value);
updated = true;
} else {
keyList.add(key);
valueList.add("=" + currentLanguageValues.get(key).replace('\u00A7', '&'));
currentLanguageValues.remove(key);
}
line = bufferedReader.readLine();
}
bufferedReader.close();
return updated;
}
/**
* Loads the given language
* @param lang <p>The language to load</p>
* @return <p>A mapping between loaded string indexes and the strings to display</p>
*/
private Map<String, String> load(String lang) {
return load(lang, null);
}
/**
* Loads the given language
* @param lang <p>The language to load</p>
* @param inputStream <p>An optional input stream to use. Defaults to using a file input stream</p>
* @return <p>A mapping between loaded string indexes and the strings to display</p>
*/
private Map<String, String> load(String lang, InputStream inputStream) {
Map<String, String> strings = new HashMap<>();
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader;
try {
if (inputStream == null) {
fileInputStream = new FileInputStream(languageFolder + lang + ".txt");
inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
} else {
inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
}
readLanguageFile(inputStreamReader, strings);
} catch (Exception e) {
return null;
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
//Ignored
}
}
}
return strings;
}
/**
* Reads a language file given its input stream
* @param inputStreamReader <p>The input stream reader to read from</p>
* @param strings <p>The loaded string pairs</p>
* @throws IOException <p>If unable to read the file</p>
*/
private void readLanguageFile(InputStreamReader inputStreamReader, Map<String, String> strings) throws IOException {
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = bufferedReader.readLine();
boolean firstLine = true;
while (line != null) {
// Strip UTF BOM
if (firstLine) {
line = removeUTF8BOM(line);
firstLine = false;
}
// Split at first "="
int equalSignIndex = line.indexOf('=');
if (equalSignIndex == -1) {
line = bufferedReader.readLine();
continue;
}
String key = line.substring(0, equalSignIndex);
String val = ChatColor.translateAlternateColorCodes('&', line.substring(equalSignIndex + 1));
strings.put(key, val);
line = bufferedReader.readLine();
}
}
/**
* Prints debug output to the console for checking of loading language strings/translations
*/
public void debug() {
Set<String> keys = loadedStringTranslations.keySet();
for (String key : keys) {
Stargate.debug("LanguageLoader::Debug::loadedStringTranslations", key + " => " + loadedStringTranslations.get(key));
}
if (loadedBackupStrings == null) return;
keys = loadedBackupStrings.keySet();
for (String key : keys) {
Stargate.debug("LanguageLoader::Debug::loadedBackupStrings", key + " => " + loadedBackupStrings.get(key));
}
}
/**
* Removes the UTF-8 Byte Order Mark if present
* @param string <p>The string to remove the BOM from</p>
* @return <p>A string guaranteed without a BOM</p>
*/
private String removeUTF8BOM(String string) {
String UTF8_BOM = "\uFEFF";
if (string.startsWith(UTF8_BOM)) {
string = string.substring(1);
}
return string;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,48 @@
package net.knarcraft.stargate;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011 Shaun (sturmeh)
* Copyright (C) 2011 Dinnerbone
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class RelativeBlockVector {
private int right = 0;
private int depth = 0;
private int distance = 0;
public RelativeBlockVector(int right, int depth, int distance) {
this.right = right;
this.depth = depth;
this.distance = distance;
}
public int getRight() {
return right;
}
public int getDepth() {
return depth;
}
public int getDistance() {
return distance;
}
}

View File

@ -0,0 +1,34 @@
package net.knarcraft.stargate;
import java.util.Iterator;
/**
* This class contains the function used to close servers which should no longer be open/active
*/
public class StarGateThread implements Runnable {
public void run() {
long time = System.currentTimeMillis() / 1000;
// Close open portals
for (Iterator<Portal> iterator = Stargate.openList.iterator(); iterator.hasNext(); ) {
Portal p = iterator.next();
// Skip always open gates
if (p.isAlwaysOn()) continue;
if (!p.isOpen()) continue;
if (time > p.getOpenTime() + Stargate.getOpenTime()) {
p.close(false);
iterator.remove();
}
}
// Deactivate active portals
for (Iterator<Portal> iterator = Stargate.activeList.iterator(); iterator.hasNext(); ) {
Portal p = iterator.next();
if (!p.isActive()) continue;
if (time > p.getOpenTime() + Stargate.getActiveTime()) {
p.deactivate();
iterator.remove();
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,71 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
/*
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
@SuppressWarnings("unused")
public class StargateAccessEvent extends StargateEvent {
private final Player player;
private boolean deny;
private static final HandlerList handlers = new HandlerList();
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargateAccessEvent(Player player, Portal portal, boolean deny) {
super("StargateAccessEvent", portal);
this.player = player;
this.deny = deny;
}
/**
* Gets whether the player should be denied access
* @return <p>Whether the player should be denied access</p>
*/
public boolean getDeny() {
return this.deny;
}
/**
* Sets whether to deny the player
* @param deny <p>Whether to deny the player</p>
*/
public void setDeny(boolean deny) {
this.deny = deny;
}
public Player getPlayer() {
return this.player;
}
}

View File

@ -0,0 +1,72 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import java.util.ArrayList;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class StargateActivateEvent extends StargateEvent {
private final Player player;
private ArrayList<String> destinations;
private String destination;
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargateActivateEvent(Portal portal, Player player, ArrayList<String> destinations, String destination) {
super("StargatActivateEvent", portal);
this.player = player;
this.destinations = destinations;
this.destination = destination;
}
public Player getPlayer() {
return player;
}
public ArrayList<String> getDestinations() {
return destinations;
}
public void setDestinations(ArrayList<String> destinations) {
this.destinations = destinations;
}
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
}

View File

@ -0,0 +1,53 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.event.HandlerList;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class StargateCloseEvent extends StargateEvent {
private boolean force;
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargateCloseEvent(Portal portal, boolean force) {
super("StargateCloseEvent", portal);
this.force = force;
}
public boolean getForce() {
return force;
}
public void setForce(boolean force) {
this.force = force;
}
}

View File

@ -0,0 +1,85 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class StargateCreateEvent extends StargateEvent {
private final Player player;
private boolean deny;
private String denyReason;
private final String[] lines;
private int cost;
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargateCreateEvent(Player player, Portal portal, String[] lines, boolean deny, String denyReason, int cost) {
super("StargateCreateEvent", portal);
this.player = player;
this.lines = lines;
this.deny = deny;
this.denyReason = denyReason;
this.cost = cost;
}
public Player getPlayer() {
return player;
}
public String getLine(int index) throws IndexOutOfBoundsException {
return lines[index];
}
public boolean getDeny() {
return deny;
}
public void setDeny(boolean deny) {
this.deny = deny;
}
public String getDenyReason() {
return denyReason;
}
public void setDenyReason(String denyReason) {
this.denyReason = denyReason;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}

View File

@ -0,0 +1,41 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.event.HandlerList;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class StargateDeactivateEvent extends StargateEvent {
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargateDeactivateEvent(Portal portal) {
super("StargatDeactivateEvent", portal);
}
}

View File

@ -0,0 +1,79 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class StargateDestroyEvent extends StargateEvent {
private final Player player;
private boolean deny;
private String denyReason;
private int cost;
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargateDestroyEvent(Portal portal, Player player, boolean deny, String denyMsg, int cost) {
super("StargateDestroyEvent", portal);
this.player = player;
this.deny = deny;
this.denyReason = denyMsg;
this.cost = cost;
}
public Player getPlayer() {
return player;
}
public boolean getDeny() {
return deny;
}
public void setDeny(boolean deny) {
this.deny = deny;
}
public String getDenyReason() {
return denyReason;
}
public void setDenyReason(String denyReason) {
this.denyReason = denyReason;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
}

View File

@ -0,0 +1,54 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
/*
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* An abstract event describing any stargate event
*/
@SuppressWarnings("unused")
public abstract class StargateEvent extends Event implements Cancellable {
protected Portal portal;
protected boolean cancelled;
public StargateEvent(String event, Portal portal) {
this.portal = portal;
this.cancelled = false;
}
public Portal getPortal() {
return portal;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}

View File

@ -0,0 +1,65 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class StargateOpenEvent extends StargateEvent {
private final Player player;
private boolean force;
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargateOpenEvent(Player player, Portal portal, boolean force) {
super("StargateOpenEvent", portal);
this.player = player;
this.force = force;
}
/**
* Return the player than opened the gate.
*
* @return player than opened the gate
*/
public Player getPlayer() {
return player;
}
public boolean getForce() {
return force;
}
public void setForce(boolean force) {
this.force = force;
}
}

View File

@ -0,0 +1,85 @@
package net.knarcraft.stargate.event;
import net.knarcraft.stargate.Portal;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
/**
* stargate - A portal plugin for Bukkit
* Copyright (C) 2011, 2012 Steven "Drakia" Scott <Contact@TheDgtl.net>
* Copyright (C) 2021 Kristian Knarvik
* <p>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
public class StargatePortalEvent extends StargateEvent {
private final Player player;
private final Portal destination;
private Location exit;
private static final HandlerList handlers = new HandlerList();
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
public StargatePortalEvent(Player player, Portal portal, Portal dest, Location exit) {
super("StargatePortalEvent", portal);
this.player = player;
this.destination = dest;
this.exit = exit;
}
/**
* Return the player that went through the gate.
*
* @return player that went through the gate
*/
public Player getPlayer() {
return player;
}
/**
* Return the destination gate
*
* @return destination gate
*/
public Portal getDestination() {
return destination;
}
/**
* Return the location of the players exit point
*
* @return org.bukkit.Location Location of the exit point
*/
public Location getExit() {
return exit;
}
/**
* Set the location of the players exit point
*/
public void setExit(Location loc) {
this.exit = loc;
}
}