/[projects]/miscJava/bukkit-minecraft-plugins/CreativeWorld/src/dk/thoerup/bukkit/creativeworld/WorldChanged.java
ViewVC logotype

Contents of /miscJava/bukkit-minecraft-plugins/CreativeWorld/src/dk/thoerup/bukkit/creativeworld/WorldChanged.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1682 - (show annotations) (download)
Thu Jan 26 19:01:52 2012 UTC (12 years, 3 months ago) by torben
File size: 3718 byte(s)
users dont accumulate xp points in creative
1 package dk.thoerup.bukkit.creativeworld;
2
3 import org.bukkit.*;
4 import org.bukkit.entity.*;
5 import org.bukkit.event.*;
6 import org.bukkit.event.player.*;
7 import org.bukkit.configuration.*;
8 import org.bukkit.inventory.*;
9
10 public class WorldChanged implements Listener {
11 final static String STACK_TYPE = ".type";
12 final static String STACK_AMOUNT = ".amount";
13 final static String STACK_DAMAGE = ".damage";
14 final static String STACK_DATA = ".data";
15
16 final static String INVENTORY_CREATIVE = ".inventory.creative.";
17 final static String INVENTORY_CREATIVE_ARMOR = ".inventory.creative.armor.";
18
19 CreativeMain plugin;
20 public WorldChanged(CreativeMain plugin) {
21 this.plugin = plugin;
22 }
23
24
25 @EventHandler
26 public void onPlayerExpChange(PlayerExpChangeEvent event) {
27 if (event.getPlayer().getWorld().getName().equalsIgnoreCase("creative")) {
28 event.setAmount(0); //users don't acummulate xp points in creative mode
29 }
30 }
31
32 @EventHandler
33 public void onPlayerChangedWorld(PlayerChangedWorldEvent event) {
34
35
36 Player player = event.getPlayer();
37
38 System.out.println( player.getName() + " : " + event.getFrom().getName() + " -> " + player.getWorld().getName() );
39
40 Configuration config = plugin.getConfig();
41
42 if (player.getWorld().getName().equalsIgnoreCase("creative") ) {
43 player.sendMessage(ChatColor.YELLOW + "You are now in creative world - have fun");
44 config.set( player.getName() + ".iscreative", true);
45
46 PlayerInventory inv = player.getInventory();
47
48 for (int i=0; i<inv.getSize(); i++) {
49 String node = player.getName() + INVENTORY_CREATIVE + i;;
50 //config.set( node, inv.getItem(i) );
51 saveItemStack(config, node, inv.getItem(i) );
52 }
53
54 ItemStack armor[] = inv.getArmorContents();
55 for (int i=0; i<armor.length; i++) {
56 String node = player.getName() + INVENTORY_CREATIVE_ARMOR + i;;
57 //config.set( node, armor[i] );
58 saveItemStack(config, node, armor[i] );
59 }
60
61 plugin.saveConfig();
62
63 inv.clear();
64
65 player.setGameMode( GameMode.CREATIVE );
66
67 } else if (event.getFrom().getName().equalsIgnoreCase("creative") ) {
68 player.sendMessage(ChatColor.YELLOW + "You are now in survival world - watch out !!!");
69 config.set( player.getName() + ".iscreative", false);
70 player.setGameMode( GameMode.SURVIVAL );
71
72 PlayerInventory inv = player.getInventory();
73 inv.clear();
74
75 ItemStack items[] = new ItemStack[ inv.getSize() ];
76
77 for (int i=0; i<inv.getSize(); i++) {
78 String node = player.getName() + INVENTORY_CREATIVE + i;;
79 //items[i] = config.getItemStack(node);
80 items[i] = getItemStack(config, node);
81 }
82
83
84
85 ItemStack armor[] = new ItemStack[ inv.getArmorContents().length];
86 for (int i=0; i<armor.length; i++) {
87 String node = player.getName() + INVENTORY_CREATIVE_ARMOR + i;
88 //armor[i] = config.getItemStack(node);
89 armor[i] = getItemStack(config, node);
90 }
91
92 inv.setContents( items );
93 inv.setArmorContents( armor );
94 }
95 }
96
97
98 public void saveItemStack(Configuration config, String node, ItemStack stack) {
99 config.set(node + STACK_TYPE, stack.getTypeId() );
100 config.set(node + STACK_AMOUNT, stack.getAmount() );
101 config.set(node + STACK_DAMAGE, (int)stack.getDurability() );
102 config.set(node + STACK_DATA, (int)stack.getData().getData() );
103 }
104
105 public ItemStack getItemStack(Configuration config, String node) {
106 int type = config.getInt( node + STACK_TYPE);
107 int amount = config.getInt( node + STACK_AMOUNT);
108 short damage = (short) config.getInt(node + STACK_DAMAGE);
109 byte data = (byte) config.getInt(node + STACK_DATA);
110
111 ItemStack stack = new ItemStack(type,amount,damage,data);
112 stack.setDurability( damage ); //ItemStack constructor with Data value overrides durability
113 return stack;
114 }
115
116 }

  ViewVC Help
Powered by ViewVC 1.1.20