Compare commits
No commits in common. "development" and "master" have entirely different histories.
developmen
...
master
38
pom.xml
38
pom.xml
@ -7,12 +7,10 @@
|
|||||||
<name>Craft Inc. Gates</name>
|
<name>Craft Inc. Gates</name>
|
||||||
<url>http://dev.bukkit.org/bukkit-plugins/craftinc-gates/</url>
|
<url>http://dev.bukkit.org/bukkit-plugins/craftinc-gates/</url>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<version>2.4.1</version>
|
<version>2.4.0</version>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.source>1.7</maven.compiler.source>
|
|
||||||
<maven.compiler.target>1.7</maven.compiler.target>
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<!-- License -->
|
<!-- License -->
|
||||||
@ -92,32 +90,22 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
<!--Spigot API-->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.spigotmc</groupId>
|
|
||||||
<artifactId>spigot-api</artifactId>
|
|
||||||
<version>1.11-R0.1-SNAPSHOT</version>
|
|
||||||
<scope>provided</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!--Bukkit API-->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bukkit</groupId>
|
<groupId>org.bukkit</groupId>
|
||||||
<artifactId>bukkit</artifactId>
|
<artifactId>bukkit</artifactId>
|
||||||
<version>1.11-R0.1-SNAPSHOT</version>
|
<version>1.7.2-R0.2-SNAPSHOT</version>
|
||||||
<scope>provided</scope>
|
<type>jar</type>
|
||||||
</dependency>
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
<!--Vault-->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>net.milkbowl.vault</groupId>
|
<groupId>net.milkbowl.vault</groupId>
|
||||||
<artifactId>Vault</artifactId>
|
<artifactId>Vault</artifactId>
|
||||||
<version>1.6.6</version>
|
<version>1.2.27-SNAPSHOT</version>
|
||||||
<type>jar</type>
|
<type>jar</type>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!--Metrics-->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mcstats.bukkit</groupId>
|
<groupId>org.mcstats.bukkit</groupId>
|
||||||
<artifactId>metrics</artifactId>
|
<artifactId>metrics</artifactId>
|
||||||
@ -130,13 +118,13 @@
|
|||||||
<repositories>
|
<repositories>
|
||||||
|
|
||||||
<repository>
|
<repository>
|
||||||
<id>spigot-repo</id>
|
<id>bukkit-repo</id>
|
||||||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
<url>http://repo.bukkit.org/content/groups/public</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
<repository>
|
<repository>
|
||||||
<id>vault-repo</id>
|
<id>vault-repo</id>
|
||||||
<url>http://nexus.hc.to/content/repositories/pub_releases</url>
|
<url>http://ci.herocraftonline.com/plugin/repository/everything</url>
|
||||||
</repository>
|
</repository>
|
||||||
|
|
||||||
<repository>
|
<repository>
|
||||||
|
367
src/de/craftinc/gates/Gate.java
Normal file
367
src/de/craftinc/gates/Gate.java
Normal file
@ -0,0 +1,367 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates;
|
||||||
|
|
||||||
|
import de.craftinc.gates.util.ConfigurationUtil;
|
||||||
|
import de.craftinc.gates.util.FloodUtil;
|
||||||
|
import de.craftinc.gates.persistence.LocationUtil;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.block.Block;
|
||||||
|
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class Gate implements ConfigurationSerializable
|
||||||
|
{
|
||||||
|
protected Location location; /* saving both location and gateBlockLocations is redundant but makes it easy to allow players to reshape gates */
|
||||||
|
protected Set<Location> gateBlockLocations = new HashSet<Location>(); /* Locations of the blocks inside the gate */
|
||||||
|
protected Set<Block> gateFrameBlocks = new HashSet<Block>();
|
||||||
|
|
||||||
|
protected Location exit;
|
||||||
|
|
||||||
|
protected boolean isHidden = false;
|
||||||
|
protected boolean isOpen = false;
|
||||||
|
|
||||||
|
protected boolean allowsVehicles = true;
|
||||||
|
|
||||||
|
protected String id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* You should never create two gates with the same 'id'. Also see 'setId(String id)'.
|
||||||
|
* @param id This parameter must not be 'null'. An exception will be thrown otherwise!
|
||||||
|
*/
|
||||||
|
public Gate(final String id)
|
||||||
|
{
|
||||||
|
setId(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return super.toString() + " " + this.getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return This method might return a 'null' data.
|
||||||
|
*/
|
||||||
|
public Location getLocation()
|
||||||
|
{
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param location Supplying 'null' is permitted.
|
||||||
|
* @throws Exception Will throw an exception if the gate is open and an invalid (no gate frame) location is
|
||||||
|
* supplied. Note that the supplied 'location' will be set even if an exception is thrown. Note that this
|
||||||
|
* gate will be closed if an exception is thrown.
|
||||||
|
*/
|
||||||
|
public void setLocation(final Location location) throws Exception
|
||||||
|
{
|
||||||
|
this.location = location;
|
||||||
|
|
||||||
|
if (isOpen) {
|
||||||
|
findPortalBlocks();
|
||||||
|
validate();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.gateBlockLocations = new HashSet<Location>();
|
||||||
|
this.gateFrameBlocks = new HashSet<Block>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return This method might return a 'null' value.
|
||||||
|
*/
|
||||||
|
public Location getExit()
|
||||||
|
{
|
||||||
|
return exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param exit Supplying 'null' is permitted.
|
||||||
|
* @throws Exception An exception will be thrown if 'null' data is supplied and this gate is open. Note that the
|
||||||
|
* supplied 'exit' will be set even if an exception is thrown. Note that this gate will be closed if an
|
||||||
|
* exception is thrown.
|
||||||
|
*/
|
||||||
|
public void setExit(final Location exit) throws Exception
|
||||||
|
{
|
||||||
|
this.exit = exit;
|
||||||
|
validate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return This method will never return 'null'.
|
||||||
|
*/
|
||||||
|
public String getId()
|
||||||
|
{
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every gate should have an unique 'id'. You should therefore check if another gate with the same 'id' exists.
|
||||||
|
* Note that this method will not check if another gate with the same 'id' exists!
|
||||||
|
* @param id This parameter must not be 'null'. An exception will be thrown otherwise!
|
||||||
|
*/
|
||||||
|
public void setId(final String id)
|
||||||
|
{
|
||||||
|
if (id == null) {
|
||||||
|
throw new IllegalArgumentException("gate 'id' cannot be 'null'");
|
||||||
|
}
|
||||||
|
|
||||||
|
this.id = id.toLowerCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isHidden()
|
||||||
|
{
|
||||||
|
return isHidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setHidden(boolean isHidden) throws Exception
|
||||||
|
{
|
||||||
|
this.isHidden = isHidden;
|
||||||
|
this.validate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean isOpen()
|
||||||
|
{
|
||||||
|
return isOpen;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setOpen(boolean isOpen) throws Exception
|
||||||
|
{
|
||||||
|
if (isOpen && !this.isOpen) {
|
||||||
|
findPortalBlocks();
|
||||||
|
}
|
||||||
|
|
||||||
|
this.isOpen = isOpen;
|
||||||
|
validate();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void setAllowsVehicles(boolean allowsVehicles)
|
||||||
|
{
|
||||||
|
this.allowsVehicles = allowsVehicles;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean getAllowsVehicles()
|
||||||
|
{
|
||||||
|
return this.allowsVehicles;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return Will never return 'null' but might return an empty Set.
|
||||||
|
*/
|
||||||
|
public Set<Location> getGateBlockLocations()
|
||||||
|
{
|
||||||
|
return gateBlockLocations;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @return Will never return 'null' but might return an empty Set.
|
||||||
|
*/
|
||||||
|
public Set<Block> getGateFrameBlocks()
|
||||||
|
{
|
||||||
|
return gateFrameBlocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
protected void findPortalBlocks()
|
||||||
|
{
|
||||||
|
gateBlockLocations = new HashSet<Location>();
|
||||||
|
Set<Block> gateBlocks = FloodUtil.getGatePortalBlocks(location.getBlock());
|
||||||
|
|
||||||
|
if (gateBlocks != null) {
|
||||||
|
for (Block b : gateBlocks) {
|
||||||
|
gateBlockLocations.add(b.getLocation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gateFrameBlocks = FloodUtil.getFrame(gateBlocks);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if values attributes do add up; will close gate on wrong values.
|
||||||
|
*/
|
||||||
|
public void validate() throws Exception
|
||||||
|
{
|
||||||
|
if (!isOpen) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (location == null) {
|
||||||
|
isOpen = false;
|
||||||
|
this.gateBlockLocations = new HashSet<Location>();
|
||||||
|
this.gateFrameBlocks = new HashSet<Block>();
|
||||||
|
|
||||||
|
throw new Exception("Gate got closed. It has no location.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (exit == null) {
|
||||||
|
isOpen = false;
|
||||||
|
this.gateBlockLocations = new HashSet<Location>();
|
||||||
|
this.gateFrameBlocks = new HashSet<Block>();
|
||||||
|
|
||||||
|
throw new Exception("Gate got closed. It has no exit.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gateBlockLocations.size() == 0) {
|
||||||
|
isOpen = false;
|
||||||
|
this.gateBlockLocations = new HashSet<Location>();
|
||||||
|
this.gateFrameBlocks = new HashSet<Block>();
|
||||||
|
|
||||||
|
throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isHidden() && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey)) {
|
||||||
|
|
||||||
|
for (Block b : gateFrameBlocks) {
|
||||||
|
|
||||||
|
if (b.getType() == Material.AIR) {
|
||||||
|
isOpen = false;
|
||||||
|
this.gateBlockLocations = new HashSet<Location>();
|
||||||
|
this.gateFrameBlocks = new HashSet<Block>();
|
||||||
|
|
||||||
|
throw new Exception("Gate got closed. The frame is missing or broken. (missing frame block(s))");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* INTERFACE: ConfigurationSerializable
|
||||||
|
*/
|
||||||
|
static protected String idKey = "id";
|
||||||
|
static protected String locationKey = "location";
|
||||||
|
static protected String gateBlocksKey = "gateBlocks";
|
||||||
|
static protected String exitKey = "exit";
|
||||||
|
static protected String isHiddenKey = "hidden";
|
||||||
|
static protected String isOpenKey = "open";
|
||||||
|
static protected String locationYawKey = "locationYaw";
|
||||||
|
static protected String locationPitchKey = "locationPitch";
|
||||||
|
static protected String exitYawKey = "exitYaw";
|
||||||
|
static protected String exitPitchKey = "exitPitch";
|
||||||
|
static protected String allowsVehiclesKey = "allowsVehiclesKey";
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public Gate(Map<String, Object> map)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
id = map.get(idKey).toString().toLowerCase();
|
||||||
|
|
||||||
|
isHidden = (Boolean)map.get(isHiddenKey);
|
||||||
|
isOpen = (Boolean)map.get(isOpenKey);
|
||||||
|
|
||||||
|
location = LocationUtil.deserializeLocation((Map<String, Object>) map.get(locationKey));
|
||||||
|
exit = LocationUtil.deserializeLocation((Map<String, Object>) map.get(exitKey));
|
||||||
|
|
||||||
|
if (map.containsKey(exitPitchKey)) {
|
||||||
|
exit.setPitch(((Number)map.get(exitPitchKey)).floatValue());
|
||||||
|
exit.setYaw(((Number)map.get(exitYawKey)).floatValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (map.containsKey(locationPitchKey)) {
|
||||||
|
location.setPitch(((Number)map.get(locationPitchKey)).floatValue());
|
||||||
|
location.setYaw(((Number)map.get(locationYawKey)).floatValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (map.containsKey(allowsVehiclesKey)) {
|
||||||
|
allowsVehicles = (Boolean)map.get(allowsVehiclesKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
gateBlockLocations = new HashSet<Location>();
|
||||||
|
List<Map<String, Object>> serializedGateBlocks = (List<Map<String, Object>>)map.get(gateBlocksKey);
|
||||||
|
|
||||||
|
for (Map<String, Object> sgb : serializedGateBlocks) {
|
||||||
|
gateBlockLocations.add(LocationUtil.deserializeLocation(sgb));
|
||||||
|
}
|
||||||
|
|
||||||
|
gateFrameBlocks = FloodUtil.getFrameWithLocations(gateBlockLocations);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Plugin.log("ERROR: Failed to load gate '" + id + "'! (" + e.getMessage() + ")");
|
||||||
|
Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!");
|
||||||
|
|
||||||
|
Plugin.getPlugin().getGatesManager().storeInvalidGate(map);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
validate(); // make sure to not write invalid stuff to disk
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
Plugin.log("The loaded gate " + this.getId() + " seems to be not valid: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Map<String, Object> serialize()
|
||||||
|
{
|
||||||
|
Map<String, Object> retVal = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
retVal.put(idKey, id);
|
||||||
|
retVal.put(locationKey, LocationUtil.serializeLocation(location));
|
||||||
|
retVal.put(exitKey, LocationUtil.serializeLocation(exit));
|
||||||
|
retVal.put(isHiddenKey, isHidden);
|
||||||
|
retVal.put(isOpenKey, isOpen);
|
||||||
|
retVal.put(allowsVehiclesKey, allowsVehicles);
|
||||||
|
|
||||||
|
if (exit != null) {
|
||||||
|
retVal.put(exitPitchKey, exit.getPitch());
|
||||||
|
retVal.put(exitYawKey, exit.getYaw());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (location != null) {
|
||||||
|
retVal.put(locationPitchKey, location.getPitch());
|
||||||
|
retVal.put(locationYawKey, location.getYaw());
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Map<String, Object>> serializedGateBlocks = new ArrayList<Map<String, Object>>();
|
||||||
|
|
||||||
|
for (Location l : gateBlockLocations) {
|
||||||
|
serializedGateBlocks.add(LocationUtil.serializeLocation(l));
|
||||||
|
}
|
||||||
|
|
||||||
|
retVal.put(gateBlocksKey, serializedGateBlocks);
|
||||||
|
|
||||||
|
return retVal;
|
||||||
|
}
|
||||||
|
}
|
31
src/de/craftinc/gates/GateChangeListener.java
Normal file
31
src/de/craftinc/gates/GateChangeListener.java
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2014 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public interface GateChangeListener
|
||||||
|
{
|
||||||
|
public static final String newGate = "GateChangeListener-newGate"; // value will be null
|
||||||
|
public static final String removedGate = "GateChangeListener-removedGate"; // value will be null
|
||||||
|
public static final String changedID = "GateChangeListener-changedID"; // value will be the old ID
|
||||||
|
public static final String changedLocation = "GateChangeListener-changedLocation"; // value will the old location
|
||||||
|
public static final String changedExit = "GateChangeListener-changedExit"; // value will be the old exit
|
||||||
|
|
||||||
|
public void gateChangedHandler(final Gate g, final Map<String, Object>changeSet);
|
||||||
|
}
|
599
src/de/craftinc/gates/GatesManager.java
Normal file
599
src/de/craftinc/gates/GatesManager.java
Normal file
@ -0,0 +1,599 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import de.craftinc.gates.persistence.MigrationUtil;
|
||||||
|
import de.craftinc.gates.util.ConfigurationUtil;
|
||||||
|
import org.bukkit.Chunk;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.block.Block;
|
||||||
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
|
import de.craftinc.gates.util.SimpleChunk;
|
||||||
|
import de.craftinc.gates.util.SimpleLocation;
|
||||||
|
|
||||||
|
|
||||||
|
public class GatesManager
|
||||||
|
{
|
||||||
|
protected File gatesConfigFile;
|
||||||
|
protected FileConfiguration gatesConfig;
|
||||||
|
protected static final String gatesPath = "gates"; // path to gates inside the yaml file
|
||||||
|
protected static final String storageVersionPath = "version";
|
||||||
|
protected static final int storageVersion = 2;
|
||||||
|
|
||||||
|
protected int chunkRadius;
|
||||||
|
|
||||||
|
protected Map<String, Gate> gatesById;
|
||||||
|
protected Map<SimpleChunk, Set<Gate>> gatesByChunk;
|
||||||
|
protected Map<SimpleLocation, Gate> gatesByLocation;
|
||||||
|
protected Map<SimpleLocation, Gate> gatesByFrameLocation;
|
||||||
|
|
||||||
|
protected List<Gate> gates;
|
||||||
|
|
||||||
|
protected boolean storageFileIsInvalid = false;
|
||||||
|
|
||||||
|
protected Set<GateChangeListener> changeListeners = new HashSet<GateChangeListener>();
|
||||||
|
|
||||||
|
|
||||||
|
public void addGateChangeListener(GateChangeListener listener)
|
||||||
|
{
|
||||||
|
this.changeListeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void removeGateChangeListener(GateChangeListener listener)
|
||||||
|
{
|
||||||
|
this.changeListeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Gate getGateWithId(final String id)
|
||||||
|
{
|
||||||
|
return gatesById.get(id.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Set<Gate> getNearbyGates(final Chunk chunk)
|
||||||
|
{
|
||||||
|
SimpleChunk simpleChunk = new SimpleChunk(chunk);
|
||||||
|
return gatesByChunk.get(simpleChunk);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the closest gate.
|
||||||
|
* @param location The location at which to look for a gate.
|
||||||
|
* @return Might return null if there are no nearby gates.
|
||||||
|
*/
|
||||||
|
public Gate getNearestGate(final Location location)
|
||||||
|
{
|
||||||
|
Set<Gate> nearbyGates = getNearbyGates(location.getChunk());
|
||||||
|
|
||||||
|
if (nearbyGates == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
double minDist = Double.MAX_VALUE;
|
||||||
|
Gate nearestGate = null;
|
||||||
|
|
||||||
|
for (Gate g : nearbyGates) {
|
||||||
|
double dist = location.distance(g.getLocation());
|
||||||
|
|
||||||
|
if (dist < minDist) {
|
||||||
|
minDist = dist;
|
||||||
|
nearestGate = g;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nearestGate;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Gate getGateAtLocation(final Location location)
|
||||||
|
{
|
||||||
|
SimpleLocation simpleLocation = new SimpleLocation(location);
|
||||||
|
return gatesByLocation.get(simpleLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Gate getGateAtFrameLocation(final Location location)
|
||||||
|
{
|
||||||
|
SimpleLocation simpleLocation = new SimpleLocation(location);
|
||||||
|
return gatesByFrameLocation.get(simpleLocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void saveGatesToDisk()
|
||||||
|
{
|
||||||
|
if (storageFileIsInvalid) {
|
||||||
|
Plugin.log(Level.SEVERE, "ERROR: Not saving gates to disk. Storage file is invalid or corrupted!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
gatesConfig.set(gatesPath, gates);
|
||||||
|
gatesConfig.set(storageVersionPath, storageVersion);
|
||||||
|
|
||||||
|
try {
|
||||||
|
gatesConfig.save(gatesConfigFile);
|
||||||
|
Plugin.log("Saved gates to disk.");
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
Plugin.log(Level.SEVERE, "ERROR: Could not save gates to disk.");
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public boolean loadGatesFromDisk()
|
||||||
|
{
|
||||||
|
this.gatesConfigFile = new File(Plugin.getPlugin().getDataFolder(), "gates.yml");
|
||||||
|
|
||||||
|
if(!this.gatesConfigFile.exists()) {
|
||||||
|
try {
|
||||||
|
boolean isNew = this.gatesConfigFile.createNewFile();
|
||||||
|
|
||||||
|
if (isNew) {
|
||||||
|
Plugin.log(Level.FINEST, "Created gate storage file.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
this.storageFileIsInvalid = true;
|
||||||
|
Plugin.log(Level.SEVERE, "Cannot create gate storage file! No gates will be persisted.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.gatesConfig = new YamlConfiguration();
|
||||||
|
|
||||||
|
try {
|
||||||
|
this.gatesConfig.load(this.gatesConfigFile);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
this.storageFileIsInvalid = true;
|
||||||
|
Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (" + Arrays.toString(e.getStackTrace()) + ")");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.gates = (List<Gate>)gatesConfig.getList(gatesPath);
|
||||||
|
|
||||||
|
if (this.gates == null) {
|
||||||
|
this.gates = new ArrayList<Gate>();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Object o : this.gates) {
|
||||||
|
|
||||||
|
if (!(o instanceof Gate)) {
|
||||||
|
this.storageFileIsInvalid = true;
|
||||||
|
Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (Invalid gate class detected)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Gate g : this.gates) {
|
||||||
|
try {
|
||||||
|
g.validate();
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
try {
|
||||||
|
g.setOpen(false);
|
||||||
|
}
|
||||||
|
catch (Exception ignored) { }
|
||||||
|
|
||||||
|
Plugin.log(Level.FINER, "closed gate '" + g.getId() + "' reason: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fillGatesById();
|
||||||
|
fillGatesByChunk();
|
||||||
|
fillGatesByLocation();
|
||||||
|
fillGatesByFrameLocation();
|
||||||
|
|
||||||
|
Plugin.log("Loaded " + this.gates.size() + " gates.");
|
||||||
|
|
||||||
|
// migration
|
||||||
|
int fileStorageVersion = gatesConfig.getInt(storageVersionPath);
|
||||||
|
|
||||||
|
if (fileStorageVersion > storageVersion) {
|
||||||
|
this.storageFileIsInvalid = true;
|
||||||
|
Plugin.log(Level.SEVERE, "Unsupported storage version detected! Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fileStorageVersion < storageVersion && !this.gates.isEmpty()) {
|
||||||
|
Plugin.log("Outdated storage version detected. Performing data migration...");
|
||||||
|
boolean success = MigrationUtil.performMigration(fileStorageVersion, storageVersion, this.gates);
|
||||||
|
|
||||||
|
this.storageFileIsInvalid = !success;
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected int getChunkRadius()
|
||||||
|
{
|
||||||
|
if (this.chunkRadius == 0) {
|
||||||
|
this.chunkRadius = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confPlayerGateBlockUpdateRadiusKey);
|
||||||
|
this.chunkRadius = this.chunkRadius >> 4;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.chunkRadius;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void fillGatesById()
|
||||||
|
{
|
||||||
|
gatesById = new HashMap<String, Gate>((int)(gates.size() * 1.25));
|
||||||
|
|
||||||
|
for (Gate g : gates) {
|
||||||
|
this.addGateWithId(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void fillGatesByChunk()
|
||||||
|
{
|
||||||
|
HashSet<SimpleChunk> chunksUsedByGates = new HashSet<SimpleChunk>();
|
||||||
|
|
||||||
|
for (Gate g : gates) {
|
||||||
|
|
||||||
|
if (g.getLocation() != null) {
|
||||||
|
|
||||||
|
Chunk c = g.getLocation().getChunk();
|
||||||
|
|
||||||
|
int x = c.getX();
|
||||||
|
int z = c.getZ();
|
||||||
|
|
||||||
|
for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) {
|
||||||
|
|
||||||
|
for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) {
|
||||||
|
|
||||||
|
chunksUsedByGates.add(new SimpleChunk(i, j, c.getWorld()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gatesByChunk = new HashMap<SimpleChunk, Set<Gate>>((int)(chunksUsedByGates.size() * 1.25));
|
||||||
|
|
||||||
|
for (Gate g : gates) {
|
||||||
|
this.addGateByChunk(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void fillGatesByLocation()
|
||||||
|
{
|
||||||
|
Set<Location> gateBlocks = new HashSet<Location>();
|
||||||
|
|
||||||
|
for (Gate g : gates) {
|
||||||
|
|
||||||
|
for (Location l : g.getGateBlockLocations()) {
|
||||||
|
gateBlocks.add(l);
|
||||||
|
|
||||||
|
Location headLocation = new Location(l.getWorld(),
|
||||||
|
l.getX(),
|
||||||
|
l.getY()+1,
|
||||||
|
l.getZ());
|
||||||
|
|
||||||
|
gateBlocks.add(headLocation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gatesByLocation = new HashMap<SimpleLocation, Gate>((int)(gateBlocks.size()*1.25));
|
||||||
|
|
||||||
|
for (Gate g : gates) {
|
||||||
|
this.addGateByLocations(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void fillGatesByFrameLocation()
|
||||||
|
{
|
||||||
|
int numFrameBlocks = 0;
|
||||||
|
|
||||||
|
for (Gate g : gates) {
|
||||||
|
numFrameBlocks += g.gateFrameBlocks.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
gatesByFrameLocation = new HashMap<SimpleLocation, Gate>((int)(numFrameBlocks*1.25));
|
||||||
|
|
||||||
|
for (Gate g : gates) {
|
||||||
|
this.addGateByFrameLocations(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void removeGateById(final String id)
|
||||||
|
{
|
||||||
|
gatesById.remove(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void addGateWithId(final Gate g)
|
||||||
|
{
|
||||||
|
gatesById.put(g.getId(), g);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void removeGateByLocation(final Set<Location> gateBlocks)
|
||||||
|
{
|
||||||
|
if (gateBlocks != null) {
|
||||||
|
|
||||||
|
for (Location l : gateBlocks) {
|
||||||
|
|
||||||
|
SimpleLocation sl = new SimpleLocation(l);
|
||||||
|
gatesByLocation.remove(sl);
|
||||||
|
|
||||||
|
SimpleLocation headLocation = new SimpleLocation(l, true);
|
||||||
|
gatesByLocation.remove(headLocation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void removeGateByFrameLocation(final Set<Block> gateFrameBlocks)
|
||||||
|
{
|
||||||
|
if (gateFrameBlocks != null) {
|
||||||
|
|
||||||
|
for (Block block : gateFrameBlocks) {
|
||||||
|
SimpleLocation sl = new SimpleLocation(block.getLocation());
|
||||||
|
gatesByFrameLocation.remove(sl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void addGateByLocations(final Gate g)
|
||||||
|
{
|
||||||
|
for (Location l : g.getGateBlockLocations()) {
|
||||||
|
|
||||||
|
SimpleLocation sl = new SimpleLocation(l);
|
||||||
|
gatesByLocation.put(sl, g);
|
||||||
|
|
||||||
|
SimpleLocation headLocation = new SimpleLocation(l, true);
|
||||||
|
gatesByLocation.put(headLocation, g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void addGateByFrameLocations(final Gate g)
|
||||||
|
{
|
||||||
|
for (Block block : g.getGateFrameBlocks()) {
|
||||||
|
SimpleLocation sl = new SimpleLocation(block.getLocation());
|
||||||
|
gatesByFrameLocation.put(sl, g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void removeGateFromChunk(final Gate g, final Location l)
|
||||||
|
{
|
||||||
|
if (l != null) {
|
||||||
|
|
||||||
|
Chunk c = l.getChunk();
|
||||||
|
int x = c.getX();
|
||||||
|
int z = c.getZ();
|
||||||
|
|
||||||
|
for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) {
|
||||||
|
|
||||||
|
for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) {
|
||||||
|
|
||||||
|
SimpleChunk sc = new SimpleChunk(i, j, c.getWorld());
|
||||||
|
Set<Gate> gatesInChunk = gatesByChunk.get(sc);
|
||||||
|
|
||||||
|
if (gatesInChunk != null) {
|
||||||
|
gatesInChunk.remove(g);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void addGateByChunk(final Gate g)
|
||||||
|
{
|
||||||
|
Location gateLocation = g.getLocation();
|
||||||
|
|
||||||
|
if (gateLocation != null) {
|
||||||
|
|
||||||
|
Chunk c = g.getLocation().getChunk();
|
||||||
|
int x = c.getX();
|
||||||
|
int z = c.getZ();
|
||||||
|
|
||||||
|
for (int i = x-getChunkRadius(); i < x+getChunkRadius(); i++) {
|
||||||
|
|
||||||
|
for (int j = z-getChunkRadius(); j < z+getChunkRadius(); j++) {
|
||||||
|
|
||||||
|
SimpleChunk sc = new SimpleChunk(i, j, c.getWorld());
|
||||||
|
|
||||||
|
Set<Gate> gatesForC = gatesByChunk.get(sc);
|
||||||
|
|
||||||
|
if (gatesForC == null) {
|
||||||
|
gatesForC = new HashSet<Gate>(); // NOTE: not optimizing size here
|
||||||
|
gatesByChunk.put(sc, gatesForC);
|
||||||
|
}
|
||||||
|
|
||||||
|
gatesForC.add(g);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void storeInvalidGate(Map<String, Object> map)
|
||||||
|
{
|
||||||
|
File invalidGatesFile = new File(Plugin.getPlugin().getDataFolder(), "invalid_gates.yml");
|
||||||
|
Boolean invalidGatesFileExists = invalidGatesFile.exists();
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileWriter fileWriter = new FileWriter(invalidGatesFile, true);
|
||||||
|
|
||||||
|
if (!invalidGatesFileExists) {
|
||||||
|
fileWriter.write("gates:\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
fileWriter.write("- ==: ");
|
||||||
|
fileWriter.write(map.get("==").toString() + "\n");
|
||||||
|
map.remove("==");
|
||||||
|
|
||||||
|
fileWriter.write("\topen: false\n");
|
||||||
|
map.remove("open");
|
||||||
|
|
||||||
|
fileWriter.write("\tgateBlocks: []\n");
|
||||||
|
map.remove("gateBlocks");
|
||||||
|
|
||||||
|
|
||||||
|
for (String key : map.keySet()) {
|
||||||
|
Object value = map.get(key);
|
||||||
|
|
||||||
|
fileWriter.write("\t" + key + ": ");
|
||||||
|
|
||||||
|
if (value instanceof Map) {
|
||||||
|
fileWriter.write("\n");
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
Map<String, Object> valueMap = (Map<String, Object>)value;
|
||||||
|
|
||||||
|
for (String k : valueMap.keySet()) {
|
||||||
|
Object v = valueMap.get(k);
|
||||||
|
|
||||||
|
fileWriter.write("\t\t" + k + ": " + v.toString() + "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
fileWriter.write(value.toString() + "\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fileWriter.close();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + Arrays.toString(e.getStackTrace()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void handleGateIdChange(final Gate g, final String oldId)
|
||||||
|
{
|
||||||
|
this.removeGateById(oldId);
|
||||||
|
this.addGateWithId(g);
|
||||||
|
|
||||||
|
Map<String, Object> changeSet = new HashMap<String, Object>();
|
||||||
|
changeSet.put(GateChangeListener.changedID, oldId);
|
||||||
|
|
||||||
|
for (GateChangeListener l : this.changeListeners) {
|
||||||
|
l.gateChangedHandler(g, changeSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void handleGateLocationChange(final Gate g,
|
||||||
|
final Location oldLocation,
|
||||||
|
final Set<Location> oldGateBlockLocations,
|
||||||
|
final Set<Block> oldGateFrameBlocks)
|
||||||
|
{
|
||||||
|
this.removeGateFromChunk(g, oldLocation);
|
||||||
|
this.addGateByChunk(g);
|
||||||
|
|
||||||
|
this.removeGateByLocation(oldGateBlockLocations);
|
||||||
|
this.addGateByLocations(g);
|
||||||
|
|
||||||
|
this.removeGateByFrameLocation(oldGateFrameBlocks);
|
||||||
|
this.addGateByFrameLocations(g);
|
||||||
|
|
||||||
|
Map<String, Object> changeSet = new HashMap<String, Object>();
|
||||||
|
changeSet.put(GateChangeListener.changedLocation, oldLocation);
|
||||||
|
|
||||||
|
for (GateChangeListener l : this.changeListeners) {
|
||||||
|
l.gateChangedHandler(g, changeSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void handleGateExitChange(final Gate g, final Location oldExit)
|
||||||
|
{
|
||||||
|
// nothing to do
|
||||||
|
|
||||||
|
Map<String, Object> changeSet = new HashMap<String, Object>();
|
||||||
|
changeSet.put(GateChangeListener.changedExit, oldExit);
|
||||||
|
|
||||||
|
for (GateChangeListener l : this.changeListeners) {
|
||||||
|
l.gateChangedHandler(g, changeSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void handleNewGate(final Gate g)
|
||||||
|
{
|
||||||
|
this.gates.add(g);
|
||||||
|
|
||||||
|
this.addGateByChunk(g);
|
||||||
|
this.addGateByLocations(g);
|
||||||
|
this.addGateWithId(g);
|
||||||
|
this.addGateByFrameLocations(g);
|
||||||
|
|
||||||
|
|
||||||
|
Map<String, Object> changeSet = new HashMap<String, Object>();
|
||||||
|
changeSet.put(GateChangeListener.newGate, null);
|
||||||
|
|
||||||
|
for (GateChangeListener l : this.changeListeners) {
|
||||||
|
l.gateChangedHandler(g, changeSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void handleDeletion(final Gate g)
|
||||||
|
{
|
||||||
|
this.gates.remove(g);
|
||||||
|
|
||||||
|
this.removeGateById(g.getId());
|
||||||
|
this.removeGateFromChunk(g, g.getLocation());
|
||||||
|
this.removeGateByLocation(g.getGateBlockLocations());
|
||||||
|
this.removeGateByFrameLocation(g.getGateFrameBlocks());
|
||||||
|
|
||||||
|
Map<String, Object> changeSet = new HashMap<String, Object>();
|
||||||
|
changeSet.put(GateChangeListener.removedGate, null);
|
||||||
|
|
||||||
|
for (GateChangeListener l : this.changeListeners) {
|
||||||
|
l.gateChangedHandler(g, changeSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean gateExists(final String id)
|
||||||
|
{
|
||||||
|
return gatesById.containsKey(id.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public List<Gate> allGates ()
|
||||||
|
{
|
||||||
|
return gates;
|
||||||
|
}
|
||||||
|
}
|
@ -21,10 +21,8 @@ import java.util.*;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.GatesManager;
|
|
||||||
import de.craftinc.gates.listeners.*;
|
import de.craftinc.gates.listeners.*;
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.util.ConfigurationUtil;
|
import de.craftinc.gates.util.ConfigurationUtil;
|
||||||
import net.milkbowl.vault.permission.Permission;
|
import net.milkbowl.vault.permission.Permission;
|
||||||
|
|
||||||
@ -39,72 +37,92 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
import de.craftinc.gates.commands.*;
|
import de.craftinc.gates.commands.*;
|
||||||
import org.mcstats.Metrics;
|
import org.mcstats.Metrics;
|
||||||
|
|
||||||
public class Plugin extends JavaPlugin {
|
|
||||||
|
public class Plugin extends JavaPlugin
|
||||||
|
{
|
||||||
|
public static final String permissionInfo = "craftincgates.info";
|
||||||
|
public static final String permissionManage = "craftincgates.manage";
|
||||||
|
public static final String permissionUse = "craftincgates.use";
|
||||||
|
|
||||||
private static Plugin instance;
|
private static Plugin instance;
|
||||||
|
private static Permission permission;
|
||||||
|
|
||||||
private String baseCommand;
|
protected String baseCommand;
|
||||||
protected List<BaseCommand> commands = new ArrayList<>();
|
protected List<BaseCommand> commands = new ArrayList<BaseCommand>();
|
||||||
|
protected GatesManager gatesManager = new GatesManager();
|
||||||
|
|
||||||
private GatesManager gatesManager = new GatesManager();
|
protected PlayerMoveListener moveListener = new PlayerMoveListener();
|
||||||
|
protected PlayerTeleportListener teleportListener = new PlayerTeleportListener();
|
||||||
|
protected PlayerRespawnListener respawnListener = new PlayerRespawnListener();
|
||||||
|
protected PlayerChangedWorldListener worldChangeListener = new PlayerChangedWorldListener();
|
||||||
|
protected PlayerJoinListener joinListener = new PlayerJoinListener();
|
||||||
|
protected BlockBreakListener blockBreakListener = new BlockBreakListener();
|
||||||
|
|
||||||
private PermissionController permissionController = new PermissionController();
|
|
||||||
|
|
||||||
private PlayerMoveListener moveListener;
|
public Plugin()
|
||||||
private VehicleMoveListener vehicleListener;
|
{
|
||||||
private PlayerTeleportListener teleportListener = new PlayerTeleportListener();
|
|
||||||
private PlayerRespawnListener respawnListener = new PlayerRespawnListener();
|
|
||||||
private PlayerChangedWorldListener worldChangeListener = new PlayerChangedWorldListener();
|
|
||||||
private PlayerJoinListener joinListener = new PlayerJoinListener();
|
|
||||||
private BlockBreakListener blockBreakListener = new BlockBreakListener();
|
|
||||||
|
|
||||||
public Plugin() {
|
|
||||||
instance = this;
|
instance = this;
|
||||||
moveListener = new PlayerMoveListener(this);
|
|
||||||
vehicleListener = new VehicleMoveListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Plugin getPlugin() {
|
|
||||||
|
public static Plugin getPlugin()
|
||||||
|
{
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
public GatesManager getGatesManager() {
|
|
||||||
|
public GatesManager getGatesManager()
|
||||||
|
{
|
||||||
return gatesManager;
|
return gatesManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad()
|
||||||
|
{
|
||||||
ConfigurationSerialization.registerClass(Gate.class);
|
ConfigurationSerialization.registerClass(Gate.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupPermissions() {
|
|
||||||
|
protected void setupPermissions()
|
||||||
|
{
|
||||||
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
if (getServer().getPluginManager().getPlugin("Vault") == null) {
|
||||||
log("Not using setup permission provider provided by Vault.");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
|
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
|
||||||
|
|
||||||
if (rsp != null) {
|
if (rsp != null)
|
||||||
|
{
|
||||||
log("Using permission provider provided by Vault.");
|
log("Using permission provider provided by Vault.");
|
||||||
permissionController.setPermission(rsp.getProvider());
|
permission = rsp.getProvider();
|
||||||
} else {
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
log("Not using setup permission provider provided by Vault.");
|
log("Not using setup permission provider provided by Vault.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable()
|
||||||
|
{
|
||||||
|
// Save gates
|
||||||
gatesManager.saveGatesToDisk();
|
gatesManager.saveGatesToDisk();
|
||||||
|
|
||||||
log("Disabled");
|
log("Disabled");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable()
|
||||||
|
{
|
||||||
// Setup Metrics
|
// Setup Metrics
|
||||||
try {
|
try {
|
||||||
Metrics metrics = new Metrics(this);
|
Metrics metrics = new Metrics(this);
|
||||||
metrics.start();
|
metrics.start();
|
||||||
} catch (IOException e) {
|
}
|
||||||
|
catch (IOException e) {
|
||||||
log("Failed to start metrics!");
|
log("Failed to start metrics!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,14 +138,18 @@ public class Plugin extends JavaPlugin {
|
|||||||
commands.add(new CommandRemove());
|
commands.add(new CommandRemove());
|
||||||
commands.add(new CommandLocation());
|
commands.add(new CommandLocation());
|
||||||
commands.add(new CommandExit());
|
commands.add(new CommandExit());
|
||||||
commands.add(new CommandTriggerOpen());
|
commands.add(new CommandOpen());
|
||||||
commands.add(new CommandRename());
|
commands.add(new CommandRename());
|
||||||
|
commands.add(new CommandClose());
|
||||||
commands.add(new CommandList());
|
commands.add(new CommandList());
|
||||||
commands.add(new CommandInfo());
|
commands.add(new CommandInfo());
|
||||||
|
commands.add(new CommandHide());
|
||||||
|
commands.add(new CommandUnhide());
|
||||||
|
commands.add(new CommandExitOpen());
|
||||||
commands.add(new CommandNearby());
|
commands.add(new CommandNearby());
|
||||||
commands.add(new CommandTriggerVehicles());
|
commands.add(new CommandAllowRiding());
|
||||||
commands.add(new CommandMaterial());
|
commands.add(new CommandDenyRiding());
|
||||||
commands.add(new CommandTeleport());
|
|
||||||
|
|
||||||
// Register events
|
// Register events
|
||||||
this.registerEventListeners();
|
this.registerEventListeners();
|
||||||
@ -137,17 +159,16 @@ public class Plugin extends JavaPlugin {
|
|||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
log("Enabled");
|
log("Enabled");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
PluginManager pm = this.getServer().getPluginManager();
|
PluginManager pm = this.getServer().getPluginManager();
|
||||||
pm.disablePlugin(this);
|
pm.disablePlugin(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PermissionController getPermissionController() {
|
|
||||||
return permissionController;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void registerEventListeners() {
|
protected void registerEventListeners()
|
||||||
|
{
|
||||||
PluginManager pm = this.getServer().getPluginManager();
|
PluginManager pm = this.getServer().getPluginManager();
|
||||||
|
|
||||||
pm.registerEvents(this.moveListener, this);
|
pm.registerEvents(this.moveListener, this);
|
||||||
@ -155,18 +176,19 @@ public class Plugin extends JavaPlugin {
|
|||||||
pm.registerEvents(this.respawnListener, this);
|
pm.registerEvents(this.respawnListener, this);
|
||||||
pm.registerEvents(this.worldChangeListener, this);
|
pm.registerEvents(this.worldChangeListener, this);
|
||||||
pm.registerEvents(this.joinListener, this);
|
pm.registerEvents(this.joinListener, this);
|
||||||
pm.registerEvents(this.vehicleListener, this);
|
|
||||||
|
|
||||||
if (getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey)) {
|
if (getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey)) {
|
||||||
pm.registerEvents(this.blockBreakListener, this);
|
pm.registerEvents(this.blockBreakListener, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// Commands
|
// Commands
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
|
|
||||||
public String getBaseCommand() {
|
public String getBaseCommand()
|
||||||
|
{
|
||||||
if (this.baseCommand != null)
|
if (this.baseCommand != null)
|
||||||
return this.baseCommand;
|
return this.baseCommand;
|
||||||
|
|
||||||
@ -177,15 +199,20 @@ public class Plugin extends JavaPlugin {
|
|||||||
return this.baseCommand;
|
return this.baseCommand;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
|
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
|
||||||
List<String> parameters = new ArrayList<>(Arrays.asList(args));
|
{
|
||||||
|
List<String> parameters = new ArrayList<String>(Arrays.asList(args));
|
||||||
this.handleCommand(sender, parameters);
|
this.handleCommand(sender, parameters);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleCommand(CommandSender sender, List<String> parameters) {
|
|
||||||
if (parameters.size() == 0) {
|
public void handleCommand(CommandSender sender, List<String> parameters)
|
||||||
|
{
|
||||||
|
if (parameters.size() == 0)
|
||||||
|
{
|
||||||
this.commands.get(0).execute(sender, parameters);
|
this.commands.get(0).execute(sender, parameters);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -193,9 +220,11 @@ public class Plugin extends JavaPlugin {
|
|||||||
String commandName = parameters.get(0).toLowerCase();
|
String commandName = parameters.get(0).toLowerCase();
|
||||||
parameters.remove(0);
|
parameters.remove(0);
|
||||||
|
|
||||||
for (BaseCommand command : this.commands) {
|
for (BaseCommand fcommand : this.commands)
|
||||||
if (command.getAliases().contains(commandName)) {
|
{
|
||||||
command.execute(sender, parameters);
|
if (fcommand.getAliases().contains(commandName))
|
||||||
|
{
|
||||||
|
fcommand.execute(sender, parameters);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -204,14 +233,23 @@ public class Plugin extends JavaPlugin {
|
|||||||
ChatColor.GREEN + " Try " + "/" + getBaseCommand() + " help");
|
ChatColor.GREEN + " Try " + "/" + getBaseCommand() + " help");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Logging
|
* Logging
|
||||||
*/
|
*/
|
||||||
public static void log(String msg) {
|
public static void log(String msg)
|
||||||
|
{
|
||||||
log(Level.INFO, msg);
|
log(Level.INFO, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void log(Level level, String msg) {
|
|
||||||
Logger.getLogger("Minecraft").log(level, "[" + instance.getDescription().getFullName() + "] " + msg);
|
public static void log(Level level, String msg)
|
||||||
|
{
|
||||||
|
Logger.getLogger("Minecraft").log(level, "["+instance.getDescription().getFullName()+"] "+msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static Permission getPermission() {
|
||||||
|
return permission;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -19,49 +19,42 @@ package de.craftinc.gates.commands;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.util.ConfigurationUtil;
|
import de.craftinc.gates.util.ConfigurationUtil;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
import de.craftinc.gates.Gate;
|
||||||
import de.craftinc.gates.controllers.GatesManager;
|
import de.craftinc.gates.GatesManager;
|
||||||
import de.craftinc.gates.Plugin;
|
import de.craftinc.gates.Plugin;
|
||||||
import de.craftinc.gates.util.TextUtil;
|
import de.craftinc.gates.util.TextUtil;
|
||||||
|
|
||||||
public abstract class BaseCommand {
|
public abstract class BaseCommand
|
||||||
PermissionController permissionController;
|
{
|
||||||
GatesManager gatesManager;
|
protected List<String> aliases = new ArrayList<String>();
|
||||||
|
protected List<String> requiredParameters = new ArrayList<String>();
|
||||||
|
protected List<String> optionalParameters = new ArrayList<String>();
|
||||||
|
|
||||||
List<String> aliases = new ArrayList<>();
|
protected String helpDescription = "no description";
|
||||||
List<String> requiredParameters = new ArrayList<>();
|
|
||||||
List<String> optionalParameters = new ArrayList<>();
|
|
||||||
|
|
||||||
String helpDescription = "no description";
|
protected List<String> parameters;
|
||||||
|
protected CommandSender sender;
|
||||||
|
protected Player player;
|
||||||
|
protected Gate gate;
|
||||||
|
|
||||||
List<String> parameters;
|
protected boolean senderMustBePlayer = true;
|
||||||
CommandSender sender;
|
protected boolean hasGateParam = true;
|
||||||
Player player;
|
|
||||||
Gate gate;
|
|
||||||
|
|
||||||
boolean senderMustBePlayer = true;
|
protected String requiredPermission;
|
||||||
boolean hasGateParam = true;
|
protected boolean needsPermissionAtCurrentLocation;
|
||||||
|
|
||||||
String requiredPermission;
|
protected boolean shouldPersistToDisk;
|
||||||
boolean needsPermissionAtCurrentLocation;
|
|
||||||
|
|
||||||
boolean shouldPersistToDisk;
|
|
||||||
|
|
||||||
public List<String> getAliases() {
|
public List<String> getAliases() {
|
||||||
return aliases;
|
return aliases;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BaseCommand() {
|
|
||||||
gatesManager = Plugin.getPlugin().getGatesManager();
|
|
||||||
permissionController = Plugin.getPlugin().getPermissionController();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void execute(CommandSender sender, List<String> parameters) {
|
public void execute(CommandSender sender, List<String> parameters) {
|
||||||
this.sender = sender;
|
this.sender = sender;
|
||||||
@ -71,82 +64,35 @@ public abstract class BaseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sender instanceof Player) {
|
if (this.senderMustBePlayer) {
|
||||||
this.player = (Player)sender;
|
this.player = (Player)sender;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.perform();
|
this.perform();
|
||||||
|
|
||||||
if (this.shouldPersistToDisk && getSaveOnChanges()) {
|
if (this.shouldPersistToDisk && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confSaveOnChangesKey)) {
|
||||||
gatesManager.saveGatesToDisk();
|
Plugin.getPlugin().getGatesManager().saveGatesToDisk();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
abstract protected void perform();
|
abstract protected void perform();
|
||||||
|
|
||||||
void sendMessage(String message) {
|
|
||||||
|
protected void sendMessage(String message) {
|
||||||
sender.sendMessage(message);
|
sender.sendMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void sendMessage(List<String> messages) {
|
|
||||||
for (String message : messages) {
|
protected void sendMessage(List<String> messages) {
|
||||||
|
for(String message : messages) {
|
||||||
this.sendMessage(message);
|
this.sendMessage(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean setGateUsingParameter(String param) {
|
|
||||||
if (!gatesManager.gateExists(param)) {
|
|
||||||
return false;
|
|
||||||
} else {
|
|
||||||
gate = gatesManager.getGateWithId(param);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
protected boolean validateCall()
|
||||||
* This will return false if a gate is required for this command but this.gate == null.
|
{
|
||||||
*/
|
|
||||||
boolean hasPermission() {
|
|
||||||
if (needsPermissionAtCurrentLocation) {
|
|
||||||
return permissionController.hasPermission(sender, requiredPermission);
|
|
||||||
} else {
|
|
||||||
return permissionController.hasPermission(sender, gate, requiredPermission);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Help and usage description
|
|
||||||
*/
|
|
||||||
|
|
||||||
String getUsageTemplate(boolean withDescription) {
|
|
||||||
String ret = "";
|
|
||||||
|
|
||||||
ret += ChatColor.AQUA;
|
|
||||||
ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(getAliases(), ",") + " ";
|
|
||||||
|
|
||||||
List<String> parts = new ArrayList<>();
|
|
||||||
|
|
||||||
for (String requiredParameter : requiredParameters) {
|
|
||||||
parts.add("[" + requiredParameter + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (String optionalParameter : optionalParameters) {
|
|
||||||
parts.add("*[" + optionalParameter + "]");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
ret += ChatColor.DARK_AQUA;
|
|
||||||
ret += TextUtil.implode(parts, " ");
|
|
||||||
|
|
||||||
if (withDescription) {
|
|
||||||
ret += " ";
|
|
||||||
ret += ChatColor.YELLOW;
|
|
||||||
ret += helpDescription;
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean validateCall() {
|
|
||||||
boolean allParametersThere = parameters.size() >= requiredParameters.size();
|
boolean allParametersThere = parameters.size() >= requiredParameters.size();
|
||||||
boolean senderIsPlayer = this.sender instanceof Player;
|
boolean senderIsPlayer = this.sender instanceof Player;
|
||||||
boolean hasGateParameter = false;
|
boolean hasGateParameter = false;
|
||||||
@ -159,25 +105,22 @@ public abstract class BaseCommand {
|
|||||||
boolean valid;
|
boolean valid;
|
||||||
|
|
||||||
if (this.senderMustBePlayer && !senderIsPlayer) {
|
if (this.senderMustBePlayer && !senderIsPlayer) {
|
||||||
sendMessage(ChatColor.RED + "This command can only be used by in-game players.");
|
sendMessage(ChatColor.RED + "This command can only be used by ingame players.");
|
||||||
valid = false;
|
valid = false;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
if (!allParametersThere) {
|
if (!allParametersThere) {
|
||||||
sendMessage(ChatColor.RED + "Some parameters are missing! " +
|
sendMessage(ChatColor.RED + "Some parameters are missing! " + ChatColor.AQUA + "Usage: " + this.getUsageTemplate(true));
|
||||||
ChatColor.AQUA + "Usage: " +
|
|
||||||
getUsageTemplate(false)
|
|
||||||
);
|
|
||||||
valid = false;
|
valid = false;
|
||||||
} else if ((!senderHasPermission && this.hasGateParam) ||
|
}
|
||||||
|
else if ((!senderHasPermission && this.hasGateParam) ||
|
||||||
(!senderHasPermission) ||
|
(!senderHasPermission) ||
|
||||||
(this.hasGateParam && !hasGateParameter)) {
|
(this.hasGateParam && !hasGateParameter)) {
|
||||||
|
|
||||||
sendMessage(ChatColor.RED +
|
sendMessage(ChatColor.RED + "You either provided a invalid gate or do not have permission to " + this.helpDescription.toLowerCase());
|
||||||
"You either provided a invalid gate or do not have permission to " +
|
|
||||||
this.helpDescription.toLowerCase()
|
|
||||||
);
|
|
||||||
valid = false;
|
valid = false;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
valid = true;
|
valid = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -185,8 +128,125 @@ public abstract class BaseCommand {
|
|||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean getSaveOnChanges() {
|
|
||||||
FileConfiguration config = Plugin.getPlugin().getConfig();
|
protected boolean setGateUsingParameter(String param)
|
||||||
return config.getBoolean(ConfigurationUtil.confSaveOnChangesKey);
|
{
|
||||||
|
GatesManager gateManager = Plugin.getPlugin().getGatesManager();
|
||||||
|
|
||||||
|
if (!gateManager.gateExists(param)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
gate = gateManager.getGateWithId(param);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This will return false if a gate is required for this command but this.gate == null.
|
||||||
|
*/
|
||||||
|
protected boolean hasPermission()
|
||||||
|
{
|
||||||
|
if (Plugin.getPermission() == null) { // fallback - use the standard bukkit permission system
|
||||||
|
return this.sender.hasPermission(this.requiredPermission);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(this.sender instanceof Player)) {
|
||||||
|
// sender is no player - there is no information about the senders locations
|
||||||
|
return Plugin.getPermission().has(this.sender, this.requiredPermission);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Player p = (Player) this.sender;
|
||||||
|
boolean hasPermission = false;
|
||||||
|
|
||||||
|
if (this.requiredPermission.equals(Plugin.permissionInfo)) {
|
||||||
|
|
||||||
|
if (this.hasGateParam) {
|
||||||
|
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (this.requiredPermission.equals(Plugin.permissionUse) ) {
|
||||||
|
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
|
||||||
|
}
|
||||||
|
else if (this.requiredPermission.equals(Plugin.permissionManage)) {
|
||||||
|
|
||||||
|
if (this.needsPermissionAtCurrentLocation && this.hasGateParam) {
|
||||||
|
boolean hasPersmissionAtCurrentLocation = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
hasPermission = hasPersmissionAtCurrentLocation && this.hasPermissionAtGateLocationAndExit(p);
|
||||||
|
}
|
||||||
|
else if (this.needsPermissionAtCurrentLocation) {
|
||||||
|
hasPermission = Plugin.getPermission().has(p.getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
hasPermission = this.hasPermissionAtGateLocationAndExit(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasPermission;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected boolean hasPermissionAtGateLocationAndExit(Player p)
|
||||||
|
{
|
||||||
|
if (this.gate == null || p == null) { // make sure we don't run into a nullpointer exception
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean permAtLocation = this.gate.getLocation() == null || Plugin.getPermission().has(this.gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
boolean permAtExit = this.gate.getExit() == null || Plugin.getPermission().has(this.gate.getExit().getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
|
||||||
|
return permAtLocation & permAtExit;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// Help and usage description
|
||||||
|
// -------------------------------------------- //
|
||||||
|
protected String getUsageTemplate(boolean withColor, boolean withDescription)
|
||||||
|
{
|
||||||
|
String ret = "";
|
||||||
|
|
||||||
|
if (withColor) {
|
||||||
|
ret += ChatColor.AQUA;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret += "/" + Plugin.getPlugin().getBaseCommand() + " " + TextUtil.implode(this.getAliases(), ",")+" ";
|
||||||
|
|
||||||
|
List<String> parts = new ArrayList<String>();
|
||||||
|
|
||||||
|
for (String requiredParameter : this.requiredParameters) {
|
||||||
|
parts.add("["+requiredParameter+"]");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String optionalParameter : this.optionalParameters) {
|
||||||
|
parts.add("*["+optionalParameter+"]");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (withColor) {
|
||||||
|
ret += ChatColor.DARK_AQUA;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret += TextUtil.implode(parts, " ");
|
||||||
|
|
||||||
|
if (withDescription) {
|
||||||
|
ret += " ";
|
||||||
|
|
||||||
|
if (withColor) {
|
||||||
|
ret += ChatColor.YELLOW;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret += this.helpDescription;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String getUsageTemplate(boolean withColor)
|
||||||
|
{
|
||||||
|
return getUsageTemplate(withColor, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,21 +21,27 @@ import org.bukkit.Material;
|
|||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
|
|
||||||
abstract class BaseLocationCommand extends BaseCommand {
|
public abstract class BaseLocationCommand extends BaseCommand
|
||||||
|
{
|
||||||
Location getValidPlayerLocation() {
|
protected Location getValidPlayerLocation()
|
||||||
|
{
|
||||||
// The player might stand in a half block or a sign or whatever
|
// The player might stand in a half block or a sign or whatever
|
||||||
// Therefore we load some extra locations and blocks
|
// Therefore we load some extra locations and blocks
|
||||||
Location location = player.getLocation().clone();
|
Block playerBlock = player.getLocation().getBlock();
|
||||||
Block playerBlock = location.getBlock();
|
|
||||||
Block upBlock = playerBlock.getRelative(BlockFace.UP);
|
Block upBlock = playerBlock.getRelative(BlockFace.UP);
|
||||||
|
|
||||||
if (playerBlock.getType() == Material.AIR) {
|
if (playerBlock.getType() == Material.AIR) {
|
||||||
return location;
|
return player.getLocation();
|
||||||
} else if (upBlock.getType() == Material.AIR) {
|
}
|
||||||
return location.add(0, 1, 0);
|
else if (upBlock.getType() == Material.AIR) {
|
||||||
} else {
|
return new Location(player.getLocation().getWorld(),
|
||||||
|
player.getLocation().getX(),
|
||||||
|
player.getLocation().getY() + 1,
|
||||||
|
player.getLocation().getZ(),
|
||||||
|
player.getLocation().getYaw(),
|
||||||
|
player.getLocation().getPitch());
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -16,32 +16,33 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
public class CommandTriggerVehicles extends BaseCommand {
|
public class CommandAllowRiding extends BaseCommand
|
||||||
|
{
|
||||||
public CommandTriggerVehicles() {
|
public CommandAllowRiding()
|
||||||
aliases.add("vehicles");
|
{
|
||||||
aliases.add("v");
|
aliases.add("allowRiding");
|
||||||
|
aliases.add("ar");
|
||||||
|
|
||||||
requiredParameters.add("id");
|
requiredParameters.add("id");
|
||||||
|
|
||||||
helpDescription = "Allow/deny players to travel while riding.";
|
helpDescription = "Allow players to travel while riding.";
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = false;
|
needsPermissionAtCurrentLocation = false;
|
||||||
shouldPersistToDisk = true;
|
shouldPersistToDisk = true;
|
||||||
|
|
||||||
senderMustBePlayer = false;
|
senderMustBePlayer = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void perform() {
|
protected void perform()
|
||||||
gate.setAllowsVehicles(gate.getAllowsVehicles());
|
{
|
||||||
|
gate.setAllowsVehicles(true);
|
||||||
if (gate.getAllowsVehicles()) {
|
|
||||||
sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate.");
|
sendMessage(ChatColor.GREEN + "Traveling while riding is now enabled for this gate.");
|
||||||
} else {
|
|
||||||
sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
64
src/de/craftinc/gates/commands/CommandClose.java
Normal file
64
src/de/craftinc/gates/commands/CommandClose.java
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
|
|
||||||
|
public class CommandClose extends BaseCommand
|
||||||
|
{
|
||||||
|
public CommandClose()
|
||||||
|
{
|
||||||
|
aliases.add("close");
|
||||||
|
aliases.add("c");
|
||||||
|
|
||||||
|
requiredParameters.add("id");
|
||||||
|
|
||||||
|
helpDescription = "Closes a gate to prevent players from using it.";
|
||||||
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
|
needsPermissionAtCurrentLocation = false;
|
||||||
|
shouldPersistToDisk = true;
|
||||||
|
|
||||||
|
senderMustBePlayer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
gate.setOpen(false);
|
||||||
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
|
sendMessage(ChatColor.GREEN + "The gate was closed.");
|
||||||
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
sendMessage(ChatColor.RED + "Opening the gate failed! See server log for more information");
|
||||||
|
Plugin.log(Level.WARNING, e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
47
src/de/craftinc/gates/commands/CommandDenyRiding.java
Normal file
47
src/de/craftinc/gates/commands/CommandDenyRiding.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
|
public class CommandDenyRiding extends BaseCommand
|
||||||
|
{
|
||||||
|
public CommandDenyRiding()
|
||||||
|
{
|
||||||
|
aliases.add("denyRiding");
|
||||||
|
aliases.add("dr");
|
||||||
|
|
||||||
|
requiredParameters.add("id");
|
||||||
|
|
||||||
|
helpDescription = "Deny players to travel while riding.";
|
||||||
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
|
needsPermissionAtCurrentLocation = false;
|
||||||
|
shouldPersistToDisk = true;
|
||||||
|
|
||||||
|
senderMustBePlayer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void perform()
|
||||||
|
{
|
||||||
|
gate.setAllowsVehicles(false);
|
||||||
|
sendMessage(ChatColor.GREEN + "Traveling while riding is now disabled for this gate.");
|
||||||
|
}
|
||||||
|
}
|
@ -18,33 +18,42 @@ package de.craftinc.gates.commands;
|
|||||||
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
import de.craftinc.gates.Plugin;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
public class CommandExit extends BaseCommand {
|
|
||||||
|
|
||||||
public CommandExit() {
|
public class CommandExit extends BaseCommand
|
||||||
|
{
|
||||||
|
public CommandExit()
|
||||||
|
{
|
||||||
aliases.add("exit");
|
aliases.add("exit");
|
||||||
aliases.add("e");
|
aliases.add("e");
|
||||||
|
|
||||||
requiredParameters.add("id");
|
requiredParameters.add("id");
|
||||||
|
|
||||||
helpDescription = "Change exit of location.";
|
helpDescription = "Change exit of location.";
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = true;
|
needsPermissionAtCurrentLocation = true;
|
||||||
shouldPersistToDisk = true;
|
shouldPersistToDisk = true;
|
||||||
senderMustBePlayer = true;
|
senderMustBePlayer = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
try {
|
public void perform()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
Location oldExit = gate.getExit();
|
Location oldExit = gate.getExit();
|
||||||
gate.setExit(player.getLocation());
|
gate.setExit(player.getLocation());
|
||||||
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
|
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
|
||||||
gatesManager.handleGateExitChange(gate, oldExit);
|
Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit);
|
||||||
} catch (Exception e) {
|
}
|
||||||
|
catch (Exception e) {
|
||||||
GateBlockChangeSender.updateGateBlocks(gate);
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information");
|
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! See server log for more information");
|
||||||
Plugin.log(Level.WARNING, e.getMessage());
|
Plugin.log(Level.WARNING, e.getMessage());
|
||||||
@ -52,3 +61,4 @@ public class CommandExit extends BaseCommand {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
83
src/de/craftinc/gates/commands/CommandExitOpen.java
Normal file
83
src/de/craftinc/gates/commands/CommandExitOpen.java
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
public class CommandExitOpen extends BaseCommand
|
||||||
|
{
|
||||||
|
public CommandExitOpen()
|
||||||
|
{
|
||||||
|
aliases.add("exitopen");
|
||||||
|
aliases.add("eo");
|
||||||
|
|
||||||
|
requiredParameters.add("id");
|
||||||
|
|
||||||
|
helpDescription = "Change exit of location and open that gate afterwards.";
|
||||||
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
|
needsPermissionAtCurrentLocation = true;
|
||||||
|
shouldPersistToDisk = true;
|
||||||
|
senderMustBePlayer = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Location oldExit = gate.getExit();
|
||||||
|
gate.setExit(player.getLocation());
|
||||||
|
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
|
||||||
|
Plugin.getPlugin().getGatesManager().handleGateExitChange(gate, oldExit);
|
||||||
|
|
||||||
|
try {
|
||||||
|
boolean needsGateManagerUpdate = false;
|
||||||
|
|
||||||
|
if (gate.getGateBlockLocations().isEmpty()) {
|
||||||
|
needsGateManagerUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
gate.setOpen(true);
|
||||||
|
|
||||||
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
|
|
||||||
|
if (needsGateManagerUpdate) {
|
||||||
|
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(ChatColor.GREEN + "The gate was opened.");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
sendMessage(ChatColor.RED + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
|
sendMessage(ChatColor.RED + "Setting the exit for the gate failed! This gate is now closed! (See server log for more information.)");
|
||||||
|
Plugin.log(Level.WARNING, e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -16,53 +16,96 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
import de.craftinc.gates.Plugin;
|
||||||
import de.craftinc.gates.util.TextUtil;
|
import de.craftinc.gates.util.TextUtil;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CommandHelp extends BaseCommand {
|
public class CommandHelp extends BaseCommand
|
||||||
|
{
|
||||||
|
public static List<List<String>> helpPages;
|
||||||
|
|
||||||
private static List<String> help;
|
static
|
||||||
|
{
|
||||||
|
// sort the usage strings
|
||||||
|
List<String> allUsageStrings = new ArrayList<String>();
|
||||||
|
|
||||||
public CommandHelp() {
|
allUsageStrings.add( new CommandHelp().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandNew().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandRemove().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandLocation().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandExit().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandOpen().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandRename().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandClose().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandList().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandInfo().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandHide().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandUnhide().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandExitOpen().getUsageTemplate(true, true) );
|
||||||
|
allUsageStrings.add( new CommandNearby().getUsageTemplate(true, true) );
|
||||||
|
|
||||||
|
Collections.sort(allUsageStrings);
|
||||||
|
|
||||||
|
|
||||||
|
// put 5 commands on one page
|
||||||
|
helpPages = new ArrayList<List<String>>();
|
||||||
|
|
||||||
|
while (!allUsageStrings.isEmpty()) {
|
||||||
|
int toIndex = allUsageStrings.size() >= 6 ? 5 : allUsageStrings.size();
|
||||||
|
List<String> currentHelpPage = new ArrayList<String>(allUsageStrings.subList(0, toIndex));
|
||||||
|
helpPages.add(currentHelpPage);
|
||||||
|
|
||||||
|
allUsageStrings.removeAll(currentHelpPage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public CommandHelp()
|
||||||
|
{
|
||||||
aliases.add("help");
|
aliases.add("help");
|
||||||
aliases.add("?");
|
aliases.add("?");
|
||||||
|
|
||||||
|
optionalParameters.add("page");
|
||||||
helpDescription = "prints this help page";
|
helpDescription = "prints this help page";
|
||||||
requiredPermission = PermissionController.permissionInfo;
|
|
||||||
|
requiredPermission = Plugin.permissionInfo;
|
||||||
|
|
||||||
hasGateParam = false;
|
hasGateParam = false;
|
||||||
needsPermissionAtCurrentLocation = false;
|
needsPermissionAtCurrentLocation = false;
|
||||||
shouldPersistToDisk = false;
|
shouldPersistToDisk = false;
|
||||||
senderMustBePlayer = false;
|
senderMustBePlayer = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
sendMessage(TextUtil.titleSize("Craft Inc. Gates Help"));
|
public void perform()
|
||||||
sendMessage(getHelp());
|
{
|
||||||
|
int page;
|
||||||
|
|
||||||
|
if (parameters.size() > 0) {
|
||||||
|
try {
|
||||||
|
page = Integer.parseInt(parameters.get(0));
|
||||||
|
}
|
||||||
|
catch (NumberFormatException e) {
|
||||||
|
// wasn't an integer
|
||||||
|
page = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
page = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<String> getHelp() {
|
sendMessage(TextUtil.titleize("Craft Inc. Gates Help (" + page + "/" + helpPages.size() + ")"));
|
||||||
if (help == null) {
|
|
||||||
help = Arrays.asList(
|
page -= 1;
|
||||||
new CommandHelp().getUsageTemplate(true),
|
if (page < 0 || page >= helpPages.size()) {
|
||||||
new CommandNew().getUsageTemplate(true),
|
sendMessage("This page does not exist");
|
||||||
new CommandRemove().getUsageTemplate(true),
|
return;
|
||||||
new CommandLocation().getUsageTemplate(true),
|
|
||||||
new CommandExit().getUsageTemplate(true),
|
|
||||||
new CommandTriggerOpen().getUsageTemplate(true),
|
|
||||||
new CommandRename().getUsageTemplate(true),
|
|
||||||
new CommandList().getUsageTemplate(true),
|
|
||||||
new CommandInfo().getUsageTemplate(true),
|
|
||||||
new CommandNearby().getUsageTemplate(true),
|
|
||||||
new CommandTriggerVehicles().getUsageTemplate(true),
|
|
||||||
new CommandTeleport().getUsageTemplate(true),
|
|
||||||
new CommandMaterial().getUsageTemplate(true)
|
|
||||||
);
|
|
||||||
Collections.sort(help);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return help;
|
sendMessage(helpPages.get(page));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
59
src/de/craftinc/gates/commands/CommandHide.java
Normal file
59
src/de/craftinc/gates/commands/CommandHide.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
|
|
||||||
|
public class CommandHide extends BaseCommand
|
||||||
|
{
|
||||||
|
public CommandHide()
|
||||||
|
{
|
||||||
|
aliases.add("hide");
|
||||||
|
aliases.add("h");
|
||||||
|
|
||||||
|
requiredParameters.add("id");
|
||||||
|
|
||||||
|
helpDescription = "Makes a gate NOT consist of gate blocks while open.";
|
||||||
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
|
needsPermissionAtCurrentLocation = false;
|
||||||
|
shouldPersistToDisk = true;
|
||||||
|
senderMustBePlayer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
gate.setHidden(true);
|
||||||
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
|
sendMessage(ChatColor.GREEN + "The gate '" + gate.getId() + "' is now hidden.");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
sendMessage(ChatColor.RED + "Hiding the gate failed! See server log for more information");
|
||||||
|
Plugin.log(Level.WARNING, e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -16,8 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
@ -25,23 +24,29 @@ import de.craftinc.gates.Plugin;
|
|||||||
import de.craftinc.gates.util.TextUtil;
|
import de.craftinc.gates.util.TextUtil;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
public class CommandInfo extends BaseCommand {
|
public class CommandInfo extends BaseCommand
|
||||||
|
{
|
||||||
public CommandInfo() {
|
public CommandInfo()
|
||||||
|
{
|
||||||
aliases.add("info");
|
aliases.add("info");
|
||||||
aliases.add("i");
|
aliases.add("i");
|
||||||
|
|
||||||
optionalParameters.add("id");
|
optionalParameters.add("id");
|
||||||
|
|
||||||
helpDescription = "Print detailed information about a certain or the closest gate.";
|
helpDescription = "Print detailed information about a certain or the closest gate.";
|
||||||
requiredPermission = PermissionController.permissionInfo;
|
|
||||||
|
requiredPermission = Plugin.permissionInfo;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = false;
|
needsPermissionAtCurrentLocation = false;
|
||||||
shouldPersistToDisk = false;
|
shouldPersistToDisk = false;
|
||||||
senderMustBePlayer = false;
|
senderMustBePlayer = false;
|
||||||
hasGateParam = false;
|
hasGateParam = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
if (this.parameters.size() > 0) {
|
if (this.parameters.size() > 0) {
|
||||||
|
|
||||||
if (!this.setGateUsingParameter(this.parameters.get(0))) {
|
if (!this.setGateUsingParameter(this.parameters.get(0))) {
|
||||||
@ -49,8 +54,9 @@ public class CommandInfo extends BaseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(TextUtil.titleSize("Information about: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
|
sendMessage(TextUtil.titleize("Information about: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
boolean senderIsPlayer = this.sender instanceof Player;
|
boolean senderIsPlayer = this.sender instanceof Player;
|
||||||
|
|
||||||
if (!senderIsPlayer) {
|
if (!senderIsPlayer) {
|
||||||
@ -58,8 +64,8 @@ public class CommandInfo extends BaseCommand {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Player p = (Player) this.sender;
|
Player p = (Player)this.sender;
|
||||||
this.gate = gatesManager.getNearestGate(p.getLocation());
|
this.gate = Plugin.getPlugin().getGatesManager().getNearestGate(p.getLocation());
|
||||||
|
|
||||||
if (!this.hasPermission() || this.gate == null) {
|
if (!this.hasPermission() || this.gate == null) {
|
||||||
sendMessage(ChatColor.RED + "There is either no gate nearby or you do not have permission to " + this.helpDescription.toLowerCase());
|
sendMessage(ChatColor.RED + "There is either no gate nearby or you do not have permission to " + this.helpDescription.toLowerCase());
|
||||||
@ -68,30 +74,33 @@ public class CommandInfo extends BaseCommand {
|
|||||||
|
|
||||||
Plugin.log(this.gate.toString());
|
Plugin.log(this.gate.toString());
|
||||||
|
|
||||||
sendMessage(TextUtil.titleSize("Information about closest gate: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
|
sendMessage(TextUtil.titleize("Information about closest gate: '" + ChatColor.WHITE + gate.getId() + ChatColor.YELLOW + "'"));
|
||||||
}
|
}
|
||||||
|
|
||||||
String openMessage = ChatColor.DARK_AQUA + "This gate is";
|
String openHiddenMessage = ChatColor.DARK_AQUA + "This gate is";
|
||||||
|
|
||||||
if (gate.isOpen())
|
if (gate.isOpen())
|
||||||
openMessage += ChatColor.AQUA + " open";
|
openHiddenMessage += ChatColor.AQUA + " open";
|
||||||
else
|
else
|
||||||
openMessage += ChatColor.AQUA + " closed";
|
openHiddenMessage += ChatColor.AQUA + " closed";
|
||||||
|
|
||||||
openMessage += ".\n";
|
if (gate.isHidden())
|
||||||
|
openHiddenMessage += ChatColor.DARK_AQUA +" and" + ChatColor.AQUA + " hidden";
|
||||||
|
|
||||||
sendMessage(openMessage);
|
openHiddenMessage += ".\n";
|
||||||
|
|
||||||
|
sendMessage(openHiddenMessage);
|
||||||
|
|
||||||
if (gate.getLocation() != null)
|
if (gate.getLocation() != null)
|
||||||
sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int) gate.getLocation().getX() +
|
sendMessage(ChatColor.DARK_AQUA + "location: " + ChatColor.AQUA + "( " + (int)gate.getLocation().getX() +
|
||||||
" | " + (int) gate.getLocation().getY() + " | " + (int) gate.getLocation().getZ() + " ) in " +
|
" | " + (int)gate.getLocation().getY() + " | " + (int)gate.getLocation().getZ() + " ) in " +
|
||||||
gate.getLocation().getWorld().getName());
|
gate.getLocation().getWorld().getName());
|
||||||
else
|
else
|
||||||
sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no location");
|
sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no location");
|
||||||
|
|
||||||
if (gate.getExit() != null)
|
if (gate.getExit() != null)
|
||||||
sendMessage(ChatColor.DARK_AQUA + "exit: " + ChatColor.AQUA + "( " + (int) gate.getExit().getX() + " | "
|
sendMessage(ChatColor.DARK_AQUA + "exit: " + ChatColor.AQUA + "( " + (int)gate.getExit().getX() + " | "
|
||||||
+ (int) gate.getExit().getY() + " | " + (int) gate.getExit().getZ() + " ) in " +
|
+ (int)gate.getExit().getY() + " | " + (int)gate.getExit().getZ() + " ) in " +
|
||||||
gate.getExit().getWorld().getName());
|
gate.getExit().getWorld().getName());
|
||||||
else
|
else
|
||||||
sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no exit");
|
sendMessage(ChatColor.DARK_AQUA + "NOTE: this gate has no exit");
|
||||||
@ -100,14 +109,9 @@ public class CommandInfo extends BaseCommand {
|
|||||||
if (gate.getAllowsVehicles())
|
if (gate.getAllowsVehicles())
|
||||||
sendMessage(ChatColor.DARK_AQUA + "You can ride through this gate.");
|
sendMessage(ChatColor.DARK_AQUA + "You can ride through this gate.");
|
||||||
|
|
||||||
sendMessage(ChatColor.DARK_AQUA + "This gate is made of "
|
|
||||||
+ ChatColor.AQUA + gate.getMaterial() + ChatColor.DARK_AQUA + ".");
|
|
||||||
|
|
||||||
if (this.sender instanceof Player) {
|
if (this.sender instanceof Player) {
|
||||||
HashSet<Gate> set = new HashSet<>();
|
GateBlockChangeSender.temporaryHighlightGateFrame((Player)this.sender, this.gate);
|
||||||
set.add(this.gate);
|
|
||||||
|
|
||||||
GateBlockChangeSender.temporaryHighlightGatesFrames((Player)this.sender, set);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,23 +21,27 @@ import java.util.Collection;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
import de.craftinc.gates.Gate;
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
import de.craftinc.gates.util.TextUtil;
|
import de.craftinc.gates.util.TextUtil;
|
||||||
|
|
||||||
public class CommandList extends BaseCommand {
|
|
||||||
|
|
||||||
private static final int linesPerPage = 15;
|
public class CommandList extends BaseCommand
|
||||||
|
{
|
||||||
/* this is actually not true. the font used by Minecraft is not
|
protected static final int linesPerPage = 10;
|
||||||
monospaced. but there seems to be no (easy) way to calculate
|
protected static final int charactersPerLine = 52; /* this is actually no true. the
|
||||||
|
font used by minecraft is not
|
||||||
|
monospace. but I don't think
|
||||||
|
there is a (easy) way for a
|
||||||
|
bukkit plugin to calculate
|
||||||
the drawing-size of a string.
|
the drawing-size of a string.
|
||||||
*/
|
*/
|
||||||
private static final int charactersPerLine = 52;
|
|
||||||
|
|
||||||
public CommandList() {
|
public CommandList()
|
||||||
|
{
|
||||||
aliases.add("list");
|
aliases.add("list");
|
||||||
aliases.add("ls");
|
aliases.add("ls");
|
||||||
|
|
||||||
@ -47,48 +51,32 @@ public class CommandList extends BaseCommand {
|
|||||||
|
|
||||||
helpDescription = "lists all availible gates.";
|
helpDescription = "lists all availible gates.";
|
||||||
|
|
||||||
requiredPermission = PermissionController.permissionInfo;
|
requiredPermission = Plugin.permissionInfo;
|
||||||
shouldPersistToDisk = false;
|
shouldPersistToDisk = false;
|
||||||
senderMustBePlayer = false;
|
senderMustBePlayer = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
int page = this.getPageParameter();
|
|
||||||
List<String> allPages = this.pagedGateIds();
|
|
||||||
|
|
||||||
if (allPages == null) { // no gates exist
|
protected static List<String> linesOfGateIds(List<String> gates)
|
||||||
sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET +
|
{
|
||||||
"(Note that you might not be allowed to get information about certain gates)");
|
List<String> lines = new ArrayList<String>();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (page > allPages.size() || page < 1) {
|
|
||||||
sendMessage(ChatColor.RED + "The requested page is not available");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String message = TextUtil.titleSize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n";
|
|
||||||
message += allPages.get(page - 1);
|
|
||||||
|
|
||||||
sendMessage(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static List<String> linesOfGateIds(List<String> gates) {
|
|
||||||
List<String> lines = new ArrayList<>();
|
|
||||||
|
|
||||||
int index = 0;
|
int index = 0;
|
||||||
List<String> gateIdsForCurrentLine = new ArrayList<>();
|
List<String> gateIdsForCurrentLine = new ArrayList<String>();
|
||||||
int numCharactersInCurrentLine = 0;
|
int numCharactersInCurrentLine = 0;
|
||||||
|
|
||||||
|
|
||||||
while (index < gates.size()) {
|
while (index < gates.size()) {
|
||||||
String gateId = gates.get(index);
|
String gateId = gates.get(index);
|
||||||
int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace
|
int gateIdLength = gateId.length() + 2; // actual length + comma + whitespace
|
||||||
|
|
||||||
if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) { // special case: very long gate id
|
// special case: very long gate id
|
||||||
gateIdsForCurrentLine = new ArrayList<>();
|
if (gateIdLength > charactersPerLine && numCharactersInCurrentLine == 0) {
|
||||||
|
gateIdsForCurrentLine = new ArrayList<String>();
|
||||||
numCharactersInCurrentLine = 0;
|
numCharactersInCurrentLine = 0;
|
||||||
|
|
||||||
while ((gateId.length() + 2) > charactersPerLine) {
|
while ((gateId.length() + 2) > charactersPerLine) {
|
||||||
|
|
||||||
int cutPos = charactersPerLine;
|
int cutPos = charactersPerLine;
|
||||||
|
|
||||||
// is the id too long to add comma and whitespace but not longer than the line?
|
// is the id too long to add comma and whitespace but not longer than the line?
|
||||||
@ -98,21 +86,28 @@ public class CommandList extends BaseCommand {
|
|||||||
|
|
||||||
lines.add(gateId.substring(0, cutPos));
|
lines.add(gateId.substring(0, cutPos));
|
||||||
gateId = gateId.substring(cutPos, gateId.length());
|
gateId = gateId.substring(cutPos, gateId.length());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
gateIdsForCurrentLine.add(gateId);
|
gateIdsForCurrentLine.add(gateId);
|
||||||
|
|
||||||
numCharactersInCurrentLine += gateId.length();
|
numCharactersInCurrentLine += gateId.length();
|
||||||
index++;
|
index++;
|
||||||
} else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) { // gate fits into current line
|
}
|
||||||
|
|
||||||
|
// gate fits into current line
|
||||||
|
else if ((numCharactersInCurrentLine + gateIdLength) <= charactersPerLine) {
|
||||||
gateIdsForCurrentLine.add(gateId);
|
gateIdsForCurrentLine.add(gateId);
|
||||||
numCharactersInCurrentLine += gateIdLength;
|
numCharactersInCurrentLine += gateIdLength;
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
} else { // the current gate does not fit on the
|
}
|
||||||
|
|
||||||
|
// the current gate does not fit on the
|
||||||
|
else {
|
||||||
lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", ");
|
lines.add(TextUtil.implode(gateIdsForCurrentLine, ", ") + ", ");
|
||||||
|
|
||||||
gateIdsForCurrentLine = new ArrayList<>();
|
gateIdsForCurrentLine = new ArrayList<String>();
|
||||||
numCharactersInCurrentLine = 0;
|
numCharactersInCurrentLine = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -121,44 +116,72 @@ public class CommandList extends BaseCommand {
|
|||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote) {
|
|
||||||
|
protected static String intToTitleString(int i, boolean addPreviousPageNote, boolean addNextPageNote)
|
||||||
|
{
|
||||||
String retVal = ChatColor.DARK_AQUA + "";
|
String retVal = ChatColor.DARK_AQUA + "";
|
||||||
|
|
||||||
if (i < 26) {
|
if ( i < 26 ) {
|
||||||
retVal += (char) (i + 65);
|
retVal += (char)(i+65);
|
||||||
} else if (i == 26) {
|
}
|
||||||
|
else if ( i == 26 ) {
|
||||||
retVal += "0-9";
|
retVal += "0-9";
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
retVal += "!@#$";
|
retVal += "!@#$";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addPreviousPageNote && addNextPageNote) {
|
if (addPreviousPageNote && addNextPageNote) {
|
||||||
retVal += " (more on previous and next page)";
|
retVal += " (more on previous and next page)";
|
||||||
} else if (addPreviousPageNote) {
|
}
|
||||||
|
else if (addPreviousPageNote) {
|
||||||
retVal += " (more on previous page)";
|
retVal += " (more on previous page)";
|
||||||
} else if (addNextPageNote) {
|
}
|
||||||
|
else if (addNextPageNote) {
|
||||||
retVal += " (more on next page)";
|
retVal += " (more on next page)";
|
||||||
}
|
}
|
||||||
|
|
||||||
return retVal + "\n";
|
return retVal + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Method for getting a collection of gates the player is allowed to see.
|
* Method for getting a collection of gates the player is allowed to see.
|
||||||
*/
|
*/
|
||||||
private Collection<Gate> getAllGates() {
|
protected Collection<Gate> getAllGates()
|
||||||
Collection<Gate> gates = gatesManager.allGates();
|
{
|
||||||
|
Collection<Gate> gates = Plugin.getPlugin().getGatesManager().allGates();
|
||||||
|
|
||||||
|
if (this.sender instanceof Player && Plugin.getPermission() != null) {
|
||||||
|
Player p = (Player)this.sender;
|
||||||
|
|
||||||
// create a copy since we cannot iterate over a collection while modifying it!
|
// create a copy since we cannot iterate over a collection while modifying it!
|
||||||
Collection<Gate> gatesCopy = new ArrayList<>(gates);
|
Collection<Gate> gatesCopy = new ArrayList<Gate>(gates);
|
||||||
|
|
||||||
for (Gate gate : gatesCopy) {
|
for (Gate gate : gatesCopy) {
|
||||||
if (!this.permissionController.hasPermission(this.sender, gate, this.requiredPermission)) {
|
|
||||||
|
if (gate.getLocation() != null) {
|
||||||
|
boolean permissionAtGateLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
if (!permissionAtGateLocation) {
|
||||||
|
gates.remove(gate);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gate.getExit() != null) {
|
||||||
|
|
||||||
|
boolean permissionAtGateExit = Plugin.getPermission().has(gate.getExit().getWorld(), p.getName(), this.requiredPermission);
|
||||||
|
if (!permissionAtGateExit) {
|
||||||
gates.remove(gate);
|
gates.remove(gate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return gates;
|
return gates;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sorts all gates by there first character.
|
* Sorts all gates by there first character.
|
||||||
* Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically)
|
* Puts gates in corresponding Lists: (all returned lists will be sorted alphabetically)
|
||||||
@ -166,11 +189,12 @@ public class CommandList extends BaseCommand {
|
|||||||
* list 26: 0-9
|
* list 26: 0-9
|
||||||
* list 27: other
|
* list 27: other
|
||||||
*/
|
*/
|
||||||
private static List<List<String>> gatesSortedByName(Collection<Gate> allGates) {
|
protected static List<List<String>> gatesSortedByName(Collection<Gate> allGates)
|
||||||
|
{
|
||||||
// create the lists
|
// create the lists
|
||||||
List<List<String>> ids = new ArrayList<>();
|
List<List<String>> ids = new ArrayList<List<String>>();
|
||||||
|
|
||||||
for (int i = 0; i < 28; i++) {
|
for (int i=0; i<28; i++) {
|
||||||
ids.add(new ArrayList<String>());
|
ids.add(new ArrayList<String>());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,11 +205,14 @@ public class CommandList extends BaseCommand {
|
|||||||
|
|
||||||
if (first > 96 && first < 123) { // convert lower case chars
|
if (first > 96 && first < 123) { // convert lower case chars
|
||||||
first -= 97;
|
first -= 97;
|
||||||
} else if (first > 64 && first < 91) { // convert upper case chars
|
}
|
||||||
|
else if (first > 64 && first < 91) { // convert upper case chars
|
||||||
first -= 65;
|
first -= 65;
|
||||||
} else if (first > 47 && first < 58) { // convert numbers
|
}
|
||||||
|
else if (first > 47 && first < 58) { // convert numbers
|
||||||
first = 26;
|
first = 26;
|
||||||
} else { // everything else
|
}
|
||||||
|
else { // everything else
|
||||||
first = 27;
|
first = 27;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -193,20 +220,22 @@ public class CommandList extends BaseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sort everything
|
// sort everything
|
||||||
for (int i = 0; i < 28; i++) {
|
for (int i=0; i<28; i++) {
|
||||||
Collections.sort(ids.get(i));
|
Collections.sort(ids.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
return ids;
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a list of strings.
|
* Returns a list of strings.
|
||||||
* Each string is the text for a page.
|
* Each string is the text for a page.
|
||||||
* The maximum number of lines per page is 'linesPerPage' minus 1.
|
* The maximum number of lines per page is 'linesPerPage' minus 1.
|
||||||
* Will return an empty list if no gates are availible.
|
* Will return an empty list if no gates are availible.
|
||||||
*/
|
*/
|
||||||
private List<String> pagedGateIds() {
|
protected List<String> pagedGateIds()
|
||||||
|
{
|
||||||
Collection<Gate> gates = this.getAllGates();
|
Collection<Gate> gates = this.getAllGates();
|
||||||
|
|
||||||
if (gates.size() == 0) {
|
if (gates.size() == 0) {
|
||||||
@ -214,15 +243,15 @@ public class CommandList extends BaseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
List<List<String>> gatesSortedByName = gatesSortedByName(gates);
|
List<List<String>> gatesSortedByName = gatesSortedByName(gates);
|
||||||
List<String> allPages = new ArrayList<>();
|
List<String> allPages = new ArrayList<String>();
|
||||||
int linesLeftOnPage = linesPerPage - 1;
|
int linesLeftOnPage = linesPerPage - 1;
|
||||||
String currentPageString = "";
|
String currentPageString = "";
|
||||||
|
|
||||||
for (int i = 0; i < gatesSortedByName.size(); i++) {
|
for (int i=0; i<gatesSortedByName.size(); i++) {
|
||||||
|
|
||||||
List<String> currentGates = gatesSortedByName.get(i);
|
List<String> currentGates = gatesSortedByName.get(i);
|
||||||
|
|
||||||
if (currentGates.isEmpty()) {
|
if(currentGates.isEmpty()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -232,7 +261,7 @@ public class CommandList extends BaseCommand {
|
|||||||
while (!currentGatesAsLines.isEmpty()) {
|
while (!currentGatesAsLines.isEmpty()) {
|
||||||
|
|
||||||
if (linesLeftOnPage < 2) {
|
if (linesLeftOnPage < 2) {
|
||||||
currentPageString = currentPageString.substring(0, currentPageString.length() - 2); // remove newlines add the end of the page
|
currentPageString = currentPageString.substring(0, currentPageString.length()-2); // remove newlines add the end of the page
|
||||||
allPages.add(currentPageString);
|
allPages.add(currentPageString);
|
||||||
currentPageString = "";
|
currentPageString = "";
|
||||||
|
|
||||||
@ -247,8 +276,9 @@ public class CommandList extends BaseCommand {
|
|||||||
if (linesNecessaryForCurrentGates < linesLeftOnPage) {
|
if (linesNecessaryForCurrentGates < linesLeftOnPage) {
|
||||||
linesToFill = linesNecessaryForCurrentGates;
|
linesToFill = linesNecessaryForCurrentGates;
|
||||||
moreGatesOnNextPage = false;
|
moreGatesOnNextPage = false;
|
||||||
} else {
|
}
|
||||||
linesToFill = linesLeftOnPage - 1;
|
else {
|
||||||
|
linesToFill = linesLeftOnPage -1;
|
||||||
moreGatesOnNextPage = true;
|
moreGatesOnNextPage = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -258,12 +288,12 @@ public class CommandList extends BaseCommand {
|
|||||||
linesLeftOnPage--;
|
linesLeftOnPage--;
|
||||||
|
|
||||||
// add gate lines
|
// add gate lines
|
||||||
for (int j = 0; j < linesToFill; j++) {
|
for (int j=0; j<linesToFill; j++) {
|
||||||
currentPageString += currentGatesAsLines.get(j) + "\n";
|
currentPageString += currentGatesAsLines.get(j) + "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove lines added
|
// remove lines added
|
||||||
for (int j = 0; j < linesToFill; j++) {
|
for (int j=0; j<linesToFill; j++) {
|
||||||
currentGatesAsLines.remove(0);
|
currentGatesAsLines.remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -275,7 +305,7 @@ public class CommandList extends BaseCommand {
|
|||||||
|
|
||||||
// add the last page
|
// add the last page
|
||||||
if (!currentPageString.isEmpty()) {
|
if (!currentPageString.isEmpty()) {
|
||||||
currentPageString = currentPageString.substring(0, currentPageString.length() - 2); // remove newlines add the end of the page
|
currentPageString = currentPageString.substring(0, currentPageString.length()-2); // remove newlines add the end of the page
|
||||||
allPages.add(currentPageString);
|
allPages.add(currentPageString);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,14 +313,39 @@ public class CommandList extends BaseCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int getPageParameter() {
|
protected int getPageParameter()
|
||||||
|
{
|
||||||
int page = 1;
|
int page = 1;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
page = new Integer(parameters.get(0));
|
page = new Integer(parameters.get(0));
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
}
|
||||||
|
catch (Exception ignored) { }
|
||||||
|
|
||||||
return page;
|
return page;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
|
int page = this.getPageParameter();
|
||||||
|
List<String> allPages = this.pagedGateIds();
|
||||||
|
|
||||||
|
if (allPages == null) { // no gates exist
|
||||||
|
sendMessage(ChatColor.RED + "There are no gates yet. " + ChatColor.RESET +
|
||||||
|
"(Note that you might not be allowed to get information about certain gates)");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (page > allPages.size() || page < 1) {
|
||||||
|
sendMessage(ChatColor.RED + "The requested page is not availible");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String message = TextUtil.titleize("List of all gates (" + page + "/" + allPages.size() + ")") + "\n";
|
||||||
|
message += allPages.get(page-1);
|
||||||
|
|
||||||
|
sendMessage(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,34 +16,42 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
import org.bukkit.block.Block;
|
import org.bukkit.block.Block;
|
||||||
|
|
||||||
public class CommandLocation extends BaseLocationCommand {
|
|
||||||
|
|
||||||
public CommandLocation() {
|
public class CommandLocation extends BaseLocationCommand
|
||||||
|
{
|
||||||
|
public CommandLocation()
|
||||||
|
{
|
||||||
aliases.add("location");
|
aliases.add("location");
|
||||||
aliases.add("lo");
|
aliases.add("lo");
|
||||||
|
|
||||||
requiredParameters.add("id");
|
requiredParameters.add("id");
|
||||||
|
|
||||||
helpDescription = "Set the entrance of the gate to your current location.";
|
helpDescription = "Set the entrance of the gate to your current location.";
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = true;
|
needsPermissionAtCurrentLocation = true;
|
||||||
shouldPersistToDisk = true;
|
shouldPersistToDisk = true;
|
||||||
senderMustBePlayer = true;
|
senderMustBePlayer = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
Location playerLocation = getValidPlayerLocation();
|
Location playerLocation = getValidPlayerLocation();
|
||||||
|
|
||||||
if (playerLocation == null) {
|
if (playerLocation == null)
|
||||||
|
{
|
||||||
sendMessage("There is not enough room for a gate to open here");
|
sendMessage("There is not enough room for a gate to open here");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -52,19 +60,25 @@ public class CommandLocation extends BaseLocationCommand {
|
|||||||
Set<Location> oldGateBlockLocations = gate.getGateBlockLocations();
|
Set<Location> oldGateBlockLocations = gate.getGateBlockLocations();
|
||||||
Set<Block> oldFrameBlocks = gate.getGateFrameBlocks();
|
Set<Block> oldFrameBlocks = gate.getGateFrameBlocks();
|
||||||
|
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
if (gate.isOpen()) {
|
if (gate.isOpen()) {
|
||||||
GateBlockChangeSender.updateGateBlocks(gate, true);
|
GateBlockChangeSender.updateGateBlocks(gate, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
gate.setLocation(playerLocation);
|
gate.setLocation(playerLocation);
|
||||||
|
|
||||||
sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location.");
|
sendMessage(ChatColor.GREEN + "The location of '" + gate.getId() + "' is now at your current location.");
|
||||||
} catch (Exception e) {
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
sendMessage(ChatColor.RED + "There seems to be no frame at your new location! The gate got closed!" + ChatColor.AQUA + " You should build a frame now and execute:");
|
sendMessage(ChatColor.RED + "There seems to be no frame at your new location! The gate got closed!" + ChatColor.AQUA + " You should build a frame now and execute:");
|
||||||
sendMessage(new CommandTriggerOpen().getUsageTemplate(true));
|
sendMessage(new CommandOpen().getUsageTemplate(true, true));
|
||||||
} finally {
|
}
|
||||||
gatesManager.handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks);
|
finally {
|
||||||
|
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, oldLocation, oldGateBlockLocations, oldFrameBlocks);
|
||||||
GateBlockChangeSender.updateGateBlocks(gate);
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,45 +0,0 @@
|
|||||||
package de.craftinc.gates.commands;
|
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.models.GateMaterial;
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
|
|
||||||
import java.security.InvalidParameterException;
|
|
||||||
|
|
||||||
public class CommandMaterial extends BaseCommand {
|
|
||||||
|
|
||||||
public CommandMaterial() {
|
|
||||||
aliases.add("material");
|
|
||||||
aliases.add("m");
|
|
||||||
|
|
||||||
requiredParameters.add("id");
|
|
||||||
requiredParameters.add("material");
|
|
||||||
|
|
||||||
senderMustBePlayer = false;
|
|
||||||
hasGateParam = true;
|
|
||||||
helpDescription = "Change the material of a gate";
|
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
needsPermissionAtCurrentLocation = false;
|
|
||||||
shouldPersistToDisk = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
GateMaterial material;
|
|
||||||
try {
|
|
||||||
material = new GateMaterial(parameters.get(1));
|
|
||||||
} catch (InvalidParameterException e) {
|
|
||||||
sendMessage(ChatColor.RED + "Invalid material!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
gate.setMaterial(material);
|
|
||||||
} catch (Exception e) {
|
|
||||||
sendMessage(ChatColor.RED + "Frame invalid. Gate is now closed!");
|
|
||||||
}
|
|
||||||
|
|
||||||
GateBlockChangeSender.updateGateBlocks(gate);
|
|
||||||
sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " uses now " + material.toString() + " as material.");
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,41 +1,53 @@
|
|||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
import de.craftinc.gates.Gate;
|
||||||
|
import de.craftinc.gates.GatesManager;
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import de.craftinc.gates.util.TextUtil;
|
import de.craftinc.gates.util.TextUtil;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public class CommandNearby extends BaseLocationCommand {
|
public class CommandNearby extends BaseLocationCommand
|
||||||
|
{
|
||||||
public CommandNearby() {
|
public CommandNearby()
|
||||||
|
{
|
||||||
aliases.add("nearby");
|
aliases.add("nearby");
|
||||||
aliases.add("nb");
|
aliases.add("nb");
|
||||||
|
|
||||||
helpDescription = "Highlight nearby gates";
|
helpDescription = "Highlight nearby gates";
|
||||||
requiredPermission = PermissionController.permissionInfo;
|
|
||||||
|
requiredPermission = Plugin.permissionInfo;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = true;
|
needsPermissionAtCurrentLocation = true;
|
||||||
shouldPersistToDisk = false;
|
shouldPersistToDisk = false;
|
||||||
senderMustBePlayer = true;
|
senderMustBePlayer = true;
|
||||||
hasGateParam = false;
|
hasGateParam = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
Set<Gate> nearbyGates = gatesManager.getNearbyGates(player.getLocation().getChunk());
|
public void perform()
|
||||||
|
{
|
||||||
|
GatesManager manager = Plugin.getPlugin().getGatesManager();
|
||||||
|
Set<Gate> nearbyGates = manager.getNearbyGates(player.getLocation().getChunk());
|
||||||
|
|
||||||
if (nearbyGates == null) {
|
if (nearbyGates == null) {
|
||||||
player.sendMessage("There are no gates near you!");
|
player.sendMessage("There are no gates near you!");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
GateBlockChangeSender.temporaryHighlightGatesFrames(player, nearbyGates);
|
GateBlockChangeSender.temporaryHighlightGatesFrames(player, nearbyGates);
|
||||||
|
|
||||||
ArrayList<String> gateNames = new ArrayList<>();
|
ArrayList<String> gateNames = new ArrayList<String>();
|
||||||
|
|
||||||
for (Gate g : nearbyGates) {
|
for (Gate g : nearbyGates) {
|
||||||
gateNames.add(g.getId());
|
gateNames.add(g.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
player.sendMessage("Nearby gates: " + TextUtil.implode(gateNames, ", "));
|
player.sendMessage("Nearby gates: " + TextUtil.implode(gateNames, ", "));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,32 +16,41 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
import de.craftinc.gates.Gate;
|
||||||
|
import de.craftinc.gates.GatesManager;
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
public class CommandNew extends BaseLocationCommand {
|
|
||||||
|
|
||||||
public CommandNew() {
|
public class CommandNew extends BaseLocationCommand
|
||||||
|
{
|
||||||
|
public CommandNew()
|
||||||
|
{
|
||||||
aliases.add("new");
|
aliases.add("new");
|
||||||
aliases.add("create");
|
|
||||||
aliases.add("n");
|
aliases.add("n");
|
||||||
|
|
||||||
requiredParameters.add("id");
|
requiredParameters.add("id");
|
||||||
|
|
||||||
senderMustBePlayer = true;
|
senderMustBePlayer = true;
|
||||||
hasGateParam = false;
|
hasGateParam = false;
|
||||||
|
|
||||||
helpDescription = "Create a gate at your current location.";
|
helpDescription = "Create a gate at your current location.";
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = true;
|
needsPermissionAtCurrentLocation = true;
|
||||||
shouldPersistToDisk = true;
|
shouldPersistToDisk = true;
|
||||||
|
|
||||||
senderMustBePlayer = true;
|
senderMustBePlayer = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
String id = parameters.get(0);
|
String id = parameters.get(0);
|
||||||
|
GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
|
||||||
|
|
||||||
if (gatesManager.gateExists(id)) {
|
if (gatesManager.gateExists(id)) {
|
||||||
sendMessage(ChatColor.RED + "Creating the gate failed! " + "A gate with the supplied id already exists!");
|
sendMessage(ChatColor.RED + "Creating the gate failed! " + "A gate with the supplied id already exists!");
|
||||||
@ -51,19 +60,24 @@ public class CommandNew extends BaseLocationCommand {
|
|||||||
gate = new Gate(id);
|
gate = new Gate(id);
|
||||||
sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created.");
|
sendMessage(ChatColor.GREEN + "Gate with id '" + id + "' was created.");
|
||||||
|
|
||||||
|
|
||||||
Location playerLocation = getValidPlayerLocation();
|
Location playerLocation = getValidPlayerLocation();
|
||||||
|
|
||||||
if (playerLocation != null) {
|
if (playerLocation != null) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
gate.setLocation(playerLocation);
|
gate.setLocation(playerLocation);
|
||||||
sendMessage(ChatColor.AQUA + "The gates location has been set to your current location.");
|
sendMessage(ChatColor.AQUA + "The gates location has been set to your current location.");
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
}
|
||||||
} else {
|
catch (Exception ignored) {}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:");
|
sendMessage(ChatColor.RED + "Your location is invalid!" + ChatColor.AQUA + "Go somewhere else and execute:");
|
||||||
sendMessage(new CommandLocation().getUsageTemplate(true));
|
sendMessage(new CommandLocation().getUsageTemplate(true, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
gatesManager.handleNewGate(gate);
|
gatesManager.handleNewGate(gate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
70
src/de/craftinc/gates/commands/CommandOpen.java
Normal file
70
src/de/craftinc/gates/commands/CommandOpen.java
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
|
|
||||||
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
|
|
||||||
|
public class CommandOpen extends BaseCommand
|
||||||
|
{
|
||||||
|
|
||||||
|
public CommandOpen()
|
||||||
|
{
|
||||||
|
aliases.add("open");
|
||||||
|
aliases.add("o");
|
||||||
|
|
||||||
|
requiredParameters.add("id");
|
||||||
|
|
||||||
|
helpDescription = "Open a gate so players can use it.";
|
||||||
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
|
needsPermissionAtCurrentLocation = false;
|
||||||
|
shouldPersistToDisk = true;
|
||||||
|
senderMustBePlayer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
boolean needsGateManagerUpdate = false;
|
||||||
|
|
||||||
|
if (gate.getGateBlockLocations().isEmpty()) {
|
||||||
|
needsGateManagerUpdate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
gate.setOpen(true);
|
||||||
|
|
||||||
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
|
|
||||||
|
if (needsGateManagerUpdate) {
|
||||||
|
Plugin.getPlugin().getGatesManager().handleGateLocationChange(gate, null, null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
sendMessage(ChatColor.GREEN + "The gate was opened.");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
sendMessage(ChatColor.RED + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -16,13 +16,16 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
public class CommandRemove extends BaseCommand {
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
public CommandRemove() {
|
|
||||||
|
public class CommandRemove extends BaseCommand
|
||||||
|
{
|
||||||
|
public CommandRemove()
|
||||||
|
{
|
||||||
aliases.add("delete");
|
aliases.add("delete");
|
||||||
aliases.add("del");
|
aliases.add("del");
|
||||||
aliases.add("remove");
|
aliases.add("remove");
|
||||||
@ -32,7 +35,7 @@ public class CommandRemove extends BaseCommand {
|
|||||||
senderMustBePlayer = false;
|
senderMustBePlayer = false;
|
||||||
helpDescription = "Removes the gate from the game.";
|
helpDescription = "Removes the gate from the game.";
|
||||||
|
|
||||||
requiredPermission = PermissionController.permissionManage;
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = false;
|
needsPermissionAtCurrentLocation = false;
|
||||||
shouldPersistToDisk = true;
|
shouldPersistToDisk = true;
|
||||||
@ -40,8 +43,10 @@ public class CommandRemove extends BaseCommand {
|
|||||||
senderMustBePlayer = false;
|
senderMustBePlayer = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
gatesManager.handleDeletion(gate);
|
public void perform()
|
||||||
|
{
|
||||||
|
Plugin.getPlugin().getGatesManager().handleDeletion(gate);
|
||||||
GateBlockChangeSender.updateGateBlocks(gate, true);
|
GateBlockChangeSender.updateGateBlocks(gate, true);
|
||||||
sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted.");
|
sendMessage(ChatColor.GREEN + "Gate with id '" + gate.getId() + "' was deleted.");
|
||||||
}
|
}
|
||||||
|
@ -16,31 +16,44 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.commands;
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
public class CommandRename extends BaseCommand {
|
import de.craftinc.gates.GatesManager;
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
public CommandRename() {
|
|
||||||
|
public class CommandRename extends BaseCommand
|
||||||
|
{
|
||||||
|
public CommandRename()
|
||||||
|
{
|
||||||
aliases.add("rename");
|
aliases.add("rename");
|
||||||
aliases.add("rn");
|
aliases.add("rn");
|
||||||
|
|
||||||
hasGateParam = true;
|
hasGateParam = true;
|
||||||
senderMustBePlayer = false;
|
senderMustBePlayer = false;
|
||||||
|
|
||||||
requiredParameters.add("current name");
|
requiredParameters.add("current name");
|
||||||
requiredParameters.add("new name");
|
requiredParameters.add("new name");
|
||||||
|
|
||||||
helpDescription = "Changes the id of a gate.";
|
helpDescription = "Changes the id of a gate.";
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = false;
|
needsPermissionAtCurrentLocation = false;
|
||||||
shouldPersistToDisk = true;
|
shouldPersistToDisk = true;
|
||||||
|
senderMustBePlayer = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
String newId = parameters.get(1);
|
String newId = parameters.get(1);
|
||||||
|
GatesManager gatesManager = Plugin.getPlugin().getGatesManager();
|
||||||
|
|
||||||
if (gatesManager.gateExists(newId)) {
|
if (gatesManager.gateExists(newId)) {
|
||||||
sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + ".");
|
sendMessage(ChatColor.RED + "Cannot rename " + gate.getId() + ". There is already a gate named " + newId + ".");
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
String oldId = gate.getId();
|
String oldId = gate.getId();
|
||||||
|
|
||||||
gate.setId(newId);
|
gate.setId(newId);
|
||||||
@ -49,4 +62,5 @@ public class CommandRename extends BaseCommand {
|
|||||||
sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " is now known as " + newId + ".");
|
sendMessage(ChatColor.GREEN + "Gate " + gate.getId() + " is now known as " + newId + ".");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
package de.craftinc.gates.commands;
|
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
|
|
||||||
public class CommandTeleport extends BaseCommand {
|
|
||||||
|
|
||||||
public CommandTeleport() {
|
|
||||||
aliases.add("teleport");
|
|
||||||
aliases.add("t");
|
|
||||||
|
|
||||||
requiredParameters.add("id");
|
|
||||||
|
|
||||||
senderMustBePlayer = true;
|
|
||||||
hasGateParam = true;
|
|
||||||
helpDescription = "Teleport to the location of a gate";
|
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
needsPermissionAtCurrentLocation = false;
|
|
||||||
shouldPersistToDisk = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
/* Craft Inc. Gates
|
|
||||||
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
|
||||||
|
|
||||||
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package de.craftinc.gates.commands;
|
|
||||||
|
|
||||||
import de.craftinc.gates.controllers.PermissionController;
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
|
|
||||||
public class CommandTriggerOpen extends BaseCommand {
|
|
||||||
|
|
||||||
public CommandTriggerOpen() {
|
|
||||||
aliases.add("triggerOpen");
|
|
||||||
aliases.add("o");
|
|
||||||
|
|
||||||
requiredParameters.add("id");
|
|
||||||
helpDescription = "Open/close a gate.";
|
|
||||||
requiredPermission = PermissionController.permissionManage;
|
|
||||||
|
|
||||||
needsPermissionAtCurrentLocation = false;
|
|
||||||
shouldPersistToDisk = true;
|
|
||||||
senderMustBePlayer = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void perform() {
|
|
||||||
try {
|
|
||||||
if (!gate.isOpen() && gate.getExit() == null && player != null) {
|
|
||||||
gate.setExit(player.getLocation());
|
|
||||||
sendMessage(ChatColor.GREEN + "The exit of gate '" + gate.getId() + "' is now where you stand.");
|
|
||||||
}
|
|
||||||
|
|
||||||
gate.setOpen(!gate.isOpen());
|
|
||||||
|
|
||||||
GateBlockChangeSender.updateGateBlocks(gate);
|
|
||||||
gatesManager.handleGateLocationChange(gate, null, null, null);
|
|
||||||
sendMessage(ChatColor.GREEN + "The gate is now " + (gate.isOpen() ? "open." : "closed."));
|
|
||||||
} catch (Exception e) {
|
|
||||||
sendMessage(ChatColor.RED + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
58
src/de/craftinc/gates/commands/CommandUnhide.java
Normal file
58
src/de/craftinc/gates/commands/CommandUnhide.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.commands;
|
||||||
|
|
||||||
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
|
|
||||||
|
public class CommandUnhide extends BaseCommand
|
||||||
|
{
|
||||||
|
|
||||||
|
public CommandUnhide()
|
||||||
|
{
|
||||||
|
aliases.add("unhide");
|
||||||
|
aliases.add("u");
|
||||||
|
|
||||||
|
requiredParameters.add("id");
|
||||||
|
|
||||||
|
helpDescription = "Make that gate visible";
|
||||||
|
|
||||||
|
requiredPermission = Plugin.permissionManage;
|
||||||
|
|
||||||
|
needsPermissionAtCurrentLocation = false;
|
||||||
|
shouldPersistToDisk = true;
|
||||||
|
senderMustBePlayer = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void perform()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
gate.setHidden(false);
|
||||||
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
|
sendMessage(ChatColor.GREEN + "The gate " + gate.getId() + " is now visible.");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
sendMessage(ChatColor.RED + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,477 +0,0 @@
|
|||||||
/* Craft Inc. Gates
|
|
||||||
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
|
||||||
|
|
||||||
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package de.craftinc.gates.controllers;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.logging.Level;
|
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
import de.craftinc.gates.persistence.MigrationUtil;
|
|
||||||
import de.craftinc.gates.util.ConfigurationUtil;
|
|
||||||
import org.bukkit.Chunk;
|
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.block.Block;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
|
|
||||||
import de.craftinc.gates.models.SimpleChunk;
|
|
||||||
import de.craftinc.gates.models.SimpleLocation;
|
|
||||||
|
|
||||||
public class GatesManager {
|
|
||||||
protected List<Gate> gates;
|
|
||||||
|
|
||||||
private static final String gatesPath = "gates"; // path to gates inside the yaml file
|
|
||||||
private static final String storageVersionPath = "version";
|
|
||||||
private static final int storageVersion = 3;
|
|
||||||
|
|
||||||
private File gatesConfigFile;
|
|
||||||
private FileConfiguration gatesConfig;
|
|
||||||
private int chunkRadius;
|
|
||||||
private Map<String, Gate> gatesById;
|
|
||||||
private Map<SimpleChunk, Set<Gate>> gatesByChunk;
|
|
||||||
private Map<SimpleLocation, Gate> gatesByLocation;
|
|
||||||
private Map<SimpleLocation, Gate> gatesByFrameLocation;
|
|
||||||
private boolean storageFileIsInvalid = false;
|
|
||||||
|
|
||||||
public Gate getGateWithId(final String id) {
|
|
||||||
return gatesById.get(id.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<Gate> getNearbyGates(final Chunk chunk) {
|
|
||||||
SimpleChunk simpleChunk = new SimpleChunk(chunk);
|
|
||||||
return gatesByChunk.get(simpleChunk);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the closest gate.
|
|
||||||
*
|
|
||||||
* @param location The location at which to look for a gate.
|
|
||||||
* @return Might return null if there are no nearby gates.
|
|
||||||
*/
|
|
||||||
public Gate getNearestGate(final Location location) {
|
|
||||||
Set<Gate> nearbyGates = getNearbyGates(location.getChunk());
|
|
||||||
|
|
||||||
if (nearbyGates == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
double minDist = Double.MAX_VALUE;
|
|
||||||
Gate nearestGate = null;
|
|
||||||
|
|
||||||
for (Gate g : nearbyGates) {
|
|
||||||
double dist = location.distance(g.getLocation());
|
|
||||||
|
|
||||||
if (dist < minDist) {
|
|
||||||
minDist = dist;
|
|
||||||
nearestGate = g;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nearestGate;
|
|
||||||
}
|
|
||||||
|
|
||||||
Gate getGateAtLocation(final Location location) {
|
|
||||||
SimpleLocation simpleLocation = new SimpleLocation(location);
|
|
||||||
return gatesByLocation.get(simpleLocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
public Gate getGateAtFrameLocation(final Location location) {
|
|
||||||
SimpleLocation simpleLocation = new SimpleLocation(location);
|
|
||||||
return gatesByFrameLocation.get(simpleLocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveGatesToDisk() {
|
|
||||||
if (storageFileIsInvalid) {
|
|
||||||
Plugin.log(Level.SEVERE, "ERROR: Not saving gates to disk. Storage file is invalid or corrupted!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
gatesConfig.set(gatesPath, gates);
|
|
||||||
gatesConfig.set(storageVersionPath, storageVersion);
|
|
||||||
|
|
||||||
try {
|
|
||||||
gatesConfig.save(gatesConfigFile);
|
|
||||||
Plugin.log("Saved gates to disk.");
|
|
||||||
} catch (IOException e) {
|
|
||||||
Plugin.log(Level.SEVERE, "ERROR: Could not save gates to disk.");
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public boolean loadGatesFromDisk() {
|
|
||||||
// TODO: refactor: move loading/saving logic into persistence package
|
|
||||||
|
|
||||||
this.gatesConfigFile = new File(Plugin.getPlugin().getDataFolder(), "gates.yml");
|
|
||||||
|
|
||||||
if (!this.gatesConfigFile.exists()) {
|
|
||||||
try {
|
|
||||||
boolean isNew = this.gatesConfigFile.createNewFile();
|
|
||||||
|
|
||||||
if (isNew) {
|
|
||||||
Plugin.log(Level.FINEST, "Created gate storage file.");
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
this.storageFileIsInvalid = true;
|
|
||||||
Plugin.log(Level.SEVERE, "Cannot create gate storage file! No gates will be persisted.");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.gatesConfig = new YamlConfiguration();
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.gatesConfig.load(this.gatesConfigFile);
|
|
||||||
} catch (Exception e) {
|
|
||||||
this.storageFileIsInvalid = true;
|
|
||||||
Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (" + Arrays.toString(e.getStackTrace()) + ")");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.gates = (List<Gate>) gatesConfig.getList(gatesPath);
|
|
||||||
|
|
||||||
if (this.gates == null) {
|
|
||||||
this.gates = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Object o : this.gates) {
|
|
||||||
|
|
||||||
if (!(o instanceof Gate)) {
|
|
||||||
this.storageFileIsInvalid = true;
|
|
||||||
Plugin.log(Level.SEVERE, "Gate file on disk is invalid. No gates loaded. Plugin will be disabled! (Invalid gate class detected)");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Gate g : this.gates) {
|
|
||||||
try {
|
|
||||||
g.validate();
|
|
||||||
} catch (Exception e) {
|
|
||||||
try {
|
|
||||||
g.setOpen(false);
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
|
|
||||||
Plugin.log(Level.FINER, "closed gate '" + g.getId() + "' reason: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fillGatesById();
|
|
||||||
fillGatesByChunk();
|
|
||||||
fillGatesByLocation();
|
|
||||||
fillGatesByFrameLocation();
|
|
||||||
|
|
||||||
Plugin.log("Loaded " + this.gates.size() + " gates.");
|
|
||||||
|
|
||||||
// migration
|
|
||||||
int fileStorageVersion = gatesConfig.getInt(storageVersionPath);
|
|
||||||
|
|
||||||
if (fileStorageVersion > storageVersion) {
|
|
||||||
this.storageFileIsInvalid = true;
|
|
||||||
Plugin.log(Level.SEVERE, "Unsupported storage version detected! Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fileStorageVersion < storageVersion && !this.gates.isEmpty()) {
|
|
||||||
Plugin.log("Outdated storage version detected. Performing data migration...");
|
|
||||||
boolean success = MigrationUtil.performMigration(fileStorageVersion, storageVersion, this.gates);
|
|
||||||
|
|
||||||
this.storageFileIsInvalid = !success;
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int getChunkRadius() {
|
|
||||||
if (this.chunkRadius == 0) {
|
|
||||||
this.chunkRadius = Plugin.getPlugin().getConfig().getInt(ConfigurationUtil.confPlayerGateBlockUpdateRadiusKey);
|
|
||||||
this.chunkRadius = this.chunkRadius >> 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
return this.chunkRadius;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void fillGatesById() {
|
|
||||||
gatesById = new HashMap<>((int) (gates.size() * 1.25));
|
|
||||||
|
|
||||||
for (Gate g : gates) {
|
|
||||||
this.addGateWithId(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void fillGatesByChunk() {
|
|
||||||
HashSet<SimpleChunk> chunksUsedByGates = new HashSet<>();
|
|
||||||
|
|
||||||
for (Gate g : gates) {
|
|
||||||
if (g.getLocation() != null) {
|
|
||||||
Chunk c = g.getLocation().getChunk();
|
|
||||||
|
|
||||||
int x = c.getX();
|
|
||||||
int z = c.getZ();
|
|
||||||
|
|
||||||
for (int i = x - getChunkRadius(); i < x + getChunkRadius(); i++) {
|
|
||||||
for (int j = z - getChunkRadius(); j < z + getChunkRadius(); j++) {
|
|
||||||
chunksUsedByGates.add(new SimpleChunk(i, j, c.getWorld()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gatesByChunk = new HashMap<>((int) (chunksUsedByGates.size() * 1.25));
|
|
||||||
|
|
||||||
for (Gate g : gates) {
|
|
||||||
this.addGateByChunk(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void fillGatesByLocation() {
|
|
||||||
Set<Location> gateBlocks = new HashSet<>();
|
|
||||||
|
|
||||||
for (Gate g : gates) {
|
|
||||||
for (Location l : g.getGateBlockLocations()) {
|
|
||||||
gateBlocks.add(l);
|
|
||||||
Location headLocation = l.clone().add(0, 1, 0);
|
|
||||||
gateBlocks.add(headLocation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gatesByLocation = new HashMap<>((int) (gateBlocks.size() * 1.25));
|
|
||||||
|
|
||||||
for (Gate g : gates) {
|
|
||||||
this.addGateByLocations(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void fillGatesByFrameLocation() {
|
|
||||||
int numFrameBlocks = 0;
|
|
||||||
|
|
||||||
for (Gate g : gates) {
|
|
||||||
numFrameBlocks += g.getGateFrameBlocks().size();
|
|
||||||
}
|
|
||||||
|
|
||||||
gatesByFrameLocation = new HashMap<>((int) (numFrameBlocks * 1.25));
|
|
||||||
|
|
||||||
for (Gate g : gates) {
|
|
||||||
this.addGateByFrameLocations(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeGateById(final String id) {
|
|
||||||
gatesById.remove(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addGateWithId(final Gate g) {
|
|
||||||
gatesById.put(g.getId(), g);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeGateByLocation(final Set<Location> gateBlocks) {
|
|
||||||
if (gateBlocks != null) {
|
|
||||||
|
|
||||||
for (Location l : gateBlocks) {
|
|
||||||
SimpleLocation sl = new SimpleLocation(l);
|
|
||||||
gatesByLocation.remove(sl);
|
|
||||||
|
|
||||||
SimpleLocation headLocation = new SimpleLocation(l, true);
|
|
||||||
gatesByLocation.remove(headLocation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeGateByFrameLocation(final Set<Block> gateFrameBlocks) {
|
|
||||||
if (gateFrameBlocks != null) {
|
|
||||||
|
|
||||||
for (Block block : gateFrameBlocks) {
|
|
||||||
SimpleLocation sl = new SimpleLocation(block.getLocation());
|
|
||||||
gatesByFrameLocation.remove(sl);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addGateByLocations(final Gate g) {
|
|
||||||
for (Location l : g.getGateBlockLocations()) {
|
|
||||||
SimpleLocation sl = new SimpleLocation(l);
|
|
||||||
gatesByLocation.put(sl, g);
|
|
||||||
|
|
||||||
SimpleLocation headLocation = new SimpleLocation(l, true);
|
|
||||||
gatesByLocation.put(headLocation, g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addGateByFrameLocations(final Gate g) {
|
|
||||||
for (Block block : g.getGateFrameBlocks()) {
|
|
||||||
SimpleLocation sl = new SimpleLocation(block.getLocation());
|
|
||||||
gatesByFrameLocation.put(sl, g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void removeGateFromChunk(final Gate g, final Location l) {
|
|
||||||
if (l != null) {
|
|
||||||
|
|
||||||
Chunk c = l.getChunk();
|
|
||||||
int x = c.getX();
|
|
||||||
int z = c.getZ();
|
|
||||||
|
|
||||||
for (int i = x - getChunkRadius(); i < x + getChunkRadius(); i++) {
|
|
||||||
|
|
||||||
for (int j = z - getChunkRadius(); j < z + getChunkRadius(); j++) {
|
|
||||||
|
|
||||||
SimpleChunk sc = new SimpleChunk(i, j, c.getWorld());
|
|
||||||
Set<Gate> gatesInChunk = gatesByChunk.get(sc);
|
|
||||||
|
|
||||||
if (gatesInChunk != null) {
|
|
||||||
gatesInChunk.remove(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void addGateByChunk(final Gate g) {
|
|
||||||
Location gateLocation = g.getLocation();
|
|
||||||
|
|
||||||
if (gateLocation != null) {
|
|
||||||
|
|
||||||
Chunk c = g.getLocation().getChunk();
|
|
||||||
int x = c.getX();
|
|
||||||
int z = c.getZ();
|
|
||||||
|
|
||||||
for (int i = x - getChunkRadius(); i < x + getChunkRadius(); i++) {
|
|
||||||
|
|
||||||
for (int j = z - getChunkRadius(); j < z + getChunkRadius(); j++) {
|
|
||||||
|
|
||||||
SimpleChunk sc = new SimpleChunk(i, j, c.getWorld());
|
|
||||||
|
|
||||||
Set<Gate> gatesForC = gatesByChunk.get(sc);
|
|
||||||
|
|
||||||
if (gatesForC == null) {
|
|
||||||
gatesForC = new HashSet<>(); // NOTE: not optimizing size here
|
|
||||||
gatesByChunk.put(sc, gatesForC);
|
|
||||||
}
|
|
||||||
|
|
||||||
gatesForC.add(g);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void storeInvalidGate(Map<String, Object> map) {
|
|
||||||
File invalidGatesFile = new File(Plugin.getPlugin().getDataFolder(), "invalid_gates.yml");
|
|
||||||
Boolean invalidGatesFileExists = invalidGatesFile.exists();
|
|
||||||
|
|
||||||
try {
|
|
||||||
FileWriter fileWriter = new FileWriter(invalidGatesFile, true);
|
|
||||||
|
|
||||||
if (!invalidGatesFileExists) {
|
|
||||||
fileWriter.write("gates:\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
fileWriter.write("- ==: ");
|
|
||||||
fileWriter.write(map.get("==").toString() + "\n");
|
|
||||||
map.remove("==");
|
|
||||||
|
|
||||||
fileWriter.write("\topen: false\n");
|
|
||||||
map.remove("open");
|
|
||||||
|
|
||||||
fileWriter.write("\tgateBlocks: []\n");
|
|
||||||
map.remove("gateBlocks");
|
|
||||||
|
|
||||||
|
|
||||||
for (String key : map.keySet()) {
|
|
||||||
Object value = map.get(key);
|
|
||||||
|
|
||||||
fileWriter.write("\t" + key + ": ");
|
|
||||||
|
|
||||||
if (value instanceof Map) {
|
|
||||||
fileWriter.write("\n");
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Map<String, Object> valueMap = (Map<String, Object>) value;
|
|
||||||
|
|
||||||
for (String k : valueMap.keySet()) {
|
|
||||||
Object v = valueMap.get(k);
|
|
||||||
|
|
||||||
fileWriter.write("\t\t" + k + ": " + v.toString() + "\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
fileWriter.write(value.toString() + "\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fileWriter.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
Plugin.log("ERROR: Could not save invalid gates to disk. Reason: \n" + Arrays.toString(e.getStackTrace()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleGateIdChange(final Gate g, final String oldId) {
|
|
||||||
this.removeGateById(oldId);
|
|
||||||
this.addGateWithId(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleGateLocationChange(final Gate g,
|
|
||||||
final Location oldLocation,
|
|
||||||
final Set<Location> oldGateBlockLocations,
|
|
||||||
final Set<Block> oldGateFrameBlocks) {
|
|
||||||
this.removeGateFromChunk(g, oldLocation);
|
|
||||||
this.addGateByChunk(g);
|
|
||||||
|
|
||||||
this.removeGateByLocation(oldGateBlockLocations);
|
|
||||||
this.addGateByLocations(g);
|
|
||||||
|
|
||||||
this.removeGateByFrameLocation(oldGateFrameBlocks);
|
|
||||||
this.addGateByFrameLocations(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleGateExitChange(final Gate g, final Location oldExit) {
|
|
||||||
// nothing to do
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleNewGate(final Gate g) {
|
|
||||||
this.gates.add(g);
|
|
||||||
|
|
||||||
this.addGateByChunk(g);
|
|
||||||
this.addGateByLocations(g);
|
|
||||||
this.addGateWithId(g);
|
|
||||||
this.addGateByFrameLocations(g);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void handleDeletion(final Gate g) {
|
|
||||||
this.gates.remove(g);
|
|
||||||
|
|
||||||
this.removeGateById(g.getId());
|
|
||||||
this.removeGateFromChunk(g, g.getLocation());
|
|
||||||
this.removeGateByLocation(g.getGateBlockLocations());
|
|
||||||
this.removeGateByFrameLocation(g.getGateFrameBlocks());
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean gateExists(final String id) {
|
|
||||||
return gatesById.containsKey(id.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Gate> allGates() {
|
|
||||||
return gates;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
package de.craftinc.gates.controllers;
|
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
import net.milkbowl.vault.permission.Permission;
|
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.OfflinePlayer;
|
|
||||||
import org.bukkit.World;
|
|
||||||
import org.bukkit.command.CommandSender;
|
|
||||||
|
|
||||||
public class PermissionController {
|
|
||||||
public static final String permissionInfo = "craftincgates.info";
|
|
||||||
public static final String permissionManage = "craftincgates.manage";
|
|
||||||
public static final String permissionUse = "craftincgates.use";
|
|
||||||
|
|
||||||
private Permission permission;
|
|
||||||
|
|
||||||
public void setPermission(Permission permission) {
|
|
||||||
this.permission = permission;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasPermission(CommandSender sender, Gate gate, String permission) {
|
|
||||||
assert(sender != null);
|
|
||||||
assert(permission != null);
|
|
||||||
|
|
||||||
if (gate == null) {
|
|
||||||
return hasPermission(sender, permission);
|
|
||||||
}
|
|
||||||
|
|
||||||
final Location location = gate.getLocation();
|
|
||||||
final Location exit = gate.getExit();
|
|
||||||
|
|
||||||
boolean permAtLocation = location == null || hasPermission(sender, location.getWorld(), permission);
|
|
||||||
boolean permAtExit = exit == null || hasPermission(sender, exit.getWorld(), permission);
|
|
||||||
|
|
||||||
return permAtLocation && permAtExit;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean hasPermission(CommandSender sender, String permission) {
|
|
||||||
return hasPermission(sender, (World)null, permission);
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean hasPermission(CommandSender sender, World world, String permission) {
|
|
||||||
assert(sender != null);
|
|
||||||
assert(permission != null);
|
|
||||||
|
|
||||||
if (this.permission == null) {
|
|
||||||
// fallback - use the standard bukkit permission system
|
|
||||||
return sender.hasPermission(permission);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!(sender instanceof OfflinePlayer)) {
|
|
||||||
return this.permission.has(sender, permission);
|
|
||||||
}
|
|
||||||
|
|
||||||
String worldName = world != null ? world.getName() : null;
|
|
||||||
return this.permission.playerHas(worldName, (OfflinePlayer)sender, permission);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,126 +0,0 @@
|
|||||||
package de.craftinc.gates.controllers;
|
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
import de.craftinc.gates.util.ConfigurationUtil;
|
|
||||||
import org.bukkit.ChatColor;
|
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.entity.Entity;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class TeleportController {
|
|
||||||
private TeleportMessageUtil messageUtil;
|
|
||||||
private GatesManager gatesManager;
|
|
||||||
private PermissionController permissionController;
|
|
||||||
|
|
||||||
public TeleportController(Plugin plugin) {
|
|
||||||
this.gatesManager = plugin.getGatesManager();
|
|
||||||
this.messageUtil = new TeleportMessageUtil(plugin.getConfig());
|
|
||||||
this.permissionController = plugin.getPermissionController();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Try teleporting a player at given location.
|
|
||||||
*/
|
|
||||||
public void teleport(final Player player, Location toLocation) {
|
|
||||||
final Gate gateAtLocation = gatesManager.getGateAtLocation(toLocation);
|
|
||||||
|
|
||||||
if ((gateAtLocation == null) || !gateAtLocation.isOpen()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!permissionController.hasPermission(player, gateAtLocation, PermissionController.permissionUse)) {
|
|
||||||
messageUtil.sendNoPermissionMessage(player);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.teleportPlayer(player, gateAtLocation);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void teleportPlayer(final Player player, final Gate gate) {
|
|
||||||
final Entity vehicle = player.getVehicle();
|
|
||||||
|
|
||||||
if (vehicle != null && !gate.getAllowsVehicles()) {
|
|
||||||
messageUtil.sendVehicleForbiddenMessage(player);
|
|
||||||
Plugin.log("no vehicle transport");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Location destination = calculateDestination(player, gate);
|
|
||||||
player.teleport(destination);
|
|
||||||
|
|
||||||
if (vehicle != null) {
|
|
||||||
vehicle.teleport(destination, PlayerTeleportEvent.TeleportCause.PLUGIN);
|
|
||||||
vehicle.setPassenger(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
messageUtil.sendTeleportMessage(player);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Location calculateDestination(Player player, Gate gate) {
|
|
||||||
final Location exit = gate.getExit();
|
|
||||||
final Location pLocation = player.getLocation();
|
|
||||||
final Float newYaw = exit.getYaw() - gate.getLocation().getYaw() + pLocation.getYaw();
|
|
||||||
|
|
||||||
return new Location(exit.getWorld(),
|
|
||||||
exit.getX(),
|
|
||||||
exit.getY(),
|
|
||||||
exit.getZ(),
|
|
||||||
newYaw,
|
|
||||||
pLocation.getPitch()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TeleportMessageUtil {
|
|
||||||
private HashMap<String, Long> lastNoPermissionMessages = new HashMap<>();
|
|
||||||
private FileConfiguration config;
|
|
||||||
|
|
||||||
TeleportMessageUtil(FileConfiguration config) {
|
|
||||||
this.config = config;
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendVehicleForbiddenMessage(Player player) {
|
|
||||||
if (!config.getBoolean(ConfigurationUtil.confShowTeleportNoPermissionMessageKey)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final String notAllowedMessage = config.getString(ConfigurationUtil.confGateTeleportVehicleNotAllowedMessageKey);
|
|
||||||
player.sendMessage(ChatColor.DARK_AQUA + notAllowedMessage);
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendNoPermissionMessage(Player player) {
|
|
||||||
if (!config.getBoolean(ConfigurationUtil.confShowTeleportNoPermissionMessageKey)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final String playerName = player.getPlayer().getName();
|
|
||||||
|
|
||||||
if (playerName == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final Long now = Calendar.getInstance().getTimeInMillis();
|
|
||||||
|
|
||||||
// do not display messages more often than once per second
|
|
||||||
if (!this.lastNoPermissionMessages.containsKey(playerName)
|
|
||||||
|| this.lastNoPermissionMessages.get(playerName) < now - 10000L) {
|
|
||||||
|
|
||||||
final String noPermissionString = config.getString(ConfigurationUtil.confGateTeleportNoPermissionMessageKey);
|
|
||||||
player.sendMessage(ChatColor.RED + noPermissionString);
|
|
||||||
this.lastNoPermissionMessages.put(playerName, now);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void sendTeleportMessage(Player player) {
|
|
||||||
if (!config.getBoolean(ConfigurationUtil.confShowTeleportMessageKey)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final String teleportMessage = config.getString(ConfigurationUtil.confGateTeleportMessageKey);
|
|
||||||
player.sendMessage(ChatColor.DARK_AQUA + teleportMessage);
|
|
||||||
}
|
|
||||||
}
|
|
@ -16,29 +16,31 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.listeners;
|
package de.craftinc.gates.listeners;
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
|
import de.craftinc.gates.Gate;
|
||||||
import de.craftinc.gates.Plugin;
|
import de.craftinc.gates.Plugin;
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.BlockBreakEvent;
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
|
|
||||||
public class BlockBreakListener implements Listener {
|
public class BlockBreakListener implements Listener
|
||||||
|
{
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onBlockBreak(BlockBreakEvent event) {
|
public void onBlockBreak(BlockBreakEvent event)
|
||||||
|
{
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Gate gate = Plugin.getPlugin().getGatesManager().getGateAtFrameLocation(event.getBlock().getLocation());
|
Gate gate = Plugin.getPlugin().getGatesManager().getGateAtFrameLocation(event.getBlock().getLocation());
|
||||||
|
|
||||||
if (gate != null && gate.getMaterial().getMaterial() != Material.AIR) {
|
if (gate != null && !gate.isHidden()) {
|
||||||
try {
|
try {
|
||||||
gate.setOpen(false);
|
gate.setOpen(false);
|
||||||
} catch (Exception ignored) {}
|
}
|
||||||
|
catch (Exception ignored) { }
|
||||||
|
|
||||||
GateBlockChangeSender.updateGateBlocks(gate);
|
GateBlockChangeSender.updateGateBlocks(gate);
|
||||||
}
|
}
|
||||||
|
@ -16,18 +16,19 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.listeners;
|
package de.craftinc.gates.listeners;
|
||||||
|
|
||||||
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||||
|
|
||||||
public class PlayerChangedWorldListener implements Listener {
|
|
||||||
|
|
||||||
|
public class PlayerChangedWorldListener implements Listener
|
||||||
|
{
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void onPlayerChangeWorld(PlayerChangedWorldEvent event) {
|
public void onPlayerChangeWorld(PlayerChangedWorldEvent event)
|
||||||
Player p = event.getPlayer();
|
{
|
||||||
GateBlockChangeSender.updateGateBlocks(p, p.getLocation());
|
GateBlockChangeSender.updateGateBlocks(event.getPlayer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,18 +16,18 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.listeners;
|
package de.craftinc.gates.listeners;
|
||||||
|
|
||||||
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
import org.bukkit.event.player.PlayerJoinEvent;
|
||||||
|
|
||||||
public class PlayerJoinListener implements Listener {
|
public class PlayerJoinListener implements Listener
|
||||||
|
{
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
public void onPlayerJoin(PlayerJoinEvent event)
|
||||||
Player p = event.getPlayer();
|
{
|
||||||
GateBlockChangeSender.updateGateBlocks(p, p.getLocation());
|
GateBlockChangeSender.updateGateBlocks(event.getPlayer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,23 +16,35 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.listeners;
|
package de.craftinc.gates.listeners;
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
import java.util.Calendar;
|
||||||
import de.craftinc.gates.controllers.TeleportController;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import de.craftinc.gates.util.ConfigurationUtil;
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
|
import de.craftinc.gates.util.VehicleCloner;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.entity.Entity;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.entity.Vehicle;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerMoveEvent;
|
import org.bukkit.event.player.PlayerMoveEvent;
|
||||||
|
|
||||||
public class PlayerMoveListener implements Listener {
|
import de.craftinc.gates.Gate;
|
||||||
private TeleportController teleportController;
|
import de.craftinc.gates.GatesManager;
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
|
|
||||||
public PlayerMoveListener(Plugin plugin) {
|
|
||||||
this.teleportController = new TeleportController(plugin);
|
public class PlayerMoveListener implements Listener
|
||||||
}
|
{
|
||||||
|
protected HashMap<String, Long> lastNoPermissionMessages = new HashMap<String, Long>();
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void onPlayerMove(PlayerMoveEvent event) {
|
public void onPlayerMove(PlayerMoveEvent event)
|
||||||
|
{
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -41,6 +53,117 @@ public class PlayerMoveListener implements Listener {
|
|||||||
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getTo());
|
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getTo());
|
||||||
}
|
}
|
||||||
|
|
||||||
teleportController.teleport(event.getPlayer(), event.getTo());
|
final GatesManager gateManager = Plugin.getPlugin().getGatesManager();
|
||||||
|
final Gate gateAtLocation = gateManager.getGateAtLocation(event.getTo());
|
||||||
|
|
||||||
|
if ((gateAtLocation == null) || !gateAtLocation.isOpen()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for permission
|
||||||
|
if (!hasPermission(event.getPlayer(), gateAtLocation)
|
||||||
|
&& Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confShowTeleportNoPermissionMessageKey)) {
|
||||||
|
|
||||||
|
final String playerName = event.getPlayer().getName();
|
||||||
|
|
||||||
|
if (playerName == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the current time
|
||||||
|
final Long now = Calendar.getInstance().getTimeInMillis();
|
||||||
|
|
||||||
|
// do not display messages more often than once per second
|
||||||
|
if (!this.lastNoPermissionMessages.containsKey(playerName) || this.lastNoPermissionMessages.get(playerName) < now - 10000L) {
|
||||||
|
|
||||||
|
final String noPermissionString = Plugin.getPlugin().getConfig().getString(ConfigurationUtil.confGateTeleportNoPermissionMessageKey);
|
||||||
|
event.getPlayer().sendMessage(ChatColor.RED + noPermissionString);
|
||||||
|
this.lastNoPermissionMessages.put(playerName, now);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
this.teleportPlayer(event.getPlayer(), gateAtLocation);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Teleports a player.
|
||||||
|
* @param player The player to teleport.
|
||||||
|
* @param gate The gate to which exit the player will be teleported.
|
||||||
|
*/
|
||||||
|
private void teleportPlayer(final Player player, final Gate gate)
|
||||||
|
{
|
||||||
|
// Destination
|
||||||
|
final Float newYaw = gate.getExit().getYaw() - gate.getLocation().getYaw() + player.getLocation().getYaw();
|
||||||
|
final Location destLocation = new Location( gate.getExit().getWorld(),
|
||||||
|
gate.getExit().getX(),
|
||||||
|
gate.getExit().getY(),
|
||||||
|
gate.getExit().getZ(),
|
||||||
|
newYaw,
|
||||||
|
player.getLocation().getPitch()
|
||||||
|
);
|
||||||
|
|
||||||
|
// Riding
|
||||||
|
final Entity vehicle = player.getVehicle();
|
||||||
|
final boolean vehicleIsSuitable = (vehicle != null) && (vehicle instanceof Vehicle);
|
||||||
|
|
||||||
|
if (vehicle != null && (!vehicleIsSuitable) || !gate.getAllowsVehicles()) {
|
||||||
|
|
||||||
|
if (!gate.getAllowsVehicles() && Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confShowTeleportNoPermissionMessageKey)) {
|
||||||
|
final String notAllowedMessage = Plugin.getPlugin().getConfig().getString(ConfigurationUtil.confGateTeleportVehicleNotAllowedMessageKey);
|
||||||
|
player.sendMessage(ChatColor.DARK_AQUA + notAllowedMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// (eject player)
|
||||||
|
if (vehicleIsSuitable) {
|
||||||
|
vehicle.eject();
|
||||||
|
vehicle.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Teleport
|
||||||
|
player.teleport(destLocation);
|
||||||
|
|
||||||
|
// Riding (mount player)
|
||||||
|
if (vehicleIsSuitable) {
|
||||||
|
final Plugin plugin = Plugin.getPlugin();
|
||||||
|
final BukkitScheduler scheduler = plugin.getServer().getScheduler();
|
||||||
|
|
||||||
|
destLocation.getChunk().load(); // load the destination chunk, no new entity will be created otherwise
|
||||||
|
|
||||||
|
scheduler.scheduleSyncDelayedTask(plugin, new Runnable() {
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
// FIXME: the code below should be executed after the chunk got loaded and not after a fixed time!
|
||||||
|
|
||||||
|
// create a new entity at the destination location
|
||||||
|
final Vehicle newVehicle = VehicleCloner.clone((Vehicle)vehicle, destLocation);
|
||||||
|
newVehicle.setPassenger(player);
|
||||||
|
}
|
||||||
|
}, 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Message
|
||||||
|
if (Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confShowTeleportMessageKey)) {
|
||||||
|
final String teleportMessage = Plugin.getPlugin().getConfig().getString(ConfigurationUtil.confGateTeleportMessageKey);
|
||||||
|
player.sendMessage(ChatColor.DARK_AQUA + teleportMessage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected boolean hasPermission(final Player player, final Gate gate)
|
||||||
|
{
|
||||||
|
if (Plugin.getPermission() == null) { // fallback: use the standard bukkit permission system
|
||||||
|
return player.hasPermission(Plugin.permissionUse);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
final boolean permAtLocation = Plugin.getPermission().has(gate.getLocation().getWorld(), player.getName(), Plugin.permissionUse);
|
||||||
|
final boolean permAtExit = Plugin.getPermission().has(gate.getExit().getWorld(), player.getName(), Plugin.permissionUse);
|
||||||
|
|
||||||
|
return permAtLocation && permAtExit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,15 +16,19 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.listeners;
|
package de.craftinc.gates.listeners;
|
||||||
|
|
||||||
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||||
|
|
||||||
public class PlayerRespawnListener implements Listener {
|
|
||||||
|
public class PlayerRespawnListener implements Listener
|
||||||
|
{
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
@EventHandler(priority = EventPriority.NORMAL)
|
||||||
public void onPlayerRespawn(PlayerRespawnEvent event) {
|
public void onPlayerRespawn(PlayerRespawnEvent event)
|
||||||
|
{
|
||||||
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getRespawnLocation(), true);
|
GateBlockChangeSender.updateGateBlocks(event.getPlayer(), event.getRespawnLocation(), true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,16 +16,18 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.listeners;
|
package de.craftinc.gates.listeners;
|
||||||
|
|
||||||
|
|
||||||
import de.craftinc.gates.util.GateBlockChangeSender;
|
import de.craftinc.gates.util.GateBlockChangeSender;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.player.PlayerTeleportEvent;
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
||||||
|
|
||||||
public class PlayerTeleportListener implements Listener {
|
public class PlayerTeleportListener implements Listener
|
||||||
|
{
|
||||||
@EventHandler(priority = EventPriority.MONITOR)
|
@EventHandler(priority = EventPriority.MONITOR)
|
||||||
public void onPlayerTeleport(PlayerTeleportEvent event) {
|
public void onPlayerTeleport(PlayerTeleportEvent event)
|
||||||
|
{
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
package de.craftinc.gates.listeners;
|
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
|
||||||
import de.craftinc.gates.controllers.TeleportController;
|
|
||||||
import org.bukkit.entity.Entity;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.EventPriority;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.vehicle.VehicleMoveEvent;
|
|
||||||
|
|
||||||
public class VehicleMoveListener implements Listener {
|
|
||||||
private TeleportController teleportController;
|
|
||||||
|
|
||||||
public VehicleMoveListener(Plugin plugin) {
|
|
||||||
this.teleportController = new TeleportController(plugin);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.NORMAL)
|
|
||||||
public void onVehicleMove(VehicleMoveEvent event) {
|
|
||||||
Entity passenger = event.getVehicle().getPassenger();
|
|
||||||
|
|
||||||
if (passenger instanceof Player) {
|
|
||||||
teleportController.teleport((Player)passenger, event.getTo());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,298 +0,0 @@
|
|||||||
/* Craft Inc. Gates
|
|
||||||
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
|
||||||
|
|
||||||
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package de.craftinc.gates.models;
|
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
|
||||||
import de.craftinc.gates.util.ConfigurationUtil;
|
|
||||||
import de.craftinc.gates.util.FloodUtil;
|
|
||||||
import org.bukkit.Location;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.block.Block;
|
|
||||||
import org.bukkit.block.BlockFace;
|
|
||||||
import org.bukkit.configuration.serialization.ConfigurationSerializable;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
|
|
||||||
public class Gate implements ConfigurationSerializable {
|
|
||||||
private Location location; /* saving both location and gateBlockLocations is redundant but makes it easy to allow players to reshape gates */
|
|
||||||
private Set<Location> gateBlockLocations = new HashSet<>(); /* Locations of the blocks inside the gate */
|
|
||||||
private Set<Block> gateFrameBlocks = new HashSet<>();
|
|
||||||
private Location exit;
|
|
||||||
private boolean isOpen = false;
|
|
||||||
private boolean allowsVehicles = true;
|
|
||||||
private String id;
|
|
||||||
private GateMaterial material = new GateMaterial(Material.PORTAL);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* You should never create two gates with the same 'id'. Also see 'setId(String id)'.
|
|
||||||
*
|
|
||||||
* @param id This parameter must not be 'null'. An exception will be thrown otherwise!
|
|
||||||
*/
|
|
||||||
public Gate(final String id) {
|
|
||||||
setId(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public String toString() {
|
|
||||||
return super.toString() + " " + this.getId();
|
|
||||||
}
|
|
||||||
|
|
||||||
public GateDirection getDirection() {
|
|
||||||
if (gateBlockLocations.isEmpty()) {
|
|
||||||
return null;
|
|
||||||
} else {
|
|
||||||
Block some = ((Location)gateBlockLocations.toArray()[0]).getBlock();
|
|
||||||
boolean east = gateBlockLocations.contains(some.getRelative(BlockFace.EAST).getLocation());
|
|
||||||
boolean west = gateBlockLocations.contains(some.getRelative(BlockFace.WEST).getLocation());
|
|
||||||
|
|
||||||
return east || west ? GateDirection.EastWest : GateDirection.NorthSouth;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public GateMaterial getMaterial() {
|
|
||||||
return material;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMaterial(GateMaterial material) throws Exception {
|
|
||||||
this.material = material;
|
|
||||||
validate();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return This method might return a 'null' data.
|
|
||||||
*/
|
|
||||||
public Location getLocation() {
|
|
||||||
return location;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param location Supplying 'null' is permitted.
|
|
||||||
* @throws Exception Will throw an exception if the gate is open and an invalid (no gate frame) location is
|
|
||||||
* supplied. Note that the supplied 'location' will be set even if an exception is thrown. Note that this
|
|
||||||
* gate will be closed if an exception is thrown.
|
|
||||||
*/
|
|
||||||
public void setLocation(final Location location) throws Exception {
|
|
||||||
this.location = location;
|
|
||||||
|
|
||||||
if (isOpen) {
|
|
||||||
findPortalBlocks();
|
|
||||||
validate();
|
|
||||||
} else {
|
|
||||||
this.gateBlockLocations = new HashSet<>();
|
|
||||||
this.gateFrameBlocks = new HashSet<>();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return This method might return a 'null' value.
|
|
||||||
*/
|
|
||||||
public Location getExit() {
|
|
||||||
return exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param exit Supplying 'null' is permitted.
|
|
||||||
* @throws Exception An exception will be thrown if 'null' data is supplied and this gate is open. Note that the
|
|
||||||
* supplied 'exit' will be set even if an exception is thrown. Note that this gate will be closed if an
|
|
||||||
* exception is thrown.
|
|
||||||
*/
|
|
||||||
public void setExit(final Location exit) throws Exception {
|
|
||||||
this.exit = exit;
|
|
||||||
validate();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return This method will never return 'null'.
|
|
||||||
*/
|
|
||||||
public String getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Every gate should have an unique 'id'. You should therefore check if another gate with the same 'id' exists.
|
|
||||||
* Note that this method will not check if another gate with the same 'id' exists!
|
|
||||||
*
|
|
||||||
* @param id This parameter must not be 'null'. An exception will be thrown otherwise!
|
|
||||||
*/
|
|
||||||
public void setId(final String id) {
|
|
||||||
if (id == null) {
|
|
||||||
throw new IllegalArgumentException("gate 'id' cannot be 'null'");
|
|
||||||
}
|
|
||||||
|
|
||||||
this.id = id.toLowerCase();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isOpen() {
|
|
||||||
return isOpen;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setOpen(boolean isOpen) throws Exception {
|
|
||||||
if (isOpen && !this.isOpen) {
|
|
||||||
findPortalBlocks();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isOpen = isOpen;
|
|
||||||
validate();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAllowsVehicles(boolean allowsVehicles) {
|
|
||||||
this.allowsVehicles = allowsVehicles;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getAllowsVehicles() {
|
|
||||||
return this.allowsVehicles;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Will never return 'null' but might return an empty Set.
|
|
||||||
*/
|
|
||||||
public Set<Location> getGateBlockLocations() {
|
|
||||||
return gateBlockLocations;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Will never return 'null' but might return an empty Set.
|
|
||||||
*/
|
|
||||||
public Set<Block> getGateFrameBlocks() {
|
|
||||||
return gateFrameBlocks;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void findPortalBlocks() {
|
|
||||||
gateBlockLocations = new HashSet<>();
|
|
||||||
Set<Block> gateBlocks = FloodUtil.getGatePortalBlocks(location.getBlock());
|
|
||||||
|
|
||||||
if (gateBlocks != null) {
|
|
||||||
for (Block b : gateBlocks) {
|
|
||||||
gateBlockLocations.add(b.getLocation());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gateFrameBlocks = FloodUtil.getFrame(gateBlocks);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if values attributes do add up; will close gate on wrong values.
|
|
||||||
*/
|
|
||||||
public void validate() throws Exception {
|
|
||||||
if (!isOpen) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (location == null) {
|
|
||||||
isOpen = false;
|
|
||||||
this.gateBlockLocations = new HashSet<>();
|
|
||||||
this.gateFrameBlocks = new HashSet<>();
|
|
||||||
|
|
||||||
throw new Exception("Gate got closed. It has no location.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (exit == null) {
|
|
||||||
isOpen = false;
|
|
||||||
this.gateBlockLocations = new HashSet<>();
|
|
||||||
this.gateFrameBlocks = new HashSet<>();
|
|
||||||
|
|
||||||
throw new Exception("Gate got closed. It has no exit.");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (gateBlockLocations.size() == 0) {
|
|
||||||
isOpen = false;
|
|
||||||
this.gateBlockLocations = new HashSet<>();
|
|
||||||
this.gateFrameBlocks = new HashSet<>();
|
|
||||||
|
|
||||||
throw new Exception("Gate got closed. The frame is missing or broken. (no gate blocks)");
|
|
||||||
}
|
|
||||||
|
|
||||||
validateFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void validateFrame() throws Exception {
|
|
||||||
boolean isAir = material.getMaterial() == Material.AIR;
|
|
||||||
boolean ignore = !Plugin.getPlugin().getConfig().getBoolean(ConfigurationUtil.confCheckForBrokenGateFramesKey);
|
|
||||||
|
|
||||||
if (isAir || ignore) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Block b : gateFrameBlocks) {
|
|
||||||
|
|
||||||
if (b.getType() == Material.AIR) {
|
|
||||||
isOpen = false;
|
|
||||||
this.gateBlockLocations = new HashSet<>();
|
|
||||||
this.gateFrameBlocks = new HashSet<>();
|
|
||||||
|
|
||||||
throw new Exception("Gate got closed. The frame is missing or broken. (missing frame block(s))");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* INTERFACE: ConfigurationSerializable
|
|
||||||
*/
|
|
||||||
static private String idKey = "id";
|
|
||||||
static private String locationKey = "location";
|
|
||||||
static private String gateBlocksKey = "gateBlocks";
|
|
||||||
static private String exitKey = "exit";
|
|
||||||
static private String materialKey = "material";
|
|
||||||
static private String isOpenKey = "open";
|
|
||||||
static private String allowsVehiclesKey = "allowsVehiclesKey";
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public Gate(Map<String, Object> map) {
|
|
||||||
try {
|
|
||||||
id = map.get(idKey).toString().toLowerCase();
|
|
||||||
location = (Location) map.get(locationKey);
|
|
||||||
exit = (Location) map.get(exitKey);
|
|
||||||
material = new GateMaterial((String)map.get(materialKey));
|
|
||||||
isOpen = (Boolean) map.get(isOpenKey);
|
|
||||||
allowsVehicles = (Boolean) map.get(allowsVehiclesKey);
|
|
||||||
gateBlockLocations = (Set<Location>) map.get(gateBlocksKey);
|
|
||||||
|
|
||||||
gateFrameBlocks = FloodUtil.getFrameWithLocations(gateBlockLocations);
|
|
||||||
} catch (Exception e) {
|
|
||||||
Plugin.log("ERROR: Failed to load gate '" + id + "'! (" + e.getMessage() + ")");
|
|
||||||
Plugin.log("NOTE: This gate will be removed from 'gates.yml' and added to 'invalid_gates.yml'!");
|
|
||||||
|
|
||||||
Plugin.getPlugin().getGatesManager().storeInvalidGate(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
validate(); // make sure to not write invalid stuff to disk
|
|
||||||
} catch (Exception e) {
|
|
||||||
Plugin.log("The loaded gate " + this.getId() + " seems to be not valid: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Map<String, Object> serialize() {
|
|
||||||
Map<String, Object> retVal = new HashMap<>();
|
|
||||||
|
|
||||||
retVal.put(idKey, id);
|
|
||||||
retVal.put(isOpenKey, isOpen);
|
|
||||||
retVal.put(allowsVehiclesKey, allowsVehicles);
|
|
||||||
retVal.put(gateBlocksKey, gateBlockLocations);
|
|
||||||
retVal.put(materialKey, material.toString());
|
|
||||||
|
|
||||||
if (exit != null) {
|
|
||||||
retVal.put(exitKey, exit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (location != null) {
|
|
||||||
retVal.put(locationKey, location);
|
|
||||||
}
|
|
||||||
|
|
||||||
return retVal;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
package de.craftinc.gates.models;
|
|
||||||
|
|
||||||
public enum GateDirection {
|
|
||||||
EastWest,
|
|
||||||
NorthSouth
|
|
||||||
}
|
|
@ -1,162 +0,0 @@
|
|||||||
package de.craftinc.gates.models;
|
|
||||||
|
|
||||||
import org.bukkit.Material;
|
|
||||||
|
|
||||||
import java.security.InvalidParameterException;
|
|
||||||
|
|
||||||
public class GateMaterial {
|
|
||||||
private Material material;
|
|
||||||
|
|
||||||
GateMaterial(Material material) {
|
|
||||||
this.material = material;
|
|
||||||
}
|
|
||||||
|
|
||||||
public GateMaterial(String materialString) throws InvalidParameterException {
|
|
||||||
Material material;
|
|
||||||
|
|
||||||
switch (materialString) {
|
|
||||||
case "air":
|
|
||||||
material = Material.AIR;
|
|
||||||
break;
|
|
||||||
case "sapling":
|
|
||||||
material = Material.SAPLING;
|
|
||||||
break;
|
|
||||||
case "water":
|
|
||||||
material = Material.STATIONARY_WATER;
|
|
||||||
break;
|
|
||||||
case "lava":
|
|
||||||
material = Material.STATIONARY_LAVA;
|
|
||||||
break;
|
|
||||||
case "cobweb":
|
|
||||||
material = Material.WEB;
|
|
||||||
break;
|
|
||||||
case "grass":
|
|
||||||
material = Material.LONG_GRASS;
|
|
||||||
break;
|
|
||||||
case "dead bush":
|
|
||||||
material = Material.DEAD_BUSH;
|
|
||||||
break;
|
|
||||||
case "dandelion":
|
|
||||||
material = Material.YELLOW_FLOWER;
|
|
||||||
break;
|
|
||||||
case "poppy":
|
|
||||||
material = Material.RED_ROSE;
|
|
||||||
break;
|
|
||||||
case "brown mushroom":
|
|
||||||
material = Material.BROWN_MUSHROOM;
|
|
||||||
break;
|
|
||||||
case "red mushroom":
|
|
||||||
material = Material.RED_MUSHROOM;
|
|
||||||
break;
|
|
||||||
case "torch":
|
|
||||||
material = Material.TORCH;
|
|
||||||
break;
|
|
||||||
case "redstone torch (off)":
|
|
||||||
material = Material.REDSTONE_TORCH_OFF;
|
|
||||||
break;
|
|
||||||
case "redstone torch (on)":
|
|
||||||
material = Material.REDSTONE_TORCH_ON;
|
|
||||||
break;
|
|
||||||
case "fence":
|
|
||||||
material = Material.FENCE;
|
|
||||||
break;
|
|
||||||
case "nether portal":
|
|
||||||
material = Material.PORTAL;
|
|
||||||
break;
|
|
||||||
case "iron bars":
|
|
||||||
material = Material.IRON_FENCE;
|
|
||||||
break;
|
|
||||||
case "glass pane":
|
|
||||||
material = Material.THIN_GLASS;
|
|
||||||
break;
|
|
||||||
case "fence gate":
|
|
||||||
material = Material.FENCE_GATE;
|
|
||||||
break;
|
|
||||||
case "nether brick fence":
|
|
||||||
material = Material.NETHER_FENCE;
|
|
||||||
break;
|
|
||||||
case "nether wart":
|
|
||||||
material = Material.NETHER_WARTS;
|
|
||||||
break;
|
|
||||||
case "end portal":
|
|
||||||
material = Material.ENDER_PORTAL;
|
|
||||||
break;
|
|
||||||
case "cobblestone wall":
|
|
||||||
material = Material.COBBLE_WALL;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new InvalidParameterException();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.material = material;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
switch (material) {
|
|
||||||
case AIR:
|
|
||||||
return "air";
|
|
||||||
case SAPLING:
|
|
||||||
return "sapling";
|
|
||||||
case STATIONARY_WATER:
|
|
||||||
return "water";
|
|
||||||
case STATIONARY_LAVA:
|
|
||||||
return "lava";
|
|
||||||
case WEB:
|
|
||||||
return "cobweb";
|
|
||||||
case LONG_GRASS:
|
|
||||||
return "grass";
|
|
||||||
case DEAD_BUSH:
|
|
||||||
return "dead bush";
|
|
||||||
case YELLOW_FLOWER:
|
|
||||||
return "dandelion";
|
|
||||||
case RED_ROSE:
|
|
||||||
return "poppy";
|
|
||||||
case BROWN_MUSHROOM:
|
|
||||||
return "brown mushroom";
|
|
||||||
case RED_MUSHROOM:
|
|
||||||
return "red mushroom";
|
|
||||||
case TORCH:
|
|
||||||
return "torch";
|
|
||||||
case REDSTONE_TORCH_OFF:
|
|
||||||
return "redstone torch (off)";
|
|
||||||
case REDSTONE_TORCH_ON:
|
|
||||||
return "redstone torch (on)";
|
|
||||||
case FENCE:
|
|
||||||
return "fence";
|
|
||||||
case PORTAL:
|
|
||||||
return "nether portal";
|
|
||||||
case IRON_FENCE:
|
|
||||||
return "iron bars";
|
|
||||||
case THIN_GLASS:
|
|
||||||
return "glass pane";
|
|
||||||
case FENCE_GATE:
|
|
||||||
return "fence gate";
|
|
||||||
case NETHER_FENCE:
|
|
||||||
return "nether brick fence";
|
|
||||||
case NETHER_WARTS:
|
|
||||||
return "nether wart";
|
|
||||||
case ENDER_PORTAL:
|
|
||||||
return "end portal";
|
|
||||||
case COBBLE_WALL:
|
|
||||||
return "cobblestone wall";
|
|
||||||
default:
|
|
||||||
return "nether portal";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public Material getMaterial() {
|
|
||||||
return material;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte getData(GateDirection direction) {
|
|
||||||
switch (material) {
|
|
||||||
case PORTAL:
|
|
||||||
return direction == GateDirection.EastWest ? (byte)0b0 : (byte)0b10;
|
|
||||||
case GRASS:
|
|
||||||
return 1;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
100
src/de/craftinc/gates/persistence/LocationUtil.java
Normal file
100
src/de/craftinc/gates/persistence/LocationUtil.java
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.persistence;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.World;
|
||||||
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
|
|
||||||
|
public class LocationUtil
|
||||||
|
{
|
||||||
|
protected final static String worldKey = "world";
|
||||||
|
protected final static String xKey = "x";
|
||||||
|
protected final static String yKey = "y";
|
||||||
|
protected final static String zKey = "z";
|
||||||
|
|
||||||
|
|
||||||
|
protected static World getWorld(final String name) throws Exception
|
||||||
|
{
|
||||||
|
if (name == null) {
|
||||||
|
throw new IllegalArgumentException("The name of the world must not be 'null");
|
||||||
|
}
|
||||||
|
|
||||||
|
World world = Plugin.getPlugin().getServer().getWorld(name);
|
||||||
|
|
||||||
|
if (world == null) {
|
||||||
|
throw new Exception("World '" + name + "' does not exists anymore! Cannot get instance!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Serializes a location. Helps storing locations inside yaml files. NOTE: We do not care about yaw
|
||||||
|
* and pitch for gate locations. So we won't serialize them.
|
||||||
|
*
|
||||||
|
* @param l The location to serialize. Supplying 'null' is ok..
|
||||||
|
* @return A Map object ready for storing inside a yaml file. Will return 'null' if 'l' is null.
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> serializeLocation(final Location l)
|
||||||
|
{
|
||||||
|
if (l == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Object> serializedLocation = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
serializedLocation.put(worldKey, l.getWorld().getName());
|
||||||
|
serializedLocation.put(xKey, l.getX());
|
||||||
|
serializedLocation.put(yKey, l.getY());
|
||||||
|
serializedLocation.put(zKey, l.getZ());
|
||||||
|
|
||||||
|
return serializedLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param map A map generated with the 'serializeLocation' method. Supplying 'null' is ok.
|
||||||
|
* @return A deserialized location. This method will return 'null' if 'map' is null!
|
||||||
|
* @throws Exception This method will throw an exception if the world of the supplied serialized location
|
||||||
|
* does not exist or if 'map' does not contain all necessary keys!
|
||||||
|
*/
|
||||||
|
public static Location deserializeLocation(final Map<String, Object> map) throws Exception
|
||||||
|
{
|
||||||
|
if (map == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
World w = getWorld((String)map.get(worldKey));
|
||||||
|
|
||||||
|
Number x = (Number)map.get(xKey);
|
||||||
|
Number y = (Number)map.get(yKey);
|
||||||
|
Number z = (Number)map.get(zKey);
|
||||||
|
|
||||||
|
if (x == null || y == null || z == null) {
|
||||||
|
throw new IllegalArgumentException("Supplied map is invalid x, y or z coordinate was not supplied");
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Location(w, x.doubleValue(), y.doubleValue(), z.doubleValue());
|
||||||
|
}
|
||||||
|
}
|
@ -16,7 +16,7 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.persistence;
|
package de.craftinc.gates.persistence;
|
||||||
|
|
||||||
import de.craftinc.gates.models.Gate;
|
import de.craftinc.gates.Gate;
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
import de.craftinc.gates.Plugin;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
@ -27,14 +27,48 @@ import java.util.List;
|
|||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
|
||||||
public class MigrationUtil {
|
public class MigrationUtil
|
||||||
|
{
|
||||||
|
public static boolean performMigration(int storageVersion, int currentVersion, List<Gate> gates)
|
||||||
|
{
|
||||||
|
if (storageVersion == 0 && currentVersion >= 2) {
|
||||||
|
removePortalBlocks(gates);
|
||||||
|
updateAllowVehicles(gates);
|
||||||
|
|
||||||
public static boolean performMigration(int storageVersion, int currentVersion, List<Gate> gates) {
|
|
||||||
if (storageVersion != currentVersion) {
|
|
||||||
Plugin.log(Level.SEVERE, "Supplied storage version is currently not supported!" +
|
|
||||||
"Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
else if (storageVersion == 1 && currentVersion >= 2) {
|
||||||
|
updateAllowVehicles(gates);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Plugin.log(Level.SEVERE, "Supplied storage version is currently not supported! Make sure you have the latest version of Craft Inc. Gates installed. Plugin will be disabled!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected static void removePortalBlocks(List<Gate> gates)
|
||||||
|
{
|
||||||
|
for (Gate g : gates) {
|
||||||
|
|
||||||
|
for (Location l : g.getGateBlockLocations()) {
|
||||||
|
Block b = l.getBlock();
|
||||||
|
|
||||||
|
if (b.getType() == Material.PORTAL) {
|
||||||
|
b.setType(Material.AIR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected static void updateAllowVehicles(List<Gate> gates)
|
||||||
|
{
|
||||||
|
for (Gate g : gates) {
|
||||||
|
|
||||||
|
g.setAllowsVehicles(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,15 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.util;
|
package de.craftinc.gates.util;
|
||||||
|
|
||||||
public class ConfigurationUtil {
|
|
||||||
|
import de.craftinc.gates.Plugin;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
|
||||||
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
|
||||||
|
public class ConfigurationUtil
|
||||||
|
{
|
||||||
public static final String confMaxGateBlocksKey = "maxGateBlocks";
|
public static final String confMaxGateBlocksKey = "maxGateBlocks";
|
||||||
public static final String confPlayerGateBlockUpdateRadiusKey = "playerGateBlockUpdateRadius";
|
public static final String confPlayerGateBlockUpdateRadiusKey = "playerGateBlockUpdateRadius";
|
||||||
public static final String confCheckForBrokenGateFramesKey = "checkForBrokenGateFrames";
|
public static final String confCheckForBrokenGateFramesKey = "checkForBrokenGateFrames";
|
||||||
@ -27,4 +35,93 @@ public class ConfigurationUtil {
|
|||||||
public static final String confShowTeleportNoPermissionMessageKey = "showTeleportNoPermissionMessage";
|
public static final String confShowTeleportNoPermissionMessageKey = "showTeleportNoPermissionMessage";
|
||||||
public static final String confSaveOnChangesKey = "saveOnChanges";
|
public static final String confSaveOnChangesKey = "saveOnChanges";
|
||||||
public static final String confHighlightDurationKey = "highlightDuration";
|
public static final String confHighlightDurationKey = "highlightDuration";
|
||||||
|
public static final String confGateMaterialKey = "gateMaterial";
|
||||||
|
|
||||||
|
|
||||||
|
public static GateMaterial getPortalMaterial()
|
||||||
|
{
|
||||||
|
String materialString = Plugin.getPlugin().getConfig().getString(confGateMaterialKey);
|
||||||
|
GateMaterial material = new GateMaterial();
|
||||||
|
|
||||||
|
if (materialString.equals("sapling")) {
|
||||||
|
material.material = Material.SAPLING;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("water")) {
|
||||||
|
material.material = Material.STATIONARY_WATER;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("lava")) {
|
||||||
|
material.material = Material.STATIONARY_LAVA;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("cobweb")) {
|
||||||
|
material.material = Material.WEB;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("grass")) {
|
||||||
|
material.material = Material.LONG_GRASS;
|
||||||
|
material.data = 1;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("dead bush")) {
|
||||||
|
material.material = Material.DEAD_BUSH;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("dandelion")) {
|
||||||
|
material.material = Material.YELLOW_FLOWER;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("poppy")) {
|
||||||
|
material.material = Material.RED_ROSE;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("brown mushroom")) {
|
||||||
|
material.material = Material.BROWN_MUSHROOM;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("red mushroom")) {
|
||||||
|
material.material = Material.RED_MUSHROOM;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("torch")) {
|
||||||
|
material.material = Material.TORCH;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("redstone torch (off)")) {
|
||||||
|
material.material = Material.REDSTONE_TORCH_OFF;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("redstone torch (on)")) {
|
||||||
|
material.material = Material.REDSTONE_TORCH_ON;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("fence")) {
|
||||||
|
material.material = Material.FENCE;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("nether portal")) {
|
||||||
|
material.material = Material.PORTAL;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("iron bars")) {
|
||||||
|
material.material = Material.IRON_FENCE;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("glass pane")) {
|
||||||
|
material.material = Material.THIN_GLASS;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("fence gate")) {
|
||||||
|
material.material = Material.FENCE_GATE;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("nether brick fence")) {
|
||||||
|
material.material = Material.NETHER_FENCE;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("nether wart")) {
|
||||||
|
material.material = Material.NETHER_WARTS;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("end portal")) {
|
||||||
|
material.material = Material.ENDER_PORTAL;
|
||||||
|
}
|
||||||
|
else if (materialString.equals("cobblestone wall")) {
|
||||||
|
material.material = Material.COBBLE_WALL;
|
||||||
|
}
|
||||||
|
else { // fallback!
|
||||||
|
material.material = Material.PORTAL;
|
||||||
|
Plugin.log(Level.WARNING, "Gate material invalid! Please check and correct your configuration file!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return material;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class GateMaterial
|
||||||
|
{
|
||||||
|
public Material material = Material.PORTAL;
|
||||||
|
public byte data = 0;
|
||||||
}
|
}
|
||||||
|
@ -28,11 +28,13 @@ import org.bukkit.block.BlockFace;
|
|||||||
import de.craftinc.gates.Plugin;
|
import de.craftinc.gates.Plugin;
|
||||||
|
|
||||||
|
|
||||||
public class FloodUtil {
|
public class FloodUtil
|
||||||
private static final Set<BlockFace> exp1 = new HashSet<>();
|
{
|
||||||
private static final Set<BlockFace> exp2 = new HashSet<>();
|
protected static final Set<BlockFace> exp1 = new HashSet<BlockFace>();
|
||||||
|
protected static final Set<BlockFace> exp2 = new HashSet<BlockFace>();
|
||||||
|
|
||||||
static {
|
static
|
||||||
|
{
|
||||||
exp1.add(BlockFace.UP);
|
exp1.add(BlockFace.UP);
|
||||||
exp1.add(BlockFace.DOWN);
|
exp1.add(BlockFace.DOWN);
|
||||||
exp1.add(BlockFace.EAST);
|
exp1.add(BlockFace.EAST);
|
||||||
@ -44,15 +46,16 @@ public class FloodUtil {
|
|||||||
exp2.add(BlockFace.SOUTH);
|
exp2.add(BlockFace.SOUTH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the all frame blocks of an gate.
|
* Returns the all frame blocks of an gate.
|
||||||
*
|
|
||||||
* @param blocks All blocks inside the gate.
|
* @param blocks All blocks inside the gate.
|
||||||
* @return A Set containing all frame block. Will never return 'null'.
|
* @return A Set containing all frame block. Will never return 'null'.
|
||||||
*/
|
*/
|
||||||
public static Set<Block> getFrame(final Set<Block> blocks) {
|
public static Set<Block> getFrame(final Set<Block> blocks)
|
||||||
|
{
|
||||||
if (blocks == null || blocks.isEmpty()) {
|
if (blocks == null || blocks.isEmpty()) {
|
||||||
return new HashSet<>();
|
return new HashSet<Block>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// try to find gate's direction (north-south or east-west)
|
// try to find gate's direction (north-south or east-west)
|
||||||
@ -78,7 +81,8 @@ public class FloodUtil {
|
|||||||
|
|
||||||
if (gateFrameSearchFaces != null) {
|
if (gateFrameSearchFaces != null) {
|
||||||
return _getFrame(blocks, gateFrameSearchFaces);
|
return _getFrame(blocks, gateFrameSearchFaces);
|
||||||
} else { // no direction found (the gate might only consist of blocks one over another)
|
}
|
||||||
|
else { // no direction found (the gate might only consist of blocks one over another)
|
||||||
|
|
||||||
// Try one direction and check if the found blocks are not air.
|
// Try one direction and check if the found blocks are not air.
|
||||||
// If air is found (frame broken or wrong direction) return the other direction
|
// If air is found (frame broken or wrong direction) return the other direction
|
||||||
@ -96,8 +100,10 @@ public class FloodUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static Set<Block> _getFrame(final Set<Block> blocks, final Set<BlockFace> searchDirections) {
|
|
||||||
Set<Block> frameBlocks = new HashSet<>();
|
protected static Set<Block> _getFrame(final Set<Block> blocks, final Set<BlockFace> searchDirections)
|
||||||
|
{
|
||||||
|
Set<Block> frameBlocks = new HashSet<Block>();
|
||||||
|
|
||||||
for (Block b : blocks) {
|
for (Block b : blocks) {
|
||||||
|
|
||||||
@ -116,16 +122,16 @@ public class FloodUtil {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the all frame blocks of an gate.
|
* Returns the all frame blocks of an gate.
|
||||||
*
|
|
||||||
* @param locations All locations inside the gate.
|
* @param locations All locations inside the gate.
|
||||||
* @return A Set containing all frame block. Will never return 'null'.
|
* @return A Set containing all frame block. Will never return 'null'.
|
||||||
*/
|
*/
|
||||||
public static Set<Block> getFrameWithLocations(final Set<Location> locations) {
|
public static Set<Block> getFrameWithLocations(final Set<Location> locations)
|
||||||
|
{
|
||||||
if (locations == null) {
|
if (locations == null) {
|
||||||
throw new IllegalArgumentException("'locations' must not be 'null'");
|
throw new IllegalArgumentException("'locations' must not be 'null'");
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<Block> blocks = new HashSet<>();
|
Set<Block> blocks = new HashSet<Block>();
|
||||||
|
|
||||||
for (Location l : locations) {
|
for (Location l : locations) {
|
||||||
blocks.add(l.getBlock());
|
blocks.add(l.getBlock());
|
||||||
@ -136,7 +142,8 @@ public class FloodUtil {
|
|||||||
|
|
||||||
|
|
||||||
// For the same frame and location this set of blocks is deterministic
|
// For the same frame and location this set of blocks is deterministic
|
||||||
public static Set<Block> getGatePortalBlocks(final Block block) {
|
public static Set<Block> getGatePortalBlocks(final Block block)
|
||||||
|
{
|
||||||
if (block == null) {
|
if (block == null) {
|
||||||
throw new IllegalArgumentException("'block' must not be 'null'");
|
throw new IllegalArgumentException("'block' must not be 'null'");
|
||||||
}
|
}
|
||||||
@ -166,10 +173,11 @@ public class FloodUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static Set<Block> getAirFloodBlocks(final Block startBlock,
|
protected static Set<Block> getAirFloodBlocks(final Block startBlock,
|
||||||
Set<Block> foundBlocks,
|
Set<Block> foundBlocks,
|
||||||
final Set<BlockFace> expandFaces,
|
final Set<BlockFace> expandFaces,
|
||||||
int limit) {
|
int limit)
|
||||||
|
{
|
||||||
if (foundBlocks == null) {
|
if (foundBlocks == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -184,8 +192,10 @@ public class FloodUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (startBlock.getType() == Material.AIR) {
|
if (startBlock.getType() == Material.AIR) {
|
||||||
|
// ... We found a block :D ...
|
||||||
foundBlocks.add(startBlock);
|
foundBlocks.add(startBlock);
|
||||||
|
|
||||||
|
// ... And flood away !
|
||||||
for (BlockFace face : expandFaces) {
|
for (BlockFace face : expandFaces) {
|
||||||
Block potentialBlock = startBlock.getRelative(face);
|
Block potentialBlock = startBlock.getRelative(face);
|
||||||
foundBlocks = getAirFloodBlocks(potentialBlock, foundBlocks, expandFaces, limit);
|
foundBlocks = getAirFloodBlocks(potentialBlock, foundBlocks, expandFaces, limit);
|
||||||
|
@ -16,10 +16,10 @@
|
|||||||
*/
|
*/
|
||||||
package de.craftinc.gates.util;
|
package de.craftinc.gates.util;
|
||||||
|
|
||||||
import de.craftinc.gates.Plugin;
|
|
||||||
import de.craftinc.gates.models.Gate;
|
|
||||||
|
|
||||||
import de.craftinc.gates.models.GateMaterial;
|
import de.craftinc.gates.Plugin;
|
||||||
|
import de.craftinc.gates.Gate;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -31,106 +31,34 @@ import java.util.Set;
|
|||||||
|
|
||||||
import static de.craftinc.gates.util.ConfigurationUtil.*;
|
import static de.craftinc.gates.util.ConfigurationUtil.*;
|
||||||
|
|
||||||
public class GateBlockChangeSender {
|
|
||||||
|
public class GateBlockChangeSender
|
||||||
|
{
|
||||||
/**
|
/**
|
||||||
* Replaces gate frame blocks with glowstone for a short period of time.
|
* Replaces gate frame blocks with glowstone for a short period of time.
|
||||||
* Uses the data stored in 'highlightDuration' inside the config file
|
* Uses the data stored in 'highlightDuration' inside the config file
|
||||||
* for determining when to de-highlight the frames.
|
* for determining when to de-highlight the frames.
|
||||||
*
|
|
||||||
* @param player The player for whom the frame should be highlighted.
|
* @param player The player for whom the frame should be highlighted.
|
||||||
* Must not be null!
|
* Must not be null!
|
||||||
*
|
|
||||||
* @param gates The gates to highlighting
|
|
||||||
*/
|
*/
|
||||||
public static void temporaryHighlightGatesFrames(final Player player, final Set<Gate> gates) {
|
public static void temporaryHighlightGatesFrames(final Player player, final Set<Gate> gates)
|
||||||
assert(player != null);
|
{
|
||||||
assert(gates != null);
|
if (player == null) {
|
||||||
|
throw new IllegalArgumentException("'player' must not be 'null'!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (gates == null) {
|
||||||
|
throw new IllegalArgumentException("'gate' must not be 'null!");
|
||||||
|
}
|
||||||
|
|
||||||
for (Gate g : gates) {
|
for (Gate g : gates) {
|
||||||
Set<Block> frameBlocks = g.getGateFrameBlocks();
|
Set<Block> frameBlocks = g.getGateFrameBlocks();
|
||||||
|
|
||||||
for (Block b : frameBlocks) {
|
for (Block b : frameBlocks) {
|
||||||
player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte) 0);
|
player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte)0);
|
||||||
}
|
|
||||||
}
|
|
||||||
scheduleDelighting(player, gates);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends gate blocks to player at a given location. Will send the updates either immediately or
|
|
||||||
* immediately and after a short delay.
|
|
||||||
*
|
|
||||||
* @param player A player to send block changes to. Must not be null!
|
|
||||||
* @param location The location to look for gates nearby. Must not be null!
|
|
||||||
* @param sendDelayed Set to 'true' if the block changes shall be send a second time after a one
|
|
||||||
* second delay.
|
|
||||||
*/
|
|
||||||
public static void updateGateBlocks(final Player player, final Location location, boolean sendDelayed) {
|
|
||||||
assert(player != null);
|
|
||||||
assert(location != null);
|
|
||||||
|
|
||||||
Set<Gate> gatesNearby = Plugin.getPlugin().getGatesManager().getNearbyGates(location.getChunk());
|
|
||||||
|
|
||||||
if (gatesNearby == null) {
|
|
||||||
return; // no gates nearby
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Gate g : gatesNearby) {
|
|
||||||
if (!g.isOpen()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
sendGateBlockChanges(g, true, player);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sendDelayed) {
|
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.getPlugin(), new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
updateGateBlocks(player, location, false);
|
|
||||||
}
|
|
||||||
}, 20L);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateGateBlocks(final Player player, final Location location) {
|
|
||||||
updateGateBlocks(player, location, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static void updateGateBlocks(final Gate gate) {
|
|
||||||
updateGateBlocks(gate, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends block changes to players near a given gate.
|
|
||||||
*
|
|
||||||
* @param gate Must not be 'null'!
|
|
||||||
* @param remove Set to true if all visible gate blocks shall be removed.
|
|
||||||
*/
|
|
||||||
public static void updateGateBlocks(final Gate gate, boolean remove) {
|
|
||||||
assert(gate != null);
|
|
||||||
|
|
||||||
Location gateLocation = gate.getLocation();
|
|
||||||
if (gate.getGateBlockLocations().isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<Player> playersNearby = new ArrayList<>();
|
|
||||||
int searchRadius = Plugin.getPlugin().getConfig().getInt(confPlayerGateBlockUpdateRadiusKey);
|
|
||||||
|
|
||||||
for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) {
|
|
||||||
if (p.getWorld() == gateLocation.getWorld() && p.getLocation().distance(gateLocation) < searchRadius) {
|
|
||||||
playersNearby.add(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean isVisible = gate.isOpen() && !remove;
|
|
||||||
for (Player p : playersNearby) {
|
|
||||||
sendGateBlockChanges(gate, isVisible, p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void scheduleDelighting(final Player player, final Set<Gate> gates) {
|
|
||||||
Plugin plugin = Plugin.getPlugin();
|
Plugin plugin = Plugin.getPlugin();
|
||||||
long highlightDuration = 20 * plugin.getConfig().getLong(confHighlightDurationKey);
|
long highlightDuration = 20 * plugin.getConfig().getLong(confHighlightDurationKey);
|
||||||
|
|
||||||
@ -142,33 +70,183 @@ public class GateBlockChangeSender {
|
|||||||
}, highlightDuration);
|
}, highlightDuration);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void dehighlightGatesFrames(final Player player, final Set<Gate> gates) {
|
|
||||||
|
public static void temporaryHighlightGateFrame(final Player player, final Gate gate)
|
||||||
|
{
|
||||||
|
if (gate == null) {
|
||||||
|
throw new IllegalArgumentException("'gate' must not be 'null!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player == null) {
|
||||||
|
throw new IllegalArgumentException("'player' must not be 'null'!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Block> frameBlocks = gate.getGateFrameBlocks();
|
||||||
|
|
||||||
|
for (Block b : frameBlocks) {
|
||||||
|
player.sendBlockChange(b.getLocation(), Material.GLOWSTONE, (byte)0);
|
||||||
|
}
|
||||||
|
|
||||||
|
Plugin plugin = Plugin.getPlugin();
|
||||||
|
long highlightDuration = 20 * plugin.getConfig().getLong(confHighlightDurationKey);
|
||||||
|
|
||||||
|
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
dehighlightGateFrame(player, gate);
|
||||||
|
}
|
||||||
|
}, highlightDuration);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected static void dehighlightGatesFrames(final Player player, final Set<Gate> gates)
|
||||||
|
{
|
||||||
for (Gate g : gates) {
|
for (Gate g : gates) {
|
||||||
Set<Block> frameBlocks = g.getGateFrameBlocks();
|
Set<Block> frameBlocks = g.getGateFrameBlocks();
|
||||||
|
|
||||||
for (Block b : frameBlocks) {
|
for (Block b : frameBlocks) {
|
||||||
player.sendBlockChange(b.getLocation(), b.getType(), (byte) 0);
|
player.sendBlockChange(b.getLocation(), b.getType(), (byte)0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void sendGateBlockChanges(final Gate gate, boolean isVisible, final Player p) {
|
|
||||||
byte data;
|
protected static void dehighlightGateFrame(final Player player, final Gate gate)
|
||||||
|
{
|
||||||
|
Set<Block> frameBlocks = gate.getGateFrameBlocks();
|
||||||
|
|
||||||
|
for (Block b : frameBlocks) {
|
||||||
|
player.sendBlockChange(b.getLocation(), b.getType(), (byte)0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends gate blocks to player at a given location. Will send the updates either immediately or
|
||||||
|
* immediately and after a short delay.
|
||||||
|
* @param player A player to send block changes to. Must not be null!
|
||||||
|
* @param location The location to look for gates nearby. Must not be null!
|
||||||
|
* @param sendDelayed Set to 'true' if the block changes shall be send a second time after a one
|
||||||
|
* second delay.
|
||||||
|
*/
|
||||||
|
public static void updateGateBlocks(final Player player, final Location location, boolean sendDelayed)
|
||||||
|
{
|
||||||
|
if (player == null) {
|
||||||
|
throw new IllegalArgumentException("'player' must not be 'null'!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (location == null) {
|
||||||
|
throw new IllegalArgumentException("'location' must not be 'null'!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Gate> gatesNearby = Plugin.getPlugin().getGatesManager().getNearbyGates(location.getChunk());
|
||||||
|
GateMaterial gateMaterial = getPortalMaterial();
|
||||||
|
|
||||||
|
if (gatesNearby == null) {
|
||||||
|
return; // no gates nearby
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Gate g : gatesNearby) {
|
||||||
|
|
||||||
|
if (!g.isOpen() || g.isHidden()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Location l : g.getGateBlockLocations()) {
|
||||||
|
|
||||||
|
if (l.getBlock().getType() == Material.AIR) {
|
||||||
|
player.sendBlockChange(l, gateMaterial.material, gateMaterial.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sendDelayed) {
|
||||||
|
Bukkit.getScheduler().scheduleSyncDelayedTask(Plugin.getPlugin(), new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
updateGateBlocks(player, location, false);
|
||||||
|
}
|
||||||
|
}, 20L);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method calls: updateGateBlocks(player, location, false);
|
||||||
|
*/
|
||||||
|
public static void updateGateBlocks(final Player player, final Location location)
|
||||||
|
{
|
||||||
|
updateGateBlocks(player, location, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method calls: updateGateBlocks(player, player.getLocation(), false);
|
||||||
|
*/
|
||||||
|
public static void updateGateBlocks(final Player player)
|
||||||
|
{
|
||||||
|
if (player == null) {
|
||||||
|
throw new IllegalArgumentException("'player' must not be 'null'!");
|
||||||
|
}
|
||||||
|
|
||||||
|
updateGateBlocks(player, player.getLocation(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static void updateGateBlocks(final Gate gate)
|
||||||
|
{
|
||||||
|
updateGateBlocks(gate, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends block changes to players near a given gate.
|
||||||
|
* @param gate Must not be 'null'!
|
||||||
|
* @param remove Set to true if all visible gate blocks shall be removed.
|
||||||
|
*/
|
||||||
|
public static void updateGateBlocks(final Gate gate, boolean remove)
|
||||||
|
{
|
||||||
|
if (gate == null) {
|
||||||
|
throw new IllegalArgumentException("'gate must not be 'null'!");
|
||||||
|
}
|
||||||
|
|
||||||
|
Location gateLocation = gate.getLocation();
|
||||||
|
|
||||||
|
if (gate.getGateBlockLocations().isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Player> playersNearby = new ArrayList<Player>();
|
||||||
|
|
||||||
|
int searchRadius = Plugin.getPlugin().getConfig().getInt(confPlayerGateBlockUpdateRadiusKey);
|
||||||
|
|
||||||
|
for (Player p : Plugin.getPlugin().getServer().getOnlinePlayers()) {
|
||||||
|
|
||||||
|
if (p.getWorld() == gateLocation.getWorld() && p.getLocation().distance(gateLocation) < searchRadius) {
|
||||||
|
playersNearby.add(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GateMaterial gateMaterial = getPortalMaterial();
|
||||||
Material material;
|
Material material;
|
||||||
|
byte data = 0;
|
||||||
|
|
||||||
if (isVisible) {
|
if (gate.isOpen() && !gate.isHidden() && !remove) {
|
||||||
GateMaterial gm = gate.getMaterial();
|
material = gateMaterial.material;
|
||||||
data = gm.getData(gate.getDirection());
|
data = gateMaterial.data;
|
||||||
material = gm.getMaterial();
|
}
|
||||||
} else {
|
else {
|
||||||
data = 0b0;
|
|
||||||
material = Material.AIR;
|
material = Material.AIR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (Player p : playersNearby) {
|
||||||
|
|
||||||
for (Location l : gate.getGateBlockLocations()) {
|
for (Location l : gate.getGateBlockLocations()) {
|
||||||
if (l.getBlock().getType() == Material.AIR) {
|
|
||||||
|
if (l.getBlock().getType() == Material.AIR) { // on server-side a gate is always made out of AIR
|
||||||
p.sendBlockChange(l, material, data);
|
p.sendBlockChange(l, material, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,24 +14,27 @@
|
|||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package de.craftinc.gates.models;
|
package de.craftinc.gates.util;
|
||||||
|
|
||||||
import org.bukkit.Chunk;
|
import org.bukkit.Chunk;
|
||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
|
|
||||||
public class SimpleChunk {
|
public class SimpleChunk
|
||||||
|
{
|
||||||
private int x;
|
private int x;
|
||||||
private int z;
|
private int z;
|
||||||
private String world;
|
private String world;
|
||||||
|
|
||||||
public SimpleChunk(Chunk c) {
|
public SimpleChunk(Chunk c)
|
||||||
|
{
|
||||||
this.x = c.getX();
|
this.x = c.getX();
|
||||||
this.z = c.getZ();
|
this.z = c.getZ();
|
||||||
this.world = c.getWorld().getName();
|
this.world = c.getWorld().getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public SimpleChunk(int x, int z, World w) {
|
public SimpleChunk(int x, int z, World w)
|
||||||
|
{
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.z = z;
|
this.z = z;
|
||||||
this.world = w.getName();
|
this.world = w.getName();
|
||||||
@ -39,9 +42,10 @@ public class SimpleChunk {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o)
|
||||||
|
{
|
||||||
if (o instanceof SimpleChunk) {
|
if (o instanceof SimpleChunk) {
|
||||||
SimpleChunk otherLocation = (SimpleChunk) o;
|
SimpleChunk otherLocation = (SimpleChunk)o;
|
||||||
|
|
||||||
if (otherLocation.x == this.x
|
if (otherLocation.x == this.x
|
||||||
&& otherLocation.z == this.z
|
&& otherLocation.z == this.z
|
||||||
@ -56,7 +60,8 @@ public class SimpleChunk {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode()
|
||||||
|
{
|
||||||
int hash = 11;
|
int hash = 11;
|
||||||
hash = 29 * hash + x;
|
hash = 29 * hash + x;
|
||||||
hash = 37 * hash + z;
|
hash = 37 * hash + z;
|
||||||
@ -67,7 +72,8 @@ public class SimpleChunk {
|
|||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString()
|
||||||
|
{
|
||||||
return this.getClass().toString() + " (x=" + this.x + " z=" + this.z + " world='" + this.world + "')";
|
return this.getClass().toString() + " (x=" + this.x + " z=" + this.z + " world='" + this.world + "')";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -14,17 +14,20 @@
|
|||||||
You should have received a copy of the GNU Lesser General Public License
|
You should have received a copy of the GNU Lesser General Public License
|
||||||
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package de.craftinc.gates.models;
|
package de.craftinc.gates.util;
|
||||||
|
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
|
||||||
public class SimpleLocation {
|
public class SimpleLocation
|
||||||
|
{
|
||||||
private String world;
|
private String world;
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
private int z;
|
private int z;
|
||||||
|
|
||||||
public SimpleLocation(Location l) {
|
|
||||||
|
public SimpleLocation(Location l)
|
||||||
|
{
|
||||||
this.world = l.getWorld().getName();
|
this.world = l.getWorld().getName();
|
||||||
|
|
||||||
// Using Block coordinates makes it possible to compare block locations with player locations.
|
// Using Block coordinates makes it possible to compare block locations with player locations.
|
||||||
@ -34,7 +37,9 @@ public class SimpleLocation {
|
|||||||
this.z = l.getBlockZ();
|
this.z = l.getBlockZ();
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleLocation(Location l, boolean isHeadPosition) {
|
|
||||||
|
public SimpleLocation(Location l, boolean isHeadPosition)
|
||||||
|
{
|
||||||
this.world = l.getWorld().getName();
|
this.world = l.getWorld().getName();
|
||||||
|
|
||||||
// Using Block coordinates makes it possible to compare block locations with player locations.
|
// Using Block coordinates makes it possible to compare block locations with player locations.
|
||||||
@ -48,15 +53,20 @@ public class SimpleLocation {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString()
|
||||||
|
{
|
||||||
return super.toString() + " x: " + x + " y: " + y + " z: " + z + " world: " + world;
|
return super.toString() + " x: " + x + " y: " + y + " z: " + z + " world: " + world;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(final Object o) {
|
public boolean equals(final Object o)
|
||||||
|
{
|
||||||
if (o instanceof SimpleLocation) {
|
if (o instanceof SimpleLocation) {
|
||||||
SimpleLocation otherLocation = (SimpleLocation) o;
|
SimpleLocation otherLocation = (SimpleLocation)o;
|
||||||
|
|
||||||
if (otherLocation.x == this.x
|
if (otherLocation.x == this.x
|
||||||
&& otherLocation.y == this.y
|
&& otherLocation.y == this.y
|
||||||
@ -70,8 +80,10 @@ public class SimpleLocation {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode()
|
||||||
|
{
|
||||||
int hash = 13;
|
int hash = 13;
|
||||||
hash = 37 * hash + x;
|
hash = 37 * hash + x;
|
||||||
hash = 31 * hash + y;
|
hash = 31 * hash + y;
|
@ -20,14 +20,16 @@ import org.bukkit.ChatColor;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class TextUtil {
|
public class TextUtil
|
||||||
|
{
|
||||||
public static String titleSize(String str) {
|
public static String titleize(String str)
|
||||||
|
{
|
||||||
String center = ".[ " + ChatColor.YELLOW + str + ChatColor.GOLD + " ].";
|
String center = ".[ " + ChatColor.YELLOW + str + ChatColor.GOLD + " ].";
|
||||||
|
|
||||||
if (center.length() >= 60) {
|
if (center.length() >= 60) {
|
||||||
return ChatColor.GOLD + center;
|
return ChatColor.GOLD + center;
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
String line = ChatColor.GOLD + repeat("_", 60);
|
String line = ChatColor.GOLD + repeat("_", 60);
|
||||||
|
|
||||||
int pivot = line.length() / 2;
|
int pivot = line.length() / 2;
|
||||||
@ -39,27 +41,32 @@ public class TextUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static String repeat(String s, int times) {
|
public static String repeat(String s, int times)
|
||||||
|
{
|
||||||
if (times <= 0)
|
if (times <= 0)
|
||||||
return "";
|
return "";
|
||||||
|
|
||||||
return s + repeat(s, times - 1);
|
return s + repeat(s, times-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Joins all elements of list into a single string, separating the original strings with glue.
|
* Joins all elements of list into a single string, separating the original strings with glue.
|
||||||
*/
|
*/
|
||||||
public static String implode(List<String> list, String glue) {
|
public static String implode(List<String> list, String glue)
|
||||||
|
{
|
||||||
if (list.size() == 0) {
|
if (list.size() == 0) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
String ret = list.get(0);
|
String ret = list.get(0);
|
||||||
|
|
||||||
for (int i = 1; i < list.size(); i++) {
|
for (int i=1; i<list.size(); i++) {
|
||||||
ret += glue + list.get(i);
|
ret += glue + list.get(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
113
src/de/craftinc/gates/util/VehicleCloner.java
Normal file
113
src/de/craftinc/gates/util/VehicleCloner.java
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
/* Craft Inc. Gates
|
||||||
|
Copyright (C) 2011-2013 Craft Inc. Gates Team (see AUTHORS.txt)
|
||||||
|
|
||||||
|
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 (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
package de.craftinc.gates.util;
|
||||||
|
|
||||||
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.entity.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class VehicleCloner
|
||||||
|
{
|
||||||
|
public static Vehicle clone(Vehicle parent, Location cloneLocation)
|
||||||
|
{
|
||||||
|
Vehicle clone = cloneLocation.getWorld().spawn(cloneLocation, parent.getClass());
|
||||||
|
|
||||||
|
clone.setFallDistance(parent.getFallDistance());
|
||||||
|
clone.setFireTicks(parent.getFireTicks());
|
||||||
|
clone.setVelocity(parent.getVelocity());
|
||||||
|
clone.setTicksLived(parent.getTicksLived());
|
||||||
|
clone.setLastDamageCause(parent.getLastDamageCause());
|
||||||
|
|
||||||
|
if (clone instanceof Boat) {
|
||||||
|
Boat boat = (Boat)clone;
|
||||||
|
Boat parentBoat = (Boat)parent;
|
||||||
|
|
||||||
|
boat.setMaxSpeed(parentBoat.getMaxSpeed());
|
||||||
|
boat.setOccupiedDeceleration(parentBoat.getOccupiedDeceleration());
|
||||||
|
boat.setUnoccupiedDeceleration(parentBoat.getUnoccupiedDeceleration());
|
||||||
|
boat.setWorkOnLand(parentBoat.getWorkOnLand());
|
||||||
|
boat.setVelocity(parentBoat.getVelocity());
|
||||||
|
}
|
||||||
|
else if (clone instanceof Animals) {
|
||||||
|
Animals animal = (Animals)clone;
|
||||||
|
Animals parentAnimal = (Animals)parent;
|
||||||
|
|
||||||
|
animal.setMaxHealth(parentAnimal.getMaxHealth());
|
||||||
|
animal.setHealth(parentAnimal.getMaxHealth());
|
||||||
|
animal.setRemainingAir(parentAnimal.getRemainingAir());
|
||||||
|
animal.setMaximumAir(parentAnimal.getMaximumAir());
|
||||||
|
animal.setMaximumNoDamageTicks(parentAnimal.getMaximumNoDamageTicks());
|
||||||
|
animal.setLastDamage(parentAnimal.getLastDamage());
|
||||||
|
animal.setNoDamageTicks(parentAnimal.getNoDamageTicks());
|
||||||
|
animal.addPotionEffects(parentAnimal.getActivePotionEffects());
|
||||||
|
animal.setRemoveWhenFarAway(parentAnimal.getRemoveWhenFarAway());
|
||||||
|
animal.setCanPickupItems(parentAnimal.getCanPickupItems());
|
||||||
|
animal.setCustomName(parentAnimal.getCustomName());
|
||||||
|
animal.setCustomNameVisible(parentAnimal.isCustomNameVisible());
|
||||||
|
animal.setTarget(parentAnimal.getTarget());
|
||||||
|
animal.setAge(parentAnimal.getAge());
|
||||||
|
animal.setAgeLock(parentAnimal.getAgeLock());
|
||||||
|
|
||||||
|
if (clone instanceof Horse) {
|
||||||
|
Horse horse = (Horse)clone;
|
||||||
|
Horse parentHorse = (Horse)parent;
|
||||||
|
|
||||||
|
horse.getInventory().setArmor(parentHorse.getInventory().getArmor());
|
||||||
|
horse.getInventory().setSaddle(parentHorse.getInventory().getSaddle());
|
||||||
|
horse.setCarryingChest(parentHorse.isCarryingChest());
|
||||||
|
horse.getInventory().setContents(parentHorse.getInventory().getContents());
|
||||||
|
horse.setTamed(parentHorse.isTamed());
|
||||||
|
horse.setOwner(parentHorse.getOwner());
|
||||||
|
horse.setJumpStrength(parentHorse.getJumpStrength());
|
||||||
|
horse.setMaxDomestication(parentHorse.getMaxDomestication());
|
||||||
|
horse.setDomestication(parentHorse.getDomestication());
|
||||||
|
horse.setStyle(parentHorse.getStyle());
|
||||||
|
horse.setColor(parentHorse.getColor());
|
||||||
|
horse.setVariant(parentHorse.getVariant());
|
||||||
|
|
||||||
|
if (parentHorse.isAdult()) {
|
||||||
|
horse.setAdult();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
horse.setBaby();
|
||||||
|
}
|
||||||
|
|
||||||
|
horse.setBreed(parentHorse.canBreed());
|
||||||
|
}
|
||||||
|
else if (clone instanceof Pig) {
|
||||||
|
Pig pig = (Pig)clone;
|
||||||
|
Pig parentPig = (Pig)parent;
|
||||||
|
|
||||||
|
pig.setSaddle(parentPig.hasSaddle());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (clone instanceof Minecart) {
|
||||||
|
Minecart minecart = (Minecart)clone;
|
||||||
|
Minecart parentMinecart = (Minecart)parent;
|
||||||
|
|
||||||
|
minecart.setDerailedVelocityMod(parentMinecart.getDerailedVelocityMod());
|
||||||
|
minecart.setFlyingVelocityMod(parentMinecart.getFlyingVelocityMod());
|
||||||
|
minecart.setSlowWhenEmpty(parentMinecart.isSlowWhenEmpty());
|
||||||
|
minecart.setMaxSpeed(parentMinecart.getMaxSpeed());
|
||||||
|
minecart.setDamage(parentMinecart.getDamage());
|
||||||
|
minecart.setVelocity(parentMinecart.getVelocity());
|
||||||
|
}
|
||||||
|
|
||||||
|
return clone;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user