More treasure rewards work

This commit is contained in:
Shaun Bennett 2014-09-08 13:26:18 -05:00
parent 347e2b1bf5
commit 652075138e
10 changed files with 199 additions and 135 deletions

View File

@ -13,9 +13,9 @@ import org.bukkit.entity.Player;
import mineplex.core.common.util.NautHashMap;
import mineplex.core.common.util.UtilParticle;
import mineplex.core.treasure.animation.ChestSpawnAnimation;
import mineplex.core.treasure.animation.ChestOpenAnimation;
import mineplex.core.treasure.animation.Animation;
import mineplex.core.treasure.animation.ChestOpenAnimation;
import mineplex.core.treasure.animation.ChestSpawnAnimation;
import mineplex.core.treasure.reward.ITreasureReward;
/**
@ -214,14 +214,15 @@ public class Treasure
ChestData data = getChestData(block);
if (data != null && !data.isOpened())
{
data.setOpened(true);
data.getReward().giveReward(_player);
String text = data.getReward().giveReward(_player);
ChestOpenAnimation chestOpenTask = new ChestOpenAnimation(this, data);
data.setOpened(true);
ChestOpenAnimation chestOpenTask = new ChestOpenAnimation(this, data, text);
_animations.add(chestOpenTask);
if (isFinished())
{
// TODO figure out finish event
System.out.println("finished");
}
}

View File

@ -1,27 +1,16 @@
package mineplex.core.treasure;
import java.util.Iterator;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.craftbukkit.v1_7_R4.block.CraftBlock;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_7_R4.util.CraftMagicNumbers;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import net.minecraft.server.v1_7_R4.PacketPlayOutBlockAction;
import mineplex.core.MiniPlugin;
import mineplex.core.common.util.F;
import mineplex.core.common.util.NautHashMap;
@ -29,10 +18,10 @@ import mineplex.core.common.util.UtilAction;
import mineplex.core.common.util.UtilAlg;
import mineplex.core.common.util.UtilBlock;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilServer;
import mineplex.core.donation.DonationManager;
import mineplex.core.treasure.reward.ITreasureReward;
import mineplex.core.treasure.reward.RewardManager;
import mineplex.core.treasure.reward.RewardRarity;
import mineplex.core.treasure.reward.rewards.GemReward;
import mineplex.core.updater.UpdateType;
import mineplex.core.updater.event.UpdateEvent;
@ -54,12 +43,18 @@ public class TreasureManager extends MiniPlugin
_playerTreasureMap = new NautHashMap<Player, Treasure>();
_rewardManager = new RewardManager();
// Very Rare
_rewardManager.addReward(new GemReward(donationManager, 1000, 2000, 1000, RewardRarity.VERY_RARE));
// Rare
_rewardManager.addReward(new GemReward(donationManager, 500, 1000, 1000, RewardRarity.RARE));
// Uncommon
_rewardManager.addReward(new GemReward(donationManager, 100, 500, 1000, RewardRarity.UNCOMMON));
// Common
_rewardManager.addReward(new GemReward(donationManager, 0, 100, 1000, RewardRarity.COMMON));
_rewardManager.addReward(new GemReward(_donationManager, 5));
_rewardManager.addReward(new GemReward(_donationManager, 50));
_rewardManager.addReward(new GemReward(_donationManager, 200));
_rewardManager.addReward(new GemReward(_donationManager, 500));
_rewardManager.addReward(new GemReward(_donationManager, 5000));
}
@Override

View File

@ -8,7 +8,6 @@ import org.bukkit.craftbukkit.v1_7_R4.util.CraftMagicNumbers;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import net.minecraft.server.v1_7_R4.PacketPlayOutBlockAction;
import mineplex.core.common.util.UtilServer;
@ -22,14 +21,16 @@ import mineplex.core.treasure.Treasure;
public class ChestOpenAnimation extends Animation
{
private ChestData _chestData;
private String _text;
private Item _itemEntity;
private SimpleHologram _hologram;
public ChestOpenAnimation(Treasure treasure, ChestData chestData)
public ChestOpenAnimation(Treasure treasure, ChestData chestData, String text)
{
super(treasure);
_chestData = chestData;
_text = text;
// Send chest open packet
Block block = chestData.getBlock();
@ -53,7 +54,7 @@ public class ChestOpenAnimation extends Animation
}
else if (getTicks() == 15)
{
_hologram = new SimpleHologram(_chestData.getBlock().getLocation().add(0.5, 1.1, 0.5), _chestData.getReward().getText());
_hologram = new SimpleHologram(_chestData.getBlock().getLocation().add(0.5, 1.1, 0.5), _text);
_hologram.spawnWithPackets();
_chestData.setFinishedOpen(true);

View File

@ -8,13 +8,22 @@ import org.bukkit.inventory.ItemStack;
*/
public interface ITreasureReward
{
public String getText();
public ItemStack getItem();
public RewardRarity getRarity();
public void giveReward(Player player);
/**
* Give the reward to the player.
*
* @return String detailing the reward. This will be shown to the player
*/
public String giveReward(Player player);
public boolean canGiveReward(Player player);
/**
* This is used to weight rewards across different rarities. Rewards with a larger weight
* will have a higher chance of being selected
*/
public int getWeight();
}

View File

@ -37,12 +37,6 @@ public class RewardManager
public ITreasureReward nextReward(Player player)
{
/*
This is potentially blocking, need to make sure each RewardRarity has at least one reward that
players can recieve no matter what. May need to touch on this later.
*/
//todo
double rand = _random.nextDouble();
RewardRarity rarity;
@ -69,15 +63,34 @@ public class RewardManager
List<ITreasureReward> treasureList = _treasureMap.get(rarity);
ITreasureReward reward;
do
int totalWeight = 0;
ArrayList<ITreasureReward> possibleRewards = new ArrayList<ITreasureReward>();
for (ITreasureReward treasure : treasureList)
{
reward = treasureList.get(_random.nextInt(treasureList.size()));
if (treasure.canGiveReward(player))
{
possibleRewards.add(treasure);
totalWeight += treasure.getWeight();
}
}
while (!reward.canGiveReward(player));
return reward;
if (totalWeight > 0)
{
int weight = _random.nextInt(totalWeight);
int currentWeight = 0;
for (ITreasureReward reward : possibleRewards)
{
if (weight <= currentWeight)
return reward;
weight += reward.getWeight();
}
}
return null;
}
}

View File

@ -0,0 +1,45 @@
package mineplex.core.treasure.reward.rewards;
import java.util.Random;
import org.bukkit.inventory.ItemStack;
import mineplex.core.treasure.reward.ITreasureReward;
import mineplex.core.treasure.reward.RewardRarity;
/**
* Created by shaun on 2014-09-08.
*/
public abstract class AbstractReward implements ITreasureReward
{
protected static final Random RANDOM = new Random();
private ItemStack _itemStack;
private RewardRarity _rarity;
private int _weight;
public AbstractReward(ItemStack itemStack, RewardRarity rarity, int weight)
{
_itemStack = itemStack;
_rarity = rarity;
_weight = weight;
}
@Override
public ItemStack getItem()
{
return _itemStack;
}
@Override
public int getWeight()
{
return _weight;
}
@Override
public RewardRarity getRarity()
{
return _rarity;
}
}

View File

@ -0,0 +1,66 @@
package mineplex.core.treasure.reward.rewards;
/**
* Created by shaun on 2014-09-08.
*/
import java.util.Random;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.donation.DonationManager;
import mineplex.core.treasure.reward.RewardRarity;
/**
* Created by shaun on 2014-09-08.
*/
public class CoinReward extends AbstractReward
{
private DonationManager _donationManager;
private Random _random;
private int _minCoinCount;
private int _maxCoinCount;
public CoinReward(DonationManager donationManager, int minGemCount, int maxGemCount, int weight, RewardRarity rarity)
{
this(donationManager, minGemCount, maxGemCount, weight, rarity, RANDOM);
}
public CoinReward(DonationManager donationManager, int minCoinCount, int maxCoinCount, int weight, RewardRarity rarity, Random random)
{
super(new ItemStack(Material.EMERALD), rarity, weight);
_donationManager = donationManager;
_minCoinCount = minCoinCount;
_maxCoinCount = maxCoinCount;
_random = random;
}
@Override
public String giveReward(Player player)
{
int gemsToReward = _random.nextInt(_maxCoinCount - _minCoinCount) + _minCoinCount;
_donationManager.RewardGems(new Callback<Boolean>()
{
@Override
public void run(Boolean data)
{
}
}, "Treasure Chest", player.getName(), gemsToReward);
return C.cYellow + gemsToReward + " Coins";
}
@Override
public boolean canGiveReward(Player player)
{
return true;
}
}

View File

@ -1,45 +0,0 @@
package mineplex.core.treasure.reward.rewards;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.C;
import mineplex.core.treasure.reward.ITreasureReward;
import mineplex.core.treasure.reward.RewardRarity;
/**
* Created by Shaun on 9/2/2014.
*/
public class ExampleReward implements ITreasureReward
{
@Override
public String getText()
{
return C.cGreen + "100 Gems";
}
@Override
public ItemStack getItem()
{
return new ItemStack(Material.EMERALD);
}
@Override
public RewardRarity getRarity()
{
return RewardRarity.COMMON;
}
@Override
public void giveReward(Player player)
{
player.sendMessage("give example reward...");
}
@Override
public boolean canGiveReward(Player player)
{
return true;
}
}

View File

@ -3,49 +3,31 @@ package mineplex.core.treasure.reward.rewards;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import mineplex.core.account.CoreClientManager;
import mineplex.core.donation.DonationManager;
import mineplex.core.gadget.types.Gadget;
import mineplex.core.gadget.types.ParticleGadget;
import mineplex.core.treasure.reward.ITreasureReward;
import mineplex.core.treasure.reward.RewardRarity;
/**
* Created by shaun on 2014-09-04.
*/
public class GadgetReward implements ITreasureReward
public class GadgetReward extends AbstractReward
{
private Gadget _gadget;
private DonationManager _donationManager;
private Gadget _gadget;
public GadgetReward(Gadget gadget, DonationManager donationManager)
public GadgetReward(Gadget gadget, DonationManager donationManager, RewardRarity rarity, int weight)
{
super(new ItemStack(gadget.GetDisplayMaterial()), rarity, weight);
_gadget = gadget;
_donationManager = donationManager;
}
@Override
public String getText()
{
return _gadget.GetDisplayName();
}
@Override
public ItemStack getItem()
{
return new ItemStack(_gadget.GetDisplayMaterial());
}
@Override
public RewardRarity getRarity()
{
return RewardRarity.VERY_RARE;
}
@Override
public void giveReward(Player player)
public String giveReward(Player player)
{
_donationManager.Get(player.getName()).AddUnknownSalesPackagesOwned(_gadget.GetName());
return _gadget.GetDisplayName();
}
@Override
@ -53,4 +35,5 @@ public class GadgetReward implements ITreasureReward
{
return !(_donationManager.Get(player.getName()).OwnsUnknownPackage(_gadget.GetName()));
}
}

View File

@ -1,5 +1,7 @@
package mineplex.core.treasure.reward.rewards;
import java.util.Random;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -7,47 +9,38 @@ import org.bukkit.inventory.ItemStack;
import mineplex.core.common.util.C;
import mineplex.core.common.util.Callback;
import mineplex.core.donation.DonationManager;
import mineplex.core.treasure.reward.ITreasureReward;
import mineplex.core.treasure.reward.RewardRarity;
/**
* Created by shaun on 2014-09-04.
*/
public class GemReward implements ITreasureReward
public class GemReward extends AbstractReward
{
private DonationManager _donationManager;
private ItemStack _itemStack;
private int _gemAmount;
private Random _random;
private int _minGemCount;
private int _maxGemCount;
public GemReward(DonationManager donationManager, int gemAmount)
public GemReward(DonationManager donationManager, int minGemCount, int maxGemCount, int weight, RewardRarity rarity)
{
this(donationManager, minGemCount, maxGemCount, weight, rarity, RANDOM);
}
public GemReward(DonationManager donationManager, int minGemCount, int maxGemCount, int weight, RewardRarity rarity, Random random)
{
super(new ItemStack(Material.EMERALD), rarity, weight);
_donationManager = donationManager;
_gemAmount = gemAmount;
_minGemCount = minGemCount;
_maxGemCount = maxGemCount;
_itemStack = new ItemStack(Material.EMERALD);
_random = random;
}
@Override
public String getText()
public String giveReward(Player player)
{
return C.cGreen + _gemAmount + " Gems";
}
int gemsToReward = _random.nextInt(_maxGemCount - _minGemCount) + _minGemCount;
@Override
public ItemStack getItem()
{
return _itemStack;
}
@Override
public RewardRarity getRarity()
{
return RewardRarity.COMMON;
}
@Override
public void giveReward(Player player)
{
_donationManager.RewardGems(new Callback<Boolean>()
{
@Override
@ -55,7 +48,9 @@ public class GemReward implements ITreasureReward
{
}
}, "Treasure Chest", player.getName(), _gemAmount);
}, "Treasure Chest", player.getName(), gemsToReward);
return C.cGreen + gemsToReward + " Gems";
}
@Override
@ -63,4 +58,5 @@ public class GemReward implements ITreasureReward
{
return true;
}
}