Skip to content

Commit

Permalink
Formatting.
Browse files Browse the repository at this point in the history
Fixed spawning issue on server startup - spawning of items is now delayed for 1 second after the server loaded, so the items will have the correct position.
Signed-off-by: narrowtux <narrow.m@gmail.com>
  • Loading branch information
narrowtux committed Feb 11, 2012
1 parent f101877 commit d8077e8
Show file tree
Hide file tree
Showing 21 changed files with 160 additions and 290 deletions.
9 changes: 3 additions & 6 deletions src/main/java/com/narrowtux/showcase/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ public int getMaximumPerUser() {

public Configuration() {
File pluginFolder = Showcase.instance.getDataFolder();
reader = new FlatFileReader(new File(pluginFolder.getAbsolutePath()
+ "/showcase.cfg"), false);
reader = new FlatFileReader(new File(pluginFolder.getAbsolutePath() + "/showcase.cfg"), false);
load();
reader.write();
}
Expand All @@ -96,8 +95,7 @@ public void load() {
removeWhenEmpty = reader.getBoolean("removewhenempty", false);
locale = reader.getString("locale", "en-US");
autoSaveInterval = reader.getInteger("autosaveinterval", 60);
showAutosaveNotification = reader.getBoolean("autosavenotification",
false);
showAutosaveNotification = reader.getBoolean("autosavenotification", false);
useSpout = reader.getBoolean("usespout", false);
basicUsesItem = reader.getBoolean("basicusesitem", false);
maxStackSize.clear();
Expand All @@ -113,8 +111,7 @@ private void loadMaxStackSize() {
File file = new File(Showcase.instance.getDataFolder(), "stacks.csv");
if (!file.exists()) {
try {
Showcase.instance.copyFromJarToDisk("stacks.csv",
Showcase.instance.getDataFolder());
Showcase.instance.copyFromJarToDisk("stacks.csv", Showcase.instance.getDataFolder());
} catch (IOException e) {
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/narrowtux/showcase/DropChestListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

import com.narrowtux.dropchest.api.DropChestSuckEvent;

public class DropChestListener extends
com.narrowtux.dropchest.api.DropChestListener {
public class DropChestListener extends com.narrowtux.dropchest.api.DropChestListener {
@Override
public void onDropChestSuck(DropChestSuckEvent event) {
if (Showcase.instance.isShowcaseItem(event.getItem())) {
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/narrowtux/showcase/FlatFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ private boolean load() {
e.printStackTrace();
}
} else {
System.out
.println("File " + file.getAbsoluteFile() + " not found.");
System.out.println("File " + file.getAbsoluteFile() + " not found.");
return false;
}
return true;
Expand All @@ -217,16 +216,13 @@ public void write() {
try {
file.createNewFile();
} catch (IOException e) {
System.out.println("Could not create Datafile (" + e.getCause()
+ "). Aborting.");
System.out.println("Could not create Datafile (" + e.getCause() + "). Aborting.");
return;
}
}
try {
FileOutputStream output = new FileOutputStream(
file.getAbsoluteFile());
BufferedWriter w = new BufferedWriter(
new OutputStreamWriter(output));
FileOutputStream output = new FileOutputStream(file.getAbsoluteFile());
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(output));
w.write(finalFile);
w.flush();
output.close();
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/narrowtux/showcase/ItemSpawner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.narrowtux.showcase;

/**
* This class is used one second after onEnable() to prevent that the items get spawned weirdly.
* @author tux
*
*/
public class ItemSpawner extends Object implements Runnable {

public void run() {
for(ShowcaseItem item:Showcase.instance.showcasedItems) {
if(item.getItem() == null && item.isChunkLoaded()) {
item.respawn();
}
}
Showcase.instance.startup = false;
}
}
120 changes: 44 additions & 76 deletions src/main/java/com/narrowtux/showcase/Showcase.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,24 +80,22 @@ public class Showcase extends JavaPlugin {

private final static int INITIAL_CAPACITY = 500;

List<ShowcaseItem> showcasedItems = new ArrayList<ShowcaseItem>(
INITIAL_CAPACITY);
HashMap<Integer, ShowcaseItem> itemsByDrop = new HashMap<Integer, ShowcaseItem>(
INITIAL_CAPACITY);
List<ShowcaseItem> showcasedItems = new ArrayList<ShowcaseItem>(INITIAL_CAPACITY);
HashMap<Integer, ShowcaseItem> itemsByDrop = new HashMap<Integer, ShowcaseItem>(INITIAL_CAPACITY);
private ItemWatcher watcher = new ItemWatcher();
public Configuration config;
public WorldGuardPlugin worldguard;
public int autosaverId = -1;
public Map<String, ShowcaseProvider> providers = new HashMap<String, ShowcaseProvider>();
WorldGuardPlugin worldguard;
int autosaverId = -1;
Map<String, ShowcaseProvider> providers = new HashMap<String, ShowcaseProvider>();
private Translation trans;
private OddItem odditem = null;
boolean startup = true;

public void onDisable() {
getServer().getScheduler().cancelTasks(this);
// Read plugin file
PluginDescriptionFile pdfFile = this.getDescription();
String logText = tr("disableMessage", pdfFile.getName(),
pdfFile.getVersion());
String logText = tr("disableMessage", pdfFile.getName(), pdfFile.getVersion());
save();
for (ShowcaseItem item : showcasedItems) {
item.remove();
Expand All @@ -121,8 +119,7 @@ public void onEnable() {
dclistener = null;
}
try {
worldguard = (WorldGuardPlugin) getServer().getPluginManager()
.getPlugin("WorldGuard");
worldguard = (WorldGuardPlugin) getServer().getPluginManager().getPlugin("WorldGuard");
} catch (Exception e) {
worldguard = null;
}
Expand All @@ -149,8 +146,7 @@ public void onEnable() {
if (e instanceof Item) {
Location loc = e.getLocation();
Block b = loc.getBlock();
if (b.getType().equals(Material.GLASS)
|| b.getType().equals(Material.STEP)) {
if (b.getType().equals(Material.GLASS) || b.getType().equals(Material.STEP)) {
e.remove();
}
}
Expand All @@ -173,21 +169,15 @@ public void onEnable() {

odditem = (OddItem) pm.getPlugin("OddItem");

trans.reload(new File(getDataFolder(), "showcase-" + config.getLocale()
+ ".csv"));
trans.reload(new File(getDataFolder(), "showcase-" + config.getLocale() + ".csv"));

if (trans.getVersion() < 5) {
try {
copyFromJarToDisk("showcase-" + config.getLocale() + ".csv",
getDataFolder());
log.log(Level.INFO,
"[Showcase] copied new translation file for "
+ config.getLocale() + " to disk.");
trans.reload(new File(getDataFolder(), "showcase-"
+ config.getLocale() + ".csv"));
copyFromJarToDisk("showcase-" + config.getLocale() + ".csv", getDataFolder());
log.log(Level.INFO, "[Showcase] copied new translation file for " + config.getLocale() + " to disk.");
trans.reload(new File(getDataFolder(), "showcase-" + config.getLocale() + ".csv"));
} catch (IOException e) {
System.out
.println("Unable to copy default translation to plugin folder");
System.out.println("Unable to copy default translation to plugin folder");
}
}

Expand All @@ -199,25 +189,24 @@ public void onEnable() {
registerProvider(new ExchangeShowcase());
registerProvider(new TutorialShowcase());
// registerProvider(new SellShowcase());

getServer().getScheduler().scheduleSyncDelayedTask(this, new ItemSpawner(), 20);

getServer().getScheduler().scheduleSyncRepeatingTask(this, watcher, 0,
40);
getServer().getScheduler().scheduleSyncRepeatingTask(this, watcher, 10, 40);
setupPermissions();

if (config.getAutosaveInterval() != -1) {
getServer().getScheduler().scheduleSyncRepeatingTask(this,
new Runnable() {
public void run() {
save();
if (config.isShowingAutosaveNotification()) {
log.log(Level.INFO, "[Showcase] Autosaved");
}
}
}, 0, config.getAutosaveInterval() * 20);
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
save();
if (config.isShowingAutosaveNotification()) {
log.log(Level.INFO, "[Showcase] Autosaved");
}
}
}, 0, config.getAutosaveInterval() * 20);
}

String logText = trans.tr("enableMessage", pdfFile.getName(),
pdfFile.getVersion());
String logText = trans.tr("enableMessage", pdfFile.getName(), pdfFile.getVersion());
log.log(Level.INFO, logText);
}

Expand All @@ -226,11 +215,7 @@ private void checkForLibs() {
if (pm.getPlugin("NarrowtuxLib") == null) {
try {
File toPut = new File("plugins/NarrowtuxLib.jar");
download(
getServer().getLogger(),
new URL(
"http://tetragaming.com/narrowtux/plugins/NarrowtuxLib.jar"),
toPut);
download(getServer().getLogger(), new URL("http://tetragaming.com/narrowtux/plugins/NarrowtuxLib.jar"), toPut);
pm.loadPlugin(toPut);
pm.enablePlugin(pm.getPlugin("NarrowtuxLib"));
} catch (Exception exception) {
Expand All @@ -257,8 +242,7 @@ public static void download(Logger log, URL url, File file) throws IOException {
out.write(buffer, 0, len);
downloaded += len;
if ((int) ((System.currentTimeMillis() - start) / 500) > msgs) {
log.info((int) ((double) downloaded / (double) size * 100d)
+ "%");
log.info((int) ((double) downloaded / (double) size * 100d) + "%");
msgs++;
}
}
Expand Down Expand Up @@ -291,16 +275,13 @@ public void save() {
try {
datafile.createNewFile();
} catch (IOException e) {
System.out.println("Could not create Datafile (" + e.getCause()
+ "). Aborting.");
System.out.println("Could not create Datafile (" + e.getCause() + "). Aborting.");
return;
}
}
try {
FileOutputStream output = new FileOutputStream(
datafile.getAbsoluteFile());
BufferedWriter w = new BufferedWriter(
new OutputStreamWriter(output));
FileOutputStream output = new FileOutputStream(datafile.getAbsoluteFile());
BufferedWriter w = new BufferedWriter(new OutputStreamWriter(output));
for (ShowcaseItem item : showcasedItems) {
try {
String line = "";
Expand All @@ -311,8 +292,7 @@ public void save() {
String showtype = item.getType();
// Save
// x,y,z,itemid,player,worldname,worldenviromnent,showtype
line += loc.getBlockX() + "," + loc.getBlockY() + ","
+ loc.getBlockZ() + ",";
line += loc.getBlockX() + "," + loc.getBlockY() + "," + loc.getBlockZ() + ",";
line += type.getId() + "," + data + ",";
line += player + ",";
line += loc.getWorld().getName() + ",";
Expand Down Expand Up @@ -366,8 +346,7 @@ public void load() {
x = Integer.valueOf(line[0]);
y = Integer.valueOf(line[1]);
z = Integer.valueOf(line[2]);
Material type = Material.getMaterial(Integer
.valueOf(line[3]));
Material type = Material.getMaterial(Integer.valueOf(line[3]));
short data = Short.valueOf(line[4]);
String player = line[5];
Environment environment = Environment.NORMAL;
Expand All @@ -376,12 +355,10 @@ public void load() {
} catch (Exception e) {
environment = Environment.NORMAL;
}
World world = getServer().createWorld(line[6],
environment);
World world = getServer().createWorld(line[6], environment);
String showtype = line[7].toLowerCase();
Location loc = new Location(world, x, y, z);
ShowcaseItem showItem = new ShowcaseItem(loc, type,
data, player, showtype);
ShowcaseItem showItem = new ShowcaseItem(loc, type, data, player, showtype);
addShowcase(showItem);
String extra = line[9];
showItem.setExtraLoad(extra);
Expand All @@ -390,15 +367,13 @@ public void load() {
x = Integer.valueOf(line[0]);
y = Integer.valueOf(line[1]);
z = Integer.valueOf(line[2]);
Material type = Material.getMaterial(Integer
.valueOf(line[3]));
Material type = Material.getMaterial(Integer.valueOf(line[3]));
short data = Short.valueOf(line[4]);
String player = line[5];
World world = getServer().getWorld(line[6]);
String showtype = line[7].toLowerCase();
Location loc = new Location(world, x, y, z);
ShowcaseItem showItem = new ShowcaseItem(loc, type,
data, player, showtype);
ShowcaseItem showItem = new ShowcaseItem(loc, type, data, player, showtype);
addShowcase(showItem);
String extra = line[8];
showItem.setExtraLoad(extra);
Expand All @@ -414,8 +389,7 @@ public void load() {

public void setupPermissions() {
try {
Plugin test = this.getServer().getPluginManager()
.getPlugin("Permissions");
Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");

if (Showcase.Permissions == null) {
try {
Expand All @@ -431,8 +405,7 @@ public void setupPermissions() {
}
}

public static boolean hasPermission(Player player, String node,
boolean adminMethod) {
public static boolean hasPermission(Player player, String node, boolean adminMethod) {
if (Permissions != null) {
return Permissions.has(player, node);
} else {
Expand All @@ -452,9 +425,7 @@ public static String getName(Material type, short data) {
}

public void registerProvider(ShowcaseProvider provider) {
if (config.isTypeEnabled(provider.getType())
&& !(!provider.getType().equals("basic") && config
.isBasicMode())) {
if (config.isTypeEnabled(provider.getType()) && !(!provider.getType().equals("basic") && config.isBasicMode())) {
providers.put(provider.getType(), provider);
int a = 0;
for (ShowcaseItem item : showcasedItems) {
Expand All @@ -469,8 +440,7 @@ public void registerProvider(ShowcaseProvider provider) {
}
}

public boolean onCommand(CommandSender sender, Command cmd, String label,
String args[]) {
public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
if (cmd.getName().equals("showcase")) {
if (args.length >= 1) {
if (args[0].equalsIgnoreCase("reload")) {
Expand All @@ -482,8 +452,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label,
showcasedItems.clear();
itemsByDrop.clear();
config.load();
trans.reload(new File(getDataFolder(), "showcase-"
+ config.getLocale() + ".csv"));
trans.reload(new File(getDataFolder(), "showcase-" + config.getLocale() + ".csv"));
load();
for (ShowcaseProvider provider : providers.values()) {
registerProvider(provider);
Expand All @@ -507,8 +476,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label,
showcasedItems.clear();
itemsByDrop.clear();
config.load();
trans.reload(new File(getDataFolder(), "showcase-"
+ config.getLocale() + ".csv"));
trans.reload(new File(getDataFolder(), "showcase-" + config.getLocale() + ".csv"));
load();
for (ShowcaseProvider provider : providers.values()) {
registerProvider(provider);
Expand Down Expand Up @@ -540,7 +508,7 @@ public static boolean hasOddItem() {
}

public void addShowcase(ShowcaseItem item) {
if (item.isChunkLoaded()) {
if (item.isChunkLoaded() && item.getItem() != null) {
itemsByDrop.put(item.getItem().getEntityId(), item);
}
showcasedItems.add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,10 @@ public void onBlockBreak(BlockBreakEvent event) {
ShowcaseItem item = Showcase.instance.getItemByBlock(event.getBlock());
if (item != null) {
event.setCancelled(true);
event.getPlayer().sendMessage(
Showcase.tr("showcaseOwner", item.getPlayer()));
event.getPlayer().sendMessage(Showcase.tr("showcaseOwner", item.getPlayer()));
}
if (event.isCancelled()) {
event.getPlayer().sendBlockChange(event.getBlock().getLocation(),
event.getBlock().getType(), event.getBlock().getData());
event.getPlayer().sendBlockChange(event.getBlock().getLocation(), event.getBlock().getType(), event.getBlock().getData());
}
}

Expand Down
Loading

0 comments on commit d8077e8

Please sign in to comment.