Changed portals to per-world file

Lotsa null checks
Better blox.equals
This commit is contained in:
Drakia 2011-03-06 20:24:31 -08:00
parent a7f6d37c53
commit 352c6e4357
6 changed files with 82 additions and 77 deletions

10
README
View File

@ -13,7 +13,8 @@ Known Issues
=============
- Vehicle implementation is nowhere near done.
- Signs aren't always updating, I don't know what's causing this, I think it's a Bukkit thing.
- Minecraft bug is causing buttons to look odd, they still work though.
- There are many bugs with portal material not showing properly. This is a bug I can not track down, and have no fix for at the moment.
- Multi-world support is VERY BETA. Please don't just say "it doesn't work" I will ignore you.
=============
Permissions
@ -75,12 +76,17 @@ portal-create-message - The message when a gate is created
not-owner-message - The message when you aren't allowed to push the gate button
other-side-blocked-message - The message when the gate you're dialing is open
teleport-message - The message when you are teleported
portal-save-location - The file your portal database is saved as
portal-folder - The folder your portal databases are saved in
gate-folder - The folder containing your .gate files
=============
Changes
=============
[Version 0.20]
- Fixed the bug SIGN_CHANGE exception when using plugins such as Lockette
[Version 0.19]
- Set button facing on new gates, fixes weirdass button glitch
- Beginning of very buggy multi-world support
[Version 0.18]
- Small permissions handling update.
[Version 0.17]

View File

@ -121,6 +121,6 @@ public class Blox {
if (getClass() != obj.getClass()) return false;
Blox blox = (Blox) obj;
return (x == blox.x) && (y == blox.y) && (z == blox.z) && (world == blox.world);
return (x == blox.x) && (y == blox.y) && (z == blox.z) && (world.getName().equals(blox.world.getName()));
}
}

View File

@ -63,7 +63,6 @@ public class Portal {
private boolean priv = false;
private World world;
private long openTime;
private boolean loaded = false;
private Portal(Blox topLeft, int modX, int modZ,
float rotX, SignPost id, Blox button,
@ -115,10 +114,6 @@ public class Portal {
return priv;
}
public boolean isLoaded() {
return loaded;
}
public boolean open(boolean force) {
return open(null, force);
}
@ -254,35 +249,11 @@ public class Portal {
public Location getExit(Location traveller, Portal origin) {
Location loc = null;
// Check if the gate has an exit block
if (gate.getExit() != null) {
Blox exit = getBlockAt(gate.getExit());
loc = exit.modRelativeLoc(0D, 0D, 1D, traveller.getYaw(), traveller.getPitch(), modX, 1, modZ);
} else {
// Move the "entrance" to the first portal block up at the current x/z
// "Exits" seem to only consist of the lowest Y coord
int bX = traveller.getBlockX();
int bY = traveller.getBlockY();
int bZ = traveller.getBlockZ();
while (traveller.getWorld().getBlockTypeIdAt(bX, bY, bZ) == gate.getPortalBlockOpen())
bY --;
bY++;
// End
Blox entrance = new Blox(world, bX, bY, bZ);
HashMap<Blox, Integer> originExits = origin.getExits();
HashMap<Blox, Integer> destExits = this.getExits();
if (originExits.containsKey(entrance)) {
int position = (int)(((float)originExits.get(entrance) / originExits.size()) * destExits.size());
Blox exit = getReverseExits().get(position);
// Workaround for different size gates. Just drop them at the first exit block.
if (exit == null) {
exit = (Blox)getReverseExits().values().toArray()[0];
}
if (exit != null) {
loc = exit.modRelativeLoc(0D, 0D, 1D, traveller.getYaw(), traveller.getPitch(), modX, 1, modZ);
}
}
if (gate.getExit() != null) {
Blox exit = getBlockAt(gate.getExit());
loc = exit.modRelativeLoc(0D, 0D, 1D, traveller.getYaw(), traveller.getPitch(), modX, 1, modZ);
} else {
Stargate.log.log(Level.WARNING, "[Stargate] Missing destination point in .gate file " + gate.getFilename());
}
if (loc != null) {
Block block = world.getBlockAt(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ());
@ -294,8 +265,6 @@ public class Portal {
loc.setPitch(traveller.getPitch());
return loc;
}
Stargate.log.log(Level.WARNING, "No position found calculating route from " + this + " to " + origin);
return traveller;
}
@ -568,7 +537,7 @@ public class Portal {
}
}
saveAllGates();
saveAllGates(world);
}
private Blox getBlockAt(int right, int depth) {
@ -615,6 +584,7 @@ public class Portal {
public static Portal createPortal(SignPost id, Player player) {
Block idParent = id.getParent();
if (idParent == null) return null;
if (Gate.getGatesByControlBlock(idParent).length == 0) return null;
Blox parent = new Blox(player.getWorld(), idParent.getX(), idParent.getY(), idParent.getZ());
@ -642,19 +612,24 @@ public class Portal {
int modX = 0;
int modZ = 0;
float rotX = 0f;
int facing = 0;
if (idParent.getX() > id.getBlock().getX()) {
modZ -= 1;
rotX = 90f;
facing = 2;
} else if (idParent.getX() < id.getBlock().getX()) {
modZ += 1;
rotX = 270f;
facing = 1;
} else if (idParent.getZ() > id.getBlock().getZ()) {
modX += 1;
rotX = 180f;
facing = 4;
} else if (idParent.getZ() < id.getBlock().getZ()) {
modX -= 1;
rotX = 0f;
facing = 3;
}
Gate[] possibleGates = Gate.getGatesByControlBlock(idParent);
@ -698,6 +673,7 @@ public class Portal {
if (!alwaysOn) {
button = topleft.modRelative(buttonVector.getRight(), buttonVector.getDepth(), buttonVector.getDistance() + 1, modX, 1, modZ);
button.setType(BUTTON);
button.setData(facing);
}
portal = new Portal(topleft, modX, modZ, rotX, id, button, destName, name, true, network, gate, player.getName(), hidden, alwaysOn, priv);
@ -715,7 +691,7 @@ public class Portal {
origin.open(true);
}
saveAllGates();
saveAllGates(topleft.getWorld());
return portal;
}
@ -738,13 +714,14 @@ public class Portal {
return lookupBlocks.get(new Blox(block));
}
public static void saveAllGates() {
String loc = Stargate.getSaveLocation();
public static void saveAllGates(World world) {
String loc = Stargate.getSaveLocation() + File.separator + world.getName() + ".db";
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(loc, false));
for (Portal portal : allPortals) {
if (!portal.world.getName().equals(world.getName())) continue;
StringBuilder builder = new StringBuilder();
Blox sign = new Blox(portal.id.getBlock());
Blox button = portal.button;
@ -790,19 +767,24 @@ public class Portal {
}
}
public static void loadAllGates(World world) {
String location = Stargate.getSaveLocation();
public static void clearGates() {
lookupBlocks.clear();
lookupNamesNet.clear();
lookupEntrances.clear();
allPortals.clear();
allPortalsNet.clear();
}
if (new File(location).exists()) {
public static void loadAllGates(World world) {
String location = Stargate.getSaveLocation();
File db = new File(location, world.getName() + ".db");
if (db.exists()) {
int l = 0;
int portalCount = 0;
try {
Scanner scanner = new Scanner(new File(location));
Scanner scanner = new Scanner(db);
while (scanner.hasNextLine()) {
l++;
String line = scanner.nextLine().trim();
@ -841,9 +823,10 @@ public class Portal {
if (!portal.isVerified() || !portal.checkIntegrity()) {
portal.close(true);
portal.unregister();
Stargate.log.info("Destroying stargate at " + portal.toString());
Stargate.log.info("[Stargate] Destroying stargate at " + portal.toString());
} else {
portal.drawSign();
portalCount++;
}
}
@ -862,11 +845,13 @@ public class Portal {
OpenCount++;
}
}
Stargate.log.info("[Stargate] Loaded " + allPortals.size() + " stargates with " + OpenCount + " set as always-on");
Stargate.log.info("[Stargate] {" + world.getName() + "} Loaded " + portalCount + " stargates with " + OpenCount + " set as always-on");
} catch (Exception e) {
Stargate.log.log(Level.SEVERE, "Exception while reading stargates from " + location + ": " + l);
e.printStackTrace();
}
} else {
Stargate.log.info("[Stargate] {" + world.getName() + "} No stargates for world ");
}
}

View File

@ -24,6 +24,7 @@ public class SignPost {
public Block getParent() {
if (parent == null) findParent();
if (parent == null) return null;
return parent.getBlock();
}
@ -61,8 +62,11 @@ public class SignPost {
public void update() {
Sign sign = findSign();
if (sign == null) return;
sign.update();
if (sign == null) {
Stargate.log.info("[Stargate::SignPost::update] Sign null");
return;
}
sign.update(true);
}
private void findParent() {

View File

@ -7,6 +7,7 @@ import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockDamageLevel;
import org.bukkit.entity.Entity;
@ -54,7 +55,8 @@ public class Stargate extends JavaPlugin {
public static Logger log;
private Configuration config;
private PluginManager pm;
private static String portalFile;
private static String portalFolder;
private static String gateFolder;
private static String teleMsg = "Teleported";
private static String regMsg = "Gate Created";
@ -65,12 +67,14 @@ public class Stargate extends JavaPlugin {
private static String defNetwork = "central";
private static int activeLimit = 10;
private static int openLimit = 10;
public static ConcurrentLinkedQueue<Portal> openList = new ConcurrentLinkedQueue<Portal>();
public static ConcurrentLinkedQueue<Portal> activeList = new ConcurrentLinkedQueue<Portal>();
//private HashMap<Integer, Location> vehicles = new HashMap<Integer, Location>();
public void onDisable() {
Portal.closeAllGates();
Portal.clearGates();
}
public void onEnable() {
@ -80,7 +84,7 @@ public class Stargate extends JavaPlugin {
log = Logger.getLogger("Minecraft");
// Set portalFile and gateFolder to the plugin folder as defaults.
portalFile = getDataFolder() + File.separator + "stargate.db";
portalFolder = getDataFolder() + File.separator + "portals";
gateFolder = getDataFolder() + File.separator + "gates" + File.separator;
log.info(pdfFile.getName() + " v." + pdfFile.getVersion() + " is enabled.");
@ -91,6 +95,7 @@ public class Stargate extends JavaPlugin {
this.reloadConfig();
this.migrate();
this.reloadGates();
if (!this.setupPermissions()) {
log.info("[Stargate] Permissions not loaded, using defaults");
} else {
@ -114,7 +119,7 @@ public class Stargate extends JavaPlugin {
public void reloadConfig() {
config.load();
portalFile = config.getString("portal-save-location", portalFile);
portalFolder = config.getString("portal-folder", portalFolder);
gateFolder = config.getString("gate-folder", gateFolder);
teleMsg = config.getString("teleport-message", teleMsg);
regMsg = config.getString("portal-create-message", regMsg);
@ -127,7 +132,7 @@ public class Stargate extends JavaPlugin {
}
public void saveConfig() {
config.setProperty("portal-save-location", portalFile);
config.setProperty("portal-folder", portalFolder);
config.setProperty("gate-folder", gateFolder);
config.setProperty("teleport-message", teleMsg);
config.setProperty("portal-create-message", regMsg);
@ -141,24 +146,29 @@ public class Stargate extends JavaPlugin {
public void reloadGates() {
Gate.loadGates(gateFolder);
Portal.loadAllGates(this.getServer().getWorlds().get(0));
// Replace nethergate.gate if it doesn't have an exit point.
if (Gate.getGateByName("nethergate.gate").getExit() == null) {
Gate.populateDefaults(gateFolder);
}
for (World world : getServer().getWorlds()) {
Portal.loadAllGates(world);
}
}
private void migrate() {
// Only migrate if new file doesn't exist.
File newFile = new File(portalFile);
File newPortalDir = new File(portalFolder);
if (!newPortalDir.exists()) {
newPortalDir.mkdirs();
}
File newFile = new File(portalFolder, getServer().getWorlds().get(0).getName() + ".db");
if (!newFile.exists()) {
// Migrate REALLY old stargates if applicable
File olderFile = new File("stargates.txt");
if (olderFile.exists()) {
Stargate.log.info("[Stargate] Migrated old stargates.txt");
olderFile.renameTo(newFile);
}
// Migrate old stargates if applicable.
File oldFile = new File("stargates/locations.dat");
if (oldFile.exists()) {
Stargate.log.info("[Stargate] Migrated existing locations.dat");
oldFile.renameTo(newFile);
// Migrate not-so-old stargate db
File oldishFile = new File("plugins/Stargate/stargate.db");
if (oldishFile.exists()) {
Stargate.log.info("[Stargate] Migrating existing stargate.db");
oldishFile.renameTo(newFile);
}
}
@ -169,13 +179,13 @@ public class Stargate extends JavaPlugin {
if (!newDir.exists()) newDir.mkdirs();
for (File file : oldDir.listFiles(new Gate.StargateFilenameFilter())) {
Stargate.log.info("[Stargate] Migrating existing gate " + file.getName());
file.renameTo(new File(gateFolder + file.getName()));
file.renameTo(new File(gateFolder, file.getName()));
}
}
}
public static String getSaveLocation() {
return portalFile;
return portalFolder;
}
public static String getDefaultNetwork() {
@ -216,8 +226,6 @@ public class Stargate extends JavaPlugin {
*/
private boolean setupPermissions() {
Plugin perm;
// Apparently GM isn't a new permissions plugin, it's Permissions "2.0.1"
// API change broke my plugin.
perm = pm.getPlugin("Permissions");
// We're running Permissions
if (perm != null) {
@ -325,6 +333,8 @@ public class Stargate extends JavaPlugin {
public void onSignChange(SignChangeEvent event) {
Player player = event.getPlayer();
Block block = event.getBlock();
if (block.getType() != Material.WALL_SIGN) return;
// Initialize a stargate
if (hasPerm(player, "stargate.create", player.isOp())) {
SignPost sign = new SignPost(new Blox(block));
@ -339,7 +349,7 @@ public class Stargate extends JavaPlugin {
if (!regMsg.isEmpty()) {
player.sendMessage(ChatColor.GREEN + regMsg);
}
log.info("Initialized stargate: " + portal.getName());
log.info("[Stargate] Initialized stargate: " + portal.getName());
portal.drawSign();
// Set event text so our new sign is instantly initialized
event.setLine(0, sign.getText(0));
@ -438,7 +448,7 @@ public class Stargate extends JavaPlugin {
private class wListener extends WorldListener {
@Override
public void onWorldLoaded(WorldEvent event) {
//Portal.loadQueue(event.getWorld());
Portal.loadAllGates(event.getWorld());
}
}

View File

@ -1,6 +1,6 @@
name: Stargate
main: net.TheDgtl.Stargate.Stargate
version: 0.18
version: 0.20
description: Stargate mod for Bukkit
author: Drakia
website: http://www.thedgtl.net