Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
Moved factories into their own package and gave them their own instances
Browse files Browse the repository at this point in the history
  • Loading branch information
bziemons committed Dec 23, 2013
1 parent dc577b9 commit abe5f25
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 47 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/thezorro266/bukkit/srm/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
import org.bukkit.entity.Player;

import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.thezorro266.bukkit.srm.factories.RegionFactory;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;
import com.thezorro266.bukkit.srm.helpers.Permission;
import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.templates.Template;
import com.thezorro266.bukkit.srm.templates.interfaces.OwnableTemplate;

Expand Down Expand Up @@ -126,7 +127,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
}

SimpleRegionMarket.getInstance().getRegionFactory().destroyRegion(realRegion);
RegionFactory.instance.destroyRegion(realRegion);
sender.sendMessage(String.format("Region %s in world %s was removed.", region, world));
}
}
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/thezorro266/bukkit/srm/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;

import com.thezorro266.bukkit.srm.factories.SignFactory;
import com.thezorro266.bukkit.srm.factories.SignFactory.Sign;
import com.thezorro266.bukkit.srm.helpers.Location;
import com.thezorro266.bukkit.srm.helpers.SignFactory.Sign;
import com.thezorro266.bukkit.srm.templates.Template;

public class EventListener implements Listener {
Expand All @@ -39,7 +40,7 @@ public EventListener() {
public void onSignChanged(SignChangeEvent event) {
if (!event.isCancelled()) {
Player player = event.getPlayer();
Sign sign = SimpleRegionMarket.getInstance().getSignFactory().getSignFromLocation(Location.fromBlock(event.getBlock()));
Sign sign = SignFactory.instance.getSignFromLocation(Location.fromBlock(event.getBlock()));
if (sign != null) {
if (!sign.getRegion().getTemplate().breakSign(player, sign)) {
event.setCancelled(true);
Expand All @@ -64,7 +65,7 @@ public void onSignChanged(SignChangeEvent event) {
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
if (!event.isCancelled()) {
Sign sign = SimpleRegionMarket.getInstance().getSignFactory().getSignFromLocation(Location.fromBlock(event.getBlock()));
Sign sign = SignFactory.instance.getSignFromLocation(Location.fromBlock(event.getBlock()));
if (sign != null) {
if (!sign.getRegion().getTemplate().breakSign(event.getPlayer(), sign)) {
event.setCancelled(true);
Expand All @@ -76,9 +77,9 @@ public void onBlockBreak(BlockBreakEvent event) {
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (event.hasBlock()) {
if (SimpleRegionMarket.getInstance().getSignFactory().isSign(event.getClickedBlock())) {
if (SignFactory.instance.isSign(event.getClickedBlock())) {
if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
Sign sign = SimpleRegionMarket.getInstance().getSignFactory().getSignFromLocation(Location.fromBlock(event.getClickedBlock()));
Sign sign = SignFactory.instance.getSignFromLocation(Location.fromBlock(event.getClickedBlock()));
if (sign != null) {
sign.getRegion().getTemplate().clickSign(event.getPlayer(), sign);
}
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/com/thezorro266/bukkit/srm/SimpleRegionMarket.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
import com.thezorro266.bukkit.srm.exceptions.ContentLoadException;
import com.thezorro266.bukkit.srm.exceptions.ContentSaveException;
import com.thezorro266.bukkit.srm.exceptions.TemplateFormatException;
import com.thezorro266.bukkit.srm.factories.RegionFactory;
import com.thezorro266.bukkit.srm.helpers.LocationSignHelper;
import com.thezorro266.bukkit.srm.helpers.RegionFactory;
import com.thezorro266.bukkit.srm.helpers.SignFactory;
import com.thezorro266.bukkit.srm.helpers.WorldHelper;
import com.thezorro266.bukkit.srm.templates.Template;
import com.thezorro266.bukkit.srm.templates.interfaces.TimedTemplate;
Expand All @@ -44,10 +43,6 @@ public class SimpleRegionMarket extends JavaPlugin {
@Getter
private final WorldHelper worldHelper;
@Getter
private final SignFactory signFactory;
@Getter
private final RegionFactory regionFactory;
@Getter
private final TemplateManager templateManager;
@Getter
private final WorldGuardManager worldGuardManager;
Expand All @@ -62,8 +57,6 @@ public SimpleRegionMarket() {
instance = this;
locationSignHelper = new LocationSignHelper();
worldHelper = new WorldHelper();
signFactory = new SignFactory();
regionFactory = new RegionFactory();
templateManager = new TemplateManager();
worldGuardManager = new WorldGuardManager();
vaultHook = new VaultHook();
Expand Down Expand Up @@ -115,7 +108,7 @@ public void onEnable() {
except(e);
}
}
getLogger().info(String.format("Loaded %d regions in %dms", regionFactory.getRegionCount(), (System.nanoTime() - start) / 1000000L));
getLogger().info(String.format("Loaded %d regions in %dms", RegionFactory.instance.getRegionCount(), (System.nanoTime() - start) / 1000000L));

// Check if the plugin should be disabled because of an exception
if (disable) {
Expand Down Expand Up @@ -149,7 +142,7 @@ public void saveRegions() throws ContentSaveException {
{
templateManager.saveContent();
}
getLogger().info(String.format("Saved %d regions in %dms", regionFactory.getRegionCount(), (System.nanoTime() - start) / 1000000L));
getLogger().info(String.format("Saved %d regions in %dms", RegionFactory.instance.getRegionCount(), (System.nanoTime() - start) / 1000000L));
} else {
getLogger().info("Not saving anything, because I didn't even load");
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/thezorro266/bukkit/srm/TemplateManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package com.thezorro266.bukkit.srm;

import static com.thezorro266.bukkit.srm.helpers.SignFactory.Sign.SIGN_LINE_COUNT;
import static com.thezorro266.bukkit.srm.factories.SignFactory.Sign.SIGN_LINE_COUNT;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -39,7 +39,8 @@
import com.thezorro266.bukkit.srm.exceptions.ContentSaveException;
import com.thezorro266.bukkit.srm.exceptions.TemplateFormatException;
import com.thezorro266.bukkit.srm.exceptions.ThisShouldNeverHappenException;
import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.RegionFactory;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;
import com.thezorro266.bukkit.srm.templates.Template;
import com.thezorro266.bukkit.srm.templates.TemplateRent;
import com.thezorro266.bukkit.srm.templates.TemplateSell;
Expand Down Expand Up @@ -324,7 +325,7 @@ public void loadContent() throws ContentLoadException {
}

// Let the RegionFactory do the rest
SimpleRegionMarket.getInstance().getRegionFactory().loadFromConfiguration(regionConfig, "");
RegionFactory.instance.loadFromConfiguration(regionConfig, "");
}
} else {
SimpleRegionMarket.getInstance().getLogger().warning(String.format("The world %s in the template %s was not found and could not be loaded.", worldStr, template.getId()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.thezorro266.bukkit.srm.exceptions;

import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;

public class ContentSaveException extends Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.thezorro266.bukkit.srm.helpers;
package com.thezorro266.bukkit.srm.factories;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -37,11 +37,16 @@
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.thezorro266.bukkit.srm.SimpleRegionMarket;
import com.thezorro266.bukkit.srm.exceptions.ContentLoadException;
import com.thezorro266.bukkit.srm.helpers.SignFactory.Sign;
import com.thezorro266.bukkit.srm.factories.SignFactory.Sign;
import com.thezorro266.bukkit.srm.helpers.Location;
import com.thezorro266.bukkit.srm.templates.SignTemplate;
import com.thezorro266.bukkit.srm.templates.Template;

public class RegionFactory {
public static final RegionFactory instance = new RegionFactory();

private RegionFactory() {
}

@Getter
private int regionCount = 0;
Expand Down Expand Up @@ -81,24 +86,24 @@ public String getName() {
}

public Sign addBlockAsSign(Block block) {
if (SimpleRegionMarket.getInstance().getSignFactory().isSign(block)) {
if (SignFactory.instance.isSign(block)) {
org.bukkit.material.Sign signMat = (org.bukkit.material.Sign) block.getState().getData();
Sign sign = SimpleRegionMarket.getInstance().getSignFactory().createSign(this, Location.fromBlock(block), block.getType().equals(Material.WALL_SIGN), signMat.getFacing());
Sign sign = SignFactory.instance.createSign(this, Location.fromBlock(block), block.getType().equals(Material.WALL_SIGN), signMat.getFacing());
return sign;
}
return null;
}

public void updateSigns() {
for (Sign sign : signList) {
template.updateSign(sign);
}
}

public boolean isOption(String optionAlias) {
return options.containsKey(optionAlias);
}

public Object getOption(String optionAlias) {
return options.get(optionAlias);
}
Expand Down Expand Up @@ -192,7 +197,7 @@ public void loadFromConfiguration(Configuration config, String path) throws Cont
for (String signKey : config.getConfigurationSection(path + "signs").getKeys(false)) {
Sign sign;
try {
sign = SimpleRegionMarket.getInstance().getSignFactory().loadFromConfiguration(config, region, String.format("%ssigns.%s.", path, signKey));
sign = SignFactory.instance.loadFromConfiguration(config, region, String.format("%ssigns.%s.", path, signKey));
} catch (IllegalArgumentException e) {
throw new ContentLoadException("Could not create sign " + signKey, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.thezorro266.bukkit.srm.helpers;
package com.thezorro266.bukkit.srm.factories;

import lombok.Data;
import lombok.Getter;

import org.bukkit.Material;
import org.bukkit.block.Block;
Expand All @@ -27,9 +28,16 @@

import com.thezorro266.bukkit.srm.SimpleRegionMarket;
import com.thezorro266.bukkit.srm.exceptions.ContentLoadException;
import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;
import com.thezorro266.bukkit.srm.helpers.Location;

public class SignFactory {
public static final SignFactory instance = new SignFactory();

private SignFactory() {
}

@Getter
private int signCount = 0;

public @Data
Expand Down Expand Up @@ -117,10 +125,6 @@ public void destroySign(Sign sign) {
--signCount;
}

public int getSignCount() {
return signCount;
}

public boolean isSign(Block block) {
if (block.getType().equals(Material.WALL_SIGN) || block.getType().equals(Material.SIGN_POST)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.ArrayList;

import com.thezorro266.bukkit.srm.helpers.SignFactory.Sign;
import com.thezorro266.bukkit.srm.factories.SignFactory.Sign;

public class LocationSignHelper {
private ArrayList<Sign> signList = new ArrayList<Sign>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import org.bukkit.World;

import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;

public class WorldHelper {
private WeakHashMap<Region, World> regionMap = new WeakHashMap<Region, World>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

package com.thezorro266.bukkit.srm.templates;

import static com.thezorro266.bukkit.srm.helpers.SignFactory.Sign.SIGN_LINE_COUNT;
import static com.thezorro266.bukkit.srm.factories.SignFactory.Sign.SIGN_LINE_COUNT;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -31,9 +31,9 @@
import org.bukkit.entity.Player;

import com.thezorro266.bukkit.srm.SimpleRegionMarket;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.SignFactory.Sign;
import com.thezorro266.bukkit.srm.helpers.Location;
import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.helpers.SignFactory.Sign;

public abstract class SignTemplate extends Template {
protected String[] signInput;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

import com.thezorro266.bukkit.srm.SimpleRegionMarket;
import com.thezorro266.bukkit.srm.exceptions.TemplateFormatException;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.SignFactory.Sign;
import com.thezorro266.bukkit.srm.helpers.Location;
import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.helpers.SignFactory.Sign;

public abstract class Template {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import com.thezorro266.bukkit.srm.SimpleRegionMarket;
import com.thezorro266.bukkit.srm.factories.RegionFactory;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.SignFactory.Sign;
import com.thezorro266.bukkit.srm.helpers.Location;
import com.thezorro266.bukkit.srm.helpers.RegionFactory;
import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.helpers.SignFactory.Sign;
import com.thezorro266.bukkit.srm.templates.interfaces.OwnableTemplate;

public class TemplateSell extends SignTemplate implements OwnableTemplate {
Expand Down Expand Up @@ -218,7 +218,7 @@ public Sign makeSign(Player player, Block block, HashMap<String, String> inputMa
}

if (!existentRegion) {
Region region = SimpleRegionMarket.getInstance().getRegionFactory().createRegion(this, block.getWorld(), worldguardRegion);
Region region = RegionFactory.instance.createRegion(this, block.getWorld(), worldguardRegion);

double price;
if (SimpleRegionMarket.getInstance().getVaultHook().getEconomy() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import org.bukkit.OfflinePlayer;

import com.thezorro266.bukkit.srm.helpers.RegionFactory.Region;
import com.thezorro266.bukkit.srm.factories.RegionFactory.Region;

/**
* An interface that gives a template the general methods to manage owners and members and being able to set a region occupied.
Expand Down

0 comments on commit abe5f25

Please sign in to comment.