Skip to content
This repository has been archived by the owner on Jun 16, 2022. It is now read-only.

Commit

Permalink
Refactored Logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
SagaciousZed committed Jul 8, 2012
1 parent 6a0a33d commit 2dd83c7
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 233 deletions.
12 changes: 7 additions & 5 deletions src/Blocks.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#This section allows you to define what blocks give exp, and how much.
#Make sure to put the block ID of the block, not the name, and then the
#amount of exp after it, like 56:
# exp: 20 Spacing is VERY important, so don't mess it up.
# This file allows you to define which blocks give exp and if so how much.
# Make sure to put the block ID of the block, not the name, and then the
# amount of exp after it, for example
# 56:
# exp: 20
# Spacing is VERY important, so don't mess it up.
Blocks:
56:
exp: 20
1:
exp: 2
exp: 2
62 changes: 16 additions & 46 deletions src/com/envisionred/smartexp/BlockListener.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.envisionred.smartexp;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.text.MessageFormat;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -25,7 +17,9 @@
*/
public class BlockListener implements Listener {

Map<Integer, Integer> expMap = new HashMap<Integer, Integer>();
private final static String AWARD = ChatColor.GREEN + "You have been awarded " + ChatColor.RED + "{0}" + ChatColor.GREEN + " exp for breaking a " + ChatColor.RED + "{1}" + ChatColor.GREEN + " block.";
private final static String PROGRESS = ChatColor.GREEN + "You are now " + ChatColor.AQUA + "{0}%" + ChatColor.GREEN + " of the way to level " + ChatColor.RED + "{1}";

private SmartExp plugin;

public BlockListener(SmartExp instance) {
Expand All @@ -34,47 +28,23 @@ public BlockListener(SmartExp instance) {

@EventHandler(priority = EventPriority.HIGHEST)
public void BlockBreak(BlockBreakEvent event) {
PutBlocks();
final int typeInt = event.getBlock().getTypeId();
if (expMap.containsKey(typeInt)) {
GiveExp(event.getPlayer(),
expMap.get(typeInt),
final ConfigurationSection blocksConfSection = plugin.getBlocksConfig().getConfigurationSection("Blocks");
final String typeID = String.valueOf(event.getBlock().getTypeId());
if (blocksConfSection.contains(typeID)) {
this.giveExp(event.getPlayer(),
blocksConfSection.getConfigurationSection(typeID).getInt("exp"),
event.getBlock().getType());
}
}

public void PutBlocks() {
final FileConfiguration blockConfig = plugin.getBlocksConfig();
final Set<String> keys = blockConfig.getConfigurationSection("Blocks").getKeys(false);
for (String block : keys) {
try {
Integer blockID = Integer.valueOf(block);
Integer expAmount = Integer.valueOf(blockConfig.getString("Blocks." + block + ".exp"));
expMap.put(blockID, expAmount);
} catch (NumberFormatException x) {
final Logger log = plugin.getLogger();
log.warning("Invalid blockID or exp amount specified in SmartExp config");
log.warning("blockID as defined in config: " + block);
log.warning("Exp amount for " + block + " as defined in config: "
+ blockConfig.getString("Blocks." + block + ".exp"));
}
}
}

public void GiveExp(Player player, int exp, Material material) {
private void giveExp(Player player, int exp, Material material) {
player.giveExp(exp);
boolean notify = plugin.getConfig().getBoolean("notifications");
if (notify) {
player.sendMessage(ChatColor.GREEN + "You have been awarded "
+ ChatColor.RED + exp + ChatColor.GREEN + " exp for breaking a "
+ ChatColor.RED + material + ChatColor.GREEN + " block.");
if (plugin.getConfig().getBoolean("notifications")) {
player.sendMessage(MessageFormat.format(AWARD, exp, material));
if (player.hasPermission("SmartExp.check")) {
int nextlevel = player.getLevel() + 1;
float xpNewer = player.getExp() * (100);
int xpPercent = Math.round(xpNewer);
player.sendMessage(ChatColor.GREEN + "You are now " + ChatColor.AQUA
+ xpPercent + "%" + ChatColor.GREEN + " of the way to level "
+ ChatColor.RED + nextlevel);
final int nextlevel = player.getLevel() + 1;
final int xpPercent = Math.round(player.getExp() * (100));
player.sendMessage(MessageFormat.format(PROGRESS, xpPercent, nextlevel));
}
}
}
Expand Down
76 changes: 19 additions & 57 deletions src/com/envisionred/smartexp/EntityListner.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.envisionred.smartexp;

import java.text.MessageFormat;

import org.bukkit.ChatColor;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -19,6 +14,10 @@
*/
public class EntityListner implements Listener {

private final static String PROGRESS = ChatColor.GREEN + "You are now " + ChatColor.AQUA + "{0}%" + ChatColor.GREEN + " of the way to level " + ChatColor.RED + "{1}";
private final static String AWARD= ChatColor.GREEN + "You have been awarded " + ChatColor.RED + "{0}" + ChatColor.GREEN + " experience for killing a " + ChatColor.RED + "{1}";
private final static String DROP = ChatColor.GREEN + "You killed a " + ChatColor.RED + "{0}" + ChatColor.GREEN + " and it dropped " + ChatColor.RED + "{1}" + ChatColor.GREEN + " experience.";

private SmartExp plugin;

public EntityListner(SmartExp instance) {
Expand All @@ -27,64 +26,27 @@ public EntityListner(SmartExp instance) {

@EventHandler
public void Death(EntityDeathEvent event) {
final Entity entity = event.getEntity();
final Player player = event.getEntity().getKiller();

// Now to check what kind of mob and act
final int expiranceAmmount = plugin.getConfig().getInt(entity.getType().getName());
this.giveExperiance(event, player, expiranceAmmount);
// whether to drop experience or not
final boolean direct = this.plugin.getConfig().getBoolean("give-xp-direct");
final boolean notify = this.plugin.getConfig().getBoolean("notifications");

}

public void giveExperiance(EntityDeathEvent event, Player player, int xp) {
final Entity mob = event.getEntity();
final EntityType type = mob.getType();

final boolean direct = plugin.getConfig().getBoolean("give-xp-direct", false);
// whether to drop xp or not
final Player player = event.getEntity().getKiller();
final int xp = this.plugin.getConfig().getInt(event.getEntityType().getName());

final boolean notify = plugin.getConfig().getBoolean("notifications", true);
if (direct) {
event.setDroppedExp(0);

player.giveExp(xp);
float xpNew = player.getExp();
int level = player.getLevel();
int nextlevel = level + 1;
float xpNewer = xpNew * (100);
int xpPercent = Math.round(xpNewer);
if (notify) {
player.sendMessage(ChatColor.GREEN + "You have been awarded "
+ ChatColor.RED + xp + ChatColor.GREEN + " experience for killing a "
+ ChatColor.RED + type);
if (player.hasPermission("SmartExp.check")) {
player.sendMessage(ChatColor.GREEN + "You are now " + ChatColor.AQUA
+ xpPercent + "%" + ChatColor.GREEN + " of the way to level "
+ ChatColor.RED + nextlevel);
}
}
} else {
event.setDroppedExp(xp);
float xpNew = player.getExp();
int level = player.getLevel();
int nextlevel = level + 1;
float xpNewer = xpNew * (100);
int xpPercent = Math.round(xpNewer);
if (notify) {
player.sendMessage(ChatColor.GREEN + "You killed a " + ChatColor.RED
+ type + ChatColor.GREEN + " and it dropped "
+ ChatColor.RED + xp + ChatColor.GREEN + " experience.");
if (player.hasPermission("SmartExp.check")) {
player.sendMessage(ChatColor.GREEN + "You are now " + ChatColor.AQUA
+ xpPercent + "%" + ChatColor.GREEN + " of the way to level "
+ ChatColor.RED + nextlevel);
}
}
}

}

public void GetTypeAndAct(EntityDeathEvent event, Player player, Entity entity) {

if (notify) {
player.sendMessage(MessageFormat.format((direct) ? AWARD : DROP, event.getEntityType(), xp));
if (player.hasPermission("SmartExp.check")) {
final int xpPercent = Math.round(player.getExp() * (100));
final int nextLevel = player.getLevel() + 1;
player.sendMessage(MessageFormat.format(PROGRESS, xpPercent, nextLevel));
}
}
}
}
13 changes: 3 additions & 10 deletions src/com/envisionred/smartexp/SmartExp.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import com.envisionred.smartexp.commands.BaseCommand;
import com.envisionred.smartexp.commands.ExpCommand;


import MetricsDependencies.Metrics;

/**
*
* @author EnvisionRed
*/
public class SmartExp extends JavaPlugin {
Expand All @@ -29,23 +28,18 @@ public void onEnable() {
if (!(new File(getDataFolder(), "Blocks.yml").exists())) {
saveResource("Blocks.yml", false);
}

// StartMetrics();

getServer().getPluginManager().registerEvents(new EntityListner(this), this);
getServer().getPluginManager().registerEvents(new BlockListener(this), this);

final BaseCommand expCommand = new BaseCommand(this);
final ExpCommand expCommand = new ExpCommand(this);
getCommand("exp").setExecutor(expCommand);

this.getLogger().info("EnvisionRed's SmartExp Enabled :D");
}



public void replaceOutdatedConfig() {
File configFile = new File(this.getDataFolder(), "config.yml");
if (!configFile.exists()) {
if (!new File(this.getDataFolder(), "config.yml").exists()) {
this.saveDefaultConfig();
} else {
if (getConfig().getInt("seriously-do-not-change-this") != 3) {
Expand All @@ -54,7 +48,6 @@ public void replaceOutdatedConfig() {
}
}

@SuppressWarnings("unused")
private void StartMetrics() {
final Logger log = this.getLogger();
final boolean noMetrics = this.getConfig().getBoolean("opt-out-metrics");
Expand Down
61 changes: 0 additions & 61 deletions src/com/envisionred/smartexp/commands/BaseCommand.java

This file was deleted.

Loading

0 comments on commit 2dd83c7

Please sign in to comment.