Econ

Discussion in 'Plugin Development' started by bodhistrontg, Dec 20, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    bodhistrontg

    So, I was wondering would you happen to know a way to create a "Economy plugin" without a config and it doesn't save it just turns to 0 when they join i tried with Hashmaps but it didn't quite turn out well and it seems easy and it probably is but i was just wonder if you hand any suggestions. Thank you guys.
     
  2. Offline

    Not2EXceL

    You can't create a permanent saving stats for a plugin without utilizing some sort of flat file or database. If you keep it in memory only, once you restart its wiped.
     
    bodhistrontg likes this.
  3. Offline

    ResultStatic

    its pretty easy to create a simple plugin for an economy that saves the player money in a config.
    here is a nice tut
     
  4. Offline

    bodhistrontg

    Exact the point

    I think i have this figured out but i need help with sign clicked and stuff sorry this is oftopic
    Code:java
    1. package me.bodhiIsEpic.bukkit;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.HashMap;
    6.  
    7. import net.minecraft.server.v1_6_R3.Block;
    8.  
    9. import org.bukkit.ChatColor;
    10. import org.bukkit.Material;
    11. import org.bukkit.block.Sign;
    12. import org.bukkit.command.Command;
    13. import org.bukkit.command.CommandExecutor;
    14. import org.bukkit.command.CommandSender;
    15. import org.bukkit.entity.Player;
    16. import org.bukkit.event.EventHandler;
    17. import org.bukkit.event.Listener;
    18. import org.bukkit.event.block.Action;
    19. import org.bukkit.event.player.PlayerInteractEvent;
    20. import org.bukkit.event.player.PlayerJoinEvent;
    21. import org.bukkit.inventory.ItemStack;
    22. import org.bukkit.inventory.meta.ItemMeta;
    23.  
    24. public class Econ
    25. implements CommandExecutor, Listener
    26. {
    27. private ItemStack coalt;
    28. private ItemStack mpl;
    29. private ItemStack AK74U;
    30. private ItemStack PM63;
    31. private ItemStack MP40;
    32. private ItemStack STAKEOUT;
    33. private ItemStack MP5K;
    34. private ItemStack M16;
    35. private ItemStack AUG;
    36. Main m;
    37. public static HashMap<String, Double> bal = new HashMap();
    38.  
    39. public void Wepons()
    40. {
    41. this.coalt = createItem(new ItemStack(Material.BOW, 1), ChatColor.RED + "coalt", "Begining Weapon", 0);
    42. this.mpl = createItem(new ItemStack(Material.BOW, 1), ChatColor.RED + "mpl", "Note", 1000);
    43. this.AK74U = createItem(new ItemStack(Material.BOW, 1), ChatColor.RED + "AK-74U", "note", 1200);
    44. this.PM63 = createItem(new ItemStack(Material.BOW), ChatColor.RED + "PM63", "note", 1000);
    45. this.MP40 = createItem(new ItemStack(Material.BOW), ChatColor.RED + "MP40", "note", 1000);
    46. this.STAKEOUT = createItem(new ItemStack(Material.BOW), ChatColor.RED + "STAKEOUT", "note", 1500);
    47. this.MP5K = createItem(new ItemStack(Material.BOW), ChatColor.RED + "MP5K", "note", 1000);
    48. this.M16 = createItem(new ItemStack(Material.BOW), ChatColor.RED + "M16", "note", 1200);
    49. this.AUG = createItem(new ItemStack(Material.BOW), ChatColor.RED + "AUG", "note", 1200);
    50. }
    51.  
    52. private ItemStack createItem(ItemStack is, String name, String note, int amount) {
    53. ItemStack i = new ItemStack(is);
    54. ItemMeta im = i.getItemMeta();
    55. im.setDisplayName(name);
    56. im.setLore(Arrays.asList(new String[] { ChatColor.LIGHT_PURPLE + note }));
    57. i.setItemMeta(im);
    58. return i;
    59. }
    60.  
    61. public Econ(Main main) {
    62. Wepons();
    63. this.m = main;
    64. }
    65. @EventHandler
    66. public void onJoin(PlayerJoinEvent e) {
    67. e.getPlayer().getInventory().addItem(new ItemStack[] { this.coalt });
    68. setBalance(e.getPlayer().getDisplayName(), 10000.0D);
    69. }
    70.  
    71. public static Double getBalance(String player)
    72. {
    73. return (Double)bal.get(player);
    74. }
    75. public static void addMoney(String player, Double amount) {
    76. setBalance(player, getBalance(player).doubleValue() + amount.doubleValue());
    77. }
    78. public static void removeMoney(String player, Double amount, Player p) {
    79. if(getBalance(player) < amount){
    80. p.sendMessage("Can not buy that not enough money");
    81. return;
    82. }
    83. setBalance(player, getBalance(player).doubleValue() - amount.doubleValue());
    84. }
    85.  
    86. public static void setBalance(String player, double amount) {
    87. bal.put(player, Double.valueOf(amount));
    88. }
    89.  
    90. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    91. if (cmd.getName().equalsIgnoreCase("bal")) {
    92. Player player = (Player)sender;
    93. player.sendMessage(ChatColor.GOLD + "You balance is: " + getBalance(player.getDisplayName()));
    94. }
    95. return false;
    96. }
    97.  
    98. @EventHandler
    99. public void onInteract(PlayerInteractEvent Event) {//not working
    100. if (!(Event.getClickedBlock().getState() instanceof Sign)) return;
    101. Player player = Event.getPlayer();
    102. Sign s = (Sign) Event.getClickedBlock();
    103. if(s.getLine(0).equalsIgnoreCase("buy")){
    104. if(getBalance(player.getDisplayName()) < 1000.0){
    105. return;
    106. }else{
    107. removeMoney(player.getDisplayName(), 1000.0, player);
    108. player.getInventory().addItem(mpl);
    109. player.sendMessage("Bought mpl");
    110. }
    111. }
    112. }
    113. }


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  5. Offline

    calebbfmv

    Erm:




    • public void onInteract(PlayerInteractEvent Event) {//not working



    • Event needs to be event, not capital.


     
  6. Offline

    bodhistrontg

    calebbfmv Nope i fixed that new code + errors
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e) {
    3. if (!(e.getClickedBlock().getState() instanceof Sign)) return;
    4. Player player = e.getPlayer();
    5. Sign s = (Sign) e.getClickedBlock();
    6. if(s.getLine(0).equalsIgnoreCase("buy")){
    7. if(getBalance(player.getDisplayName()) <= 1000.0){
    8. return;
    9. }else{
    10. removeMoney(player.getDisplayName(), 1000.0, player);
    11. player.getInventory().addItem(mpl);
    12. player.sendMessage("Bought mpl");
    13. }
    14. }
    15. }

    Errors
    Code:
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.block.CraftBlock cannot be cast to org.bukkit.block.Sign
    at me.bodhiIsEpic.bukkit.Econ.onInteract(Econ.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more
    19:27:58 [SEVERE] Could not pass event PlayerInteractEvent to ZEA v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
    at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:190)
    at net.minecraft.server.v1_6_R3.PlayerInteractManager.interact(PlayerInteractManager.java:373)
    at net.minecraft.server.v1_6_R3.PlayerConnection.a(PlayerConnection.java:630)
    at net.minecraft.server.v1_6_R3.Packet15Place.handle(SourceFile:58)
    at net.minecraft.server.v1_6_R3.NetworkManager.b(NetworkManager.java:296)
    at net.minecraft.server.v1_6_R3.PlayerConnection.e(PlayerConnection.java:116)
    at net.minecraft.server.v1_6_R3.ServerConnection.b(SourceFile:37)
    at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:30)
    at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
    at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
    at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
    at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
    at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_6_R3.block.CraftBlock cannot be cast to org.bukkit.block.Sign
    at me.bodhiIsEpic.bukkit.Econ.onInteract(Econ.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
    ... 16 more
    
     
Thread Status:
Not open for further replies.

Share This Page