Skip to content

Commit

Permalink
beta…?
Browse files Browse the repository at this point in the history
  • Loading branch information
jjkoletar committed Jan 7, 2013
1 parent 4bf17c1 commit f2504dc
Show file tree
Hide file tree
Showing 8 changed files with 824 additions and 47 deletions.
Binary file added lib/Vault-1.2.22.jar
Binary file not shown.
51 changes: 48 additions & 3 deletions src/com/koletar/jj/chestkeeper/CKChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,19 @@ public CKChest(Map<String, Object> me) {
if (me.size() - 2 != SMALL_CHEST_SIZE && me.size() - 2 != LARGE_CHEST_SIZE) { //Minus two offsets for the == and _title
throw new IllegalArgumentException("Size of item list is not the size of a large or small chest");
}
contents = new ItemStack[me.size() - 1];
contents = new ItemStack[me.size() - 2];
for (Map.Entry<String, Object> entry : me.entrySet()) {
if (entry.getKey().equalsIgnoreCase("_title")) {
title = entry.getValue().toString();
continue;
} else if (entry.getKey().equalsIgnoreCase("==")) {
continue;
}
int i = -1;
try {
i = Integer.valueOf(entry.getKey());
} catch (NumberFormatException nfe) {
throw new IllegalArgumentException("A key wasn't an integer");
throw new IllegalArgumentException("A key wasn't an integer, " + entry.getKey());
}
ItemStack is;
try {
Expand All @@ -58,7 +60,7 @@ public CKChest(Map<String, Object> me) {
public Map<String, Object> serialize() {
Map<String, Object> me = new HashMap<String, Object>();
for (int i = 0; i < contents.length; i++) {
me.put(String.valueOf(i), contents[i] == null ? new ItemStack(0) : contents[i]);
me.put(String.valueOf(i), contents[i]);
}
me.put("_title", title);
return me;
Expand Down Expand Up @@ -101,6 +103,17 @@ public void kick() {
}
}

public void empty() {
if (inventory != null) {
if (modified) {
inventory.clear();
} else {
contents = new ItemStack[contents.length];
modified = true;
}
}
}

private static String makeMagic(int magic) {
StringBuilder sb = new StringBuilder();
char[] digits = String.valueOf(magic).toCharArray();
Expand All @@ -110,4 +123,36 @@ private static String makeMagic(int magic) {
}
return sb.toString();
}

public void setName(String name) {
this.title = name;
}

public boolean isLargeChest() {
return contents.length == LARGE_CHEST_SIZE;
}

public boolean upgrade() {
if (contents.length == LARGE_CHEST_SIZE) {
return false;
}
kick();
save();
ItemStack[] newContents = new ItemStack[LARGE_CHEST_SIZE];
System.arraycopy(contents, 0, newContents, 0, contents.length);
contents = newContents;
inventory = null;
return true;
}

public String getTitle() {
return title;
}

protected void setItems(ItemStack[] in) {
contents = new ItemStack[contents.length];
for (int i = 0; i < in.length && i < contents.length; i++) {
contents[i] = in[i];
}
}
}
560 changes: 540 additions & 20 deletions src/com/koletar/jj/chestkeeper/CKFacilitator.java

Large diffs are not rendered by default.

122 changes: 106 additions & 16 deletions src/com/koletar/jj/chestkeeper/CKUser.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
package com.koletar.jj.chestkeeper;

import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

Expand Down Expand Up @@ -60,10 +67,6 @@ public int hashCode() {
return username.hashCode() + 1;
}

public boolean createChest(boolean isLargeChest) {
return createChest(isLargeChest ? "large" + (chests.size() + 1) : "normal" + (chests.size() + 1), isLargeChest);
}

public boolean createChest(String name, boolean isLargeChest) {
if (name.equalsIgnoreCase("defaultChest") || name.equalsIgnoreCase("username") || name.equalsIgnoreCase("magic") || chests.containsKey(name.toLowerCase())) {
return false;
Expand All @@ -77,9 +80,12 @@ public CKChest getChest() {
return chests.get(key);
}

public Inventory openChest() { //TODO: remember to validate renaming the default chest
public CKChest getChest(String name) {
return chests.get(name.toLowerCase());
}

public Inventory openChest() {
String key = (defaultChest == null || defaultChest.equals("")) && chests.size() > 0 ? chests.firstKey() : defaultChest;
ChestKeeper.trace(key);
return openChest(key);
}

Expand All @@ -101,16 +107,6 @@ public boolean save(Inventory inventory) {
return false;
}

public boolean isModified() {
boolean combo = true;
for (CKChest chest : inventoryPairings.values()) {
if (chest.isModified()) {
combo = false;
}
}
return combo;
}

public void forceClean() {
for (CKChest chest : inventoryPairings.values()) {
chest.kick();
Expand All @@ -121,4 +117,98 @@ public void forceClean() {
public String getUsername() {
return username;
}

public int getNumberOfChests() {
return chests.size();
}

public Set<String> getChestNames() {
return chests.keySet();
}

public boolean removeChest(String name) {
if (chests.containsKey(name.toLowerCase())) {
chests.remove(name.toLowerCase());
return true;
}
return false;
}

public boolean setDefaultChest(String name) {
if (chests.containsKey(name.toLowerCase())) {
defaultChest = name.toLowerCase();
return true;
}
return false;
}

public boolean hasChest(String name) {
return chests.containsKey(name.toLowerCase());
}

public void mv(String from, String to) {
CKChest chest = chests.get(from.toLowerCase());
chest.setName(to);
chests.remove(from.toLowerCase());
chests.put(to.toLowerCase(), chest);
if (defaultChest.equalsIgnoreCase(from)) {
defaultChest = to.toLowerCase();
}
}

public void fromVC(BufferedReader chestYml, String defaultChest) {
String currentChest = null;
boolean isLargeChest = false;
boolean areReadingItems = false;
ItemStack currentItem = null;
List<ItemStack> items = new LinkedList<ItemStack>();
try {
boolean done = false;
while (!done) {
String line = chestYml.readLine();
if (line != null) {
if (line.equals("")) {
//Skip
} else if (!line.substring(0, 1).equals(" ")) {
if (currentChest != null) {
CKChest chest = new CKChest(currentChest, isLargeChest);
if (currentItem != null) {
items.add(currentItem);
}
chest.setItems(items.toArray(new ItemStack[items.size()]));
chests.put(currentChest.toLowerCase(), chest);
}
currentChest = line.substring(0, line.length() - 1);
} else if (line.startsWith(" type: ")) {
isLargeChest = line.contains("large");
} else if (line.startsWith(" eitems:")) {
areReadingItems = true;
} else if (areReadingItems && line.equals(" - !!com.aranai.virtualchest.ItemStackSave")) {
if (currentItem != null) {
items.add(currentItem);
}
currentItem = new ItemStack(1);
} else if (areReadingItems && line.startsWith(" count: ")) {
int i = Integer.valueOf(line.substring(11));
currentItem.setAmount(i);
} else if (areReadingItems && line.startsWith(" damage: ")) {
short s = Short.valueOf(line.substring(12));
currentItem.setDurability(s);
} else if (areReadingItems && line.startsWith(" id: ")) {
int id = Integer.valueOf(line.substring(8));
currentItem.setTypeId(id);
} else if (areReadingItems && line.startsWith(" ")) {
String ench = line.substring(6);
String[] bits = ench.split(": ");
currentItem.addUnsafeEnchantment(Enchantment.getById(Integer.valueOf(bits[0])), Integer.valueOf(bits[1]));
}
} else {
done = true;
}
}
} catch (IOException e) {
e.printStackTrace();
}
setDefaultChest(defaultChest);
}
}
Loading

0 comments on commit f2504dc

Please sign in to comment.