diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b69915f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,116 @@
+### Java ###
+*.class
+
+# Mobile Tools for Java (J2ME)
+.mtj.tmp/
+
+# Package Files #
+*.jar
+*.war
+*.ear
+
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
+hs_err_pid*
+
+
+### Maven ###
+target/
+pom.xml.tag
+pom.xml.releaseBackup
+pom.xml.versionsBackup
+pom.xml.next
+release.properties
+dependency-reduced-pom.xml
+buildNumber.properties
+.mvn/timing.properties
+
+
+### Eclipse ###
+*.pydevproject
+.metadata
+.gradle
+bin/
+tmp/
+*.tmp
+*.bak
+*.swp
+*~.nib
+local.properties
+.settings/
+.loadpath
+
+# Eclipse Core
+.project
+
+# External tool builders
+.externalToolBuilders/
+
+# Locally stored "Eclipse launch configurations"
+*.launch
+
+# CDT-specific
+.cproject
+
+# JDT-specific (Eclipse Java Development Tools)
+.classpath
+
+# Java annotation processor (APT)
+.factorypath
+
+# PDT-specific
+.buildpath
+
+# sbteclipse plugin
+.target
+
+# TeXlipse plugin
+.texlipse
+
+
+### Intellij ###
+# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio
+
+*.iml
+
+## Directory-based project format:
+.idea/
+# if you remove the above rule, at least ignore the following:
+
+# User-specific stuff:
+# .idea/workspace.xml
+# .idea/tasks.xml
+# .idea/dictionaries
+
+# Sensitive or high-churn files:
+# .idea/dataSources.ids
+# .idea/dataSources.xml
+# .idea/sqlDataSources.xml
+# .idea/dynamic.xml
+# .idea/uiDesigner.xml
+
+# Gradle:
+# .idea/gradle.xml
+# .idea/libraries
+
+# Mongo Explorer plugin:
+# .idea/mongoSettings.xml
+
+## File-based project format:
+*.ipr
+*.iws
+
+## Plugin-specific files:
+
+# IntelliJ
+/out/
+
+# mpeltonen/sbt-idea plugin
+.idea_modules/
+
+# JIRA plugin
+atlassian-ide-plugin.xml
+
+# Crashlytics plugin (for Android Studio and IntelliJ)
+com_crashlytics_export_strings.xml
+crashlytics.properties
+crashlytics-build.properties
diff --git a/AttributeStorage/.classpath b/AttributeStorage/.classpath
deleted file mode 100644
index fd7ad7f..0000000
--- a/AttributeStorage/.classpath
+++ /dev/null
@@ -1,26 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/AttributeStorage/.project b/AttributeStorage/.project
deleted file mode 100644
index 779621f..0000000
--- a/AttributeStorage/.project
+++ /dev/null
@@ -1,23 +0,0 @@
-
-
- AttributeStorage
-
-
-
-
-
- org.eclipse.jdt.core.javabuilder
-
-
-
-
- org.eclipse.m2e.core.maven2Builder
-
-
-
-
-
- org.eclipse.jdt.core.javanature
- org.eclipse.m2e.core.maven2Nature
-
-
diff --git a/AttributeStorage/.settings/org.eclipse.core.resources.prefs b/AttributeStorage/.settings/org.eclipse.core.resources.prefs
deleted file mode 100644
index f9fe345..0000000
--- a/AttributeStorage/.settings/org.eclipse.core.resources.prefs
+++ /dev/null
@@ -1,4 +0,0 @@
-eclipse.preferences.version=1
-encoding//src/main/java=UTF-8
-encoding//src/test/java=UTF-8
-encoding/=UTF-8
diff --git a/AttributeStorage/.settings/org.eclipse.jdt.core.prefs b/AttributeStorage/.settings/org.eclipse.jdt.core.prefs
deleted file mode 100644
index 60105c1..0000000
--- a/AttributeStorage/.settings/org.eclipse.jdt.core.prefs
+++ /dev/null
@@ -1,5 +0,0 @@
-eclipse.preferences.version=1
-org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
-org.eclipse.jdt.core.compiler.compliance=1.6
-org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
-org.eclipse.jdt.core.compiler.source=1.6
diff --git a/AttributeStorage/.settings/org.eclipse.m2e.core.prefs b/AttributeStorage/.settings/org.eclipse.m2e.core.prefs
deleted file mode 100644
index f897a7f..0000000
--- a/AttributeStorage/.settings/org.eclipse.m2e.core.prefs
+++ /dev/null
@@ -1,4 +0,0 @@
-activeProfiles=
-eclipse.preferences.version=1
-resolveWorkspaceProjects=true
-version=1
diff --git a/AttributeStorage/pom.xml b/AttributeStorage/pom.xml
deleted file mode 100644
index 603dc18..0000000
--- a/AttributeStorage/pom.xml
+++ /dev/null
@@ -1,142 +0,0 @@
-
- 4.0.0
-
- com.comphenix.attribute
- AttributeStorage
- 0.0.2-SNAPSHOT
- jar
-
- AttributeStorage
- http://maven.apache.org
-
-
-
- comphenix-releases
- Comphenix Maven Releases
- http://repo.comphenix.net/content/repositories/releases/
-
-
- comphenix-snapshots
- Comphenix Maven Snapshots
- http://repo.comphenix.net/content/repositories/snapshots/
-
-
-
-
-
- bukkit-rep
- http://repo.bukkit.org/content/groups/public
-
-
-
-
- UTF-8
- 1.5
-
-
-
-
-
- maven-compiler-plugin
- 2.3.2
-
- 1.6
- 1.6
-
-
-
-
-
-
-
- release-sign-artifacts
-
-
- performRelease
- true
-
-
-
-
-
- org.apache.maven.plugins
- maven-source-plugin
- 2.2
-
-
- attach-sources
-
- jar
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-javadoc-plugin
- 2.9
-
-
- attach-javadocs
-
- jar
-
-
-
-
-
-
- org.apache.maven.plugins
- maven-gpg-plugin
- 1.4
-
-
- sign-artifacts
- verify
-
- sign
-
-
-
-
-
-
-
-
-
-
-
-
- org.bukkit
- craftbukkit
- 1.7.8-R0.1-SNAPSHOT
- provided
-
-
- junit
- junit
- 4.11
- test
-
-
- org.mockito
- mockito-all
- 1.8.4
- test
-
-
- org.powermock
- powermock-module-junit4
- ${powermock.version}
- test
-
-
- org.powermock
- powermock-api-mockito
- ${powermock.version}
- test
-
-
-
diff --git a/AttributeStorage/src/test/java/com/comphenix/attribute/NbtFactoryTest.java b/AttributeStorage/src/test/java/com/comphenix/attribute/NbtFactoryTest.java
deleted file mode 100644
index cfa7a76..0000000
--- a/AttributeStorage/src/test/java/com/comphenix/attribute/NbtFactoryTest.java
+++ /dev/null
@@ -1,124 +0,0 @@
-// The MIT License (MIT)
-//
-// Copyright (c) 2015 Kristian Stangeland
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
-// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
-// Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
-// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-package com.comphenix.attribute;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.Arrays;
-
-import net.minecraft.server.v1_7_R3.NBTTagCompound;
-import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemFactory;
-
-import org.bukkit.Material;
-import org.bukkit.inventory.ItemStack;
-import org.bukkit.inventory.meta.ItemMeta;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.powermock.core.classloader.annotations.PrepareForTest;
-
-import com.comphenix.attribute.NbtFactory.NbtCompound;
-import com.comphenix.attribute.NbtFactory.StreamOptions;
-import com.comphenix.attribute.mocking.BukkitInitialization;
-import com.google.common.io.ByteStreams;
-import com.google.common.io.OutputSupplier;
-
-/**
- * Unit test for simple App.
- */
-@RunWith(org.powermock.modules.junit4.PowerMockRunner.class)
-@PrepareForTest(CraftItemFactory.class)
-public class NbtFactoryTest {
- @BeforeClass
- public static void initializeMeta() throws IllegalAccessException {
- BukkitInitialization.initializeItemMeta();
- }
-
- @Test
- public void testCompound() {
- NbtCompound compound = createTestCompound();
-
- // Simple test
- verifyCompound(compound);
- }
-
- private void verifyCompound(NbtCompound compound) {
- // Verify the NBT content
- assertEquals(NBTTagCompound.class, compound.getHandle().getClass());
- assertEquals(2009, (int)compound.getInteger("released", 0));
- assertEquals("Minecraft", compound.getString("game", ""));
- assertEquals(Arrays.asList(1, 2, 3), compound.getList("list", false));
- assertEquals("Markus Persson", compound.getPath("author.name"));
- assertEquals("Kristian Stangeland", compound.getPath("fan.name"));
-
- assertNull("Missing root path was not NULL", compound.getPath("missing.test"));
- }
-
- private NbtCompound createTestCompound() {
- // Use NbtFactory.fromCompound(obj); to load from a NBTCompound class.
- NbtCompound compound = NbtFactory.createCompound();
- NbtCompound author = NbtFactory.createCompound();
-
- compound.put("released", 2009);
- compound.put("game", "Minecraft");
- compound.put("author", author);
- compound.put("bytes", new byte[] { 1, 2, 3 });
- compound.put("integers", new int[] { 1, 2, 3});
- compound.put("list", NbtFactory.createList(1, 2, 3));
-
- author.put("name", "Markus Persson");
- compound.putPath("fan.name", "Kristian Stangeland");
- return compound;
- }
-
- @Test
- public void testSaving() throws IOException {
- final ByteArrayOutputStream output = new ByteArrayOutputStream();
- final NbtCompound compound = createTestCompound();
-
- // Save the compound with compression
- compound.saveTo(new OutputSupplier() {
- public OutputStream getOutput() throws IOException {
- return output;
- }
- }, StreamOptions.GZIP_COMPRESSION);
-
- // Load the compound
- NbtCompound loaded = NbtFactory.fromStream(
- ByteStreams.newInputStreamSupplier(output.toByteArray()), StreamOptions.GZIP_COMPRESSION);
- verifyCompound(loaded);
- }
-
- @Test
- public void testItemMeta() {
- ItemStack stack = NbtFactory.getCraftItemStack(new ItemStack(Material.GOLD_AXE));
- NbtCompound other = NbtFactory.fromItemTag(stack);
-
- // Do whatever
- other.putPath("display.Name", "New display");
- other.putPath("display.Lore", NbtFactory.createList("Line 1", "Line 2"));
-
- ItemMeta meta = stack.getItemMeta();
- assertEquals("New display", meta.getDisplayName());
- assertEquals(Arrays.asList("Line 1", "Line 2"), meta.getLore());
- }
-}
diff --git a/AttributeStorage/src/test/java/com/comphenix/attribute/mocking/BukkitInitialization.java b/AttributeStorage/src/test/java/com/comphenix/attribute/mocking/BukkitInitialization.java
deleted file mode 100644
index 17cccce..0000000
--- a/AttributeStorage/src/test/java/com/comphenix/attribute/mocking/BukkitInitialization.java
+++ /dev/null
@@ -1,91 +0,0 @@
-// The MIT License (MIT)
-//
-// Copyright (c) 2015 Kristian Stangeland
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
-// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
-// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
-// Software is furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
-// Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
-// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
-// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-package com.comphenix.attribute.mocking;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-import java.lang.reflect.Field;
-
-import net.minecraft.server.v1_7_R3.Block;
-import net.minecraft.server.v1_7_R3.Item;
-import net.minecraft.server.v1_7_R3.StatisticList;
-
-// Will have to be updated for every version though
-import org.bukkit.craftbukkit.v1_7_R3.inventory.CraftItemFactory;
-
-import org.bukkit.Bukkit;
-import org.bukkit.Material;
-import org.bukkit.Server;
-import org.bukkit.inventory.ItemFactory;
-import org.bukkit.inventory.meta.ItemMeta;
-
-/**
- * Used to ensure that ProtocolLib and Bukkit is prepared to be tested.
- *
- * @author Kristian
- */
-public class BukkitInitialization {
- private static boolean initialized;
-
- /**
- * Initialize Bukkit and ProtocolLib such that we can perfrom unit testing.
- * @throws IllegalAccessException If we are unable to initialize Bukkit.
- */
- public static void initializeItemMeta() throws IllegalAccessException {
- if (!initialized) {
- // Denote that we're done
- initialized = true;
-
- try {
- Block.p();
- Item.l();
- StatisticList.a();
- } catch (Exception e) {
- // Swallow
- e.printStackTrace();
- }
-
- // Mock the server object
- Server mockedServer = mock(Server.class);
- ItemFactory mockedFactory = mock(CraftItemFactory.class);
- ItemMeta mockedMeta = mock(ItemMeta.class);
-
- when(mockedServer.getItemFactory()).thenReturn(mockedFactory);
- when(mockedServer.isPrimaryThread()).thenReturn(true);
- when(mockedFactory.getItemMeta(any(Material.class))).thenReturn(mockedMeta);
-
- // Inject this fake server
- setStaticField(Bukkit.class, "server", mockedServer);
-
- // And the fake item factory
- setStaticField(CraftItemFactory.class, "instance", mockedFactory);
- }
- }
-
- private static void setStaticField(Class> parent, String name, Object value) {
- try {
- Field field = parent.getDeclaredField(name);
- field.setAccessible(true);
- field.set(null, value);
- } catch (Exception e) {
- throw new IllegalArgumentException("Cannot set static field " + name + ".", e);
- }
- }
-}
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..f086329
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2015 Kristian Stangeland
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
diff --git a/README.md b/README.md
index 1a6a2d4..fa72cc4 100644
--- a/README.md
+++ b/README.md
@@ -1,25 +1,25 @@
# AttributeStorage
-Able to edit ItemStack attributes in Bukkit 1.7.2
+This library allows your plugin to edit ItemStack attributes in Bukkit/Spigot 1.7 - 1.8
-#License
-The MIT License (MIT)
-
-Copyright (c) 2015 Kristian Stangeland
+You can use this as maven dependency:
+Repository:
+```XML
+
+ comphenix-snapshots
+ Comphenix Maven Snapshots
+ http://repo.comphenix.net/content/repositories/snapshots/
+
+```
-Permission is hereby granted, free of charge, to any person obtaining a copy
-of this software and associated documentation files (the "Software"), to deal
-in the Software without restriction, including without limitation the rights
-to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the Software is
-furnished to do so, subject to the following conditions:
+Dependency:
+```XML
+
+ com.comphenix.attribute
+ AttributeStorage
+ 0.0.3-SNAPSHOT
+ compile
+
+```
-The above copyright notice and this permission notice shall be included in all
-copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-SOFTWARE.
+#License
+This project is under the the MIT License (MIT), look at the LICENSE file for more informations.
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..1849096
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,51 @@
+
+ 4.0.0
+
+ com.comphenix.attribute
+ AttributeStorage
+ 0.0.3-SNAPSHOT
+ jar
+
+ AttributeStorage
+ https://github.com/aadnk/AttributeStorage
+
+
+
+ spigot-repo
+ https://hub.spigotmc.org/nexus/content/repositories/snapshots/
+
+
+
+
+ UTF-8
+
+
+
+
+
+ maven-compiler-plugin
+ 3.6.2
+
+ 1.8
+ 1.8
+
+
+
+
+
+
+
+ org.spigotmc
+ spigot-api
+ 1.12.1-R0.1-SNAPSHOT
+ provided
+
+
+ javax.validation
+ validation-api
+ 2.0.0.Final
+ provided
+
+
+
diff --git a/AttributeStorage/src/main/java/com/comphenix/attribute/AttributeStorage.java b/src/main/java/com/comphenix/attribute/AttributeStorage.java
similarity index 74%
rename from AttributeStorage/src/main/java/com/comphenix/attribute/AttributeStorage.java
rename to src/main/java/com/comphenix/attribute/AttributeStorage.java
index f8e7b46..b0359bc 100644
--- a/AttributeStorage/src/main/java/com/comphenix/attribute/AttributeStorage.java
+++ b/src/main/java/com/comphenix/attribute/AttributeStorage.java
@@ -17,42 +17,45 @@
package com.comphenix.attribute;
-import java.util.UUID;
-
-import org.bukkit.inventory.ItemStack;
-
import com.comphenix.attribute.Attributes.Attribute;
import com.comphenix.attribute.Attributes.AttributeType;
import com.comphenix.attribute.Attributes.Operation;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
+import org.bukkit.inventory.ItemStack;
+
+import java.util.UUID;
/**
* Store meta-data in an ItemStack as attributes.
+ *
* @author Kristian
*/
+@SuppressWarnings({"unused", "WeakerAccess"})
public class AttributeStorage {
- private ItemStack target;
private final UUID uniqueKey;
-
+ private ItemStack target;
+
private AttributeStorage(ItemStack target, UUID uniqueKey) {
this.target = Preconditions.checkNotNull(target, "target cannot be NULL");
this.uniqueKey = Preconditions.checkNotNull(uniqueKey, "uniqueKey cannot be NULL");
}
-
+
/**
* Construct a new attribute storage system.
*
* The key must be the same in order to retrieve the same data.
- * @param target - the item stack where the data will be stored.
+ *
+ * @param target - the item stack where the data will be stored.
* @param uniqueKey - the unique key used to retrieve the correct data.
*/
public static AttributeStorage newTarget(ItemStack target, UUID uniqueKey) {
return new AttributeStorage(target, uniqueKey);
}
-
+
/**
* Retrieve the data stored in the item's attribute.
+ *
* @param defaultValue - the default value to return if no data can be found.
* @return The stored data, or defaultValue if not found.
*/
@@ -60,17 +63,19 @@ public String getData(String defaultValue) {
Attribute current = getAttribute(new Attributes(target), uniqueKey);
return current != null ? current.getName() : defaultValue;
}
-
+
/**
* Determine if we are storing any data.
+ *
* @return TRUE if we are, FALSE otherwise.
*/
public boolean hasData() {
- return getAttribute(new Attributes(target), uniqueKey) != null;
+ return getAttribute(new Attributes(target), uniqueKey) != null;
}
-
+
/**
* Set the data stored in the attributes.
+ *
* @param data - the data.
*/
public void setData(String data) {
@@ -79,55 +84,69 @@ public void setData(String data) {
if (current == null) {
attributes.add(
- Attribute.newBuilder().
- name(data).
- amount(getBaseDamage(target)).
- uuid(uniqueKey).
- operation(Operation.ADD_NUMBER).
- type(AttributeType.GENERIC_ATTACK_DAMAGE).
- build()
+ Attribute.newBuilder().
+ name(data).
+ amount(getBaseDamage(target)).
+ uuid(uniqueKey).
+ operation(Operation.ADD_NUMBER).
+ type(AttributeType.GENERIC_ATTACK_DAMAGE).
+ build()
);
} else {
current.setName(data);
}
this.target = attributes.getStack();
}
-
+
/**
* Retrieve the base damage of the given item.
+ *
* @param stack - the stack.
* @return The base damage.
*/
private int getBaseDamage(ItemStack stack) {
- // Yes - we have to hard code these values. Cannot use Operation.ADD_PERCENTAGE either.
- switch (stack.getType()) {
- case WOOD_SWORD: return 4;
- case GOLD_SWORD: return 4;
- case STONE_SWORD: return 5;
- case IRON_SWORD: return 6;
- case DIAMOND_SWORD: return 7;
-
- case WOOD_AXE: return 3;
- case GOLD_AXE: return 3;
- case STONE_AXE: return 4;
- case IRON_AXE: return 5;
- case DIAMOND_AXE: return 6;
- default: return 0;
- }
+ // Yes - we have to hard code these values. Cannot use Operation.ADD_PERCENTAGE either.
+ switch (stack.getType()) {
+ case WOOD_SWORD:
+ return 4;
+ case GOLD_SWORD:
+ return 4;
+ case STONE_SWORD:
+ return 5;
+ case IRON_SWORD:
+ return 6;
+ case DIAMOND_SWORD:
+ return 7;
+
+ case WOOD_AXE:
+ return 3;
+ case GOLD_AXE:
+ return 3;
+ case STONE_AXE:
+ return 4;
+ case IRON_AXE:
+ return 5;
+ case DIAMOND_AXE:
+ return 6;
+ default:
+ return 0;
+ }
}
-
+
/**
* Retrieve the target stack. May have been changed.
+ *
* @return The target stack.
*/
public ItemStack getTarget() {
return target;
}
-
+
/**
* Retrieve an attribute by UUID.
+ *
* @param attributes - the attribute.
- * @param id - the UUID to search for.
+ * @param id - the UUID to search for.
* @return The first attribute associated with this UUID, or NULL.
*/
private Attribute getAttribute(Attributes attributes, UUID id) {
diff --git a/AttributeStorage/src/main/java/com/comphenix/attribute/Attributes.java b/src/main/java/com/comphenix/attribute/Attributes.java
similarity index 81%
rename from AttributeStorage/src/main/java/com/comphenix/attribute/Attributes.java
rename to src/main/java/com/comphenix/attribute/Attributes.java
index db675bf..7e54893 100644
--- a/AttributeStorage/src/main/java/com/comphenix/attribute/Attributes.java
+++ b/src/main/java/com/comphenix/attribute/Attributes.java
@@ -17,39 +17,150 @@
package com.comphenix.attribute;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.UUID;
-import java.util.concurrent.ConcurrentMap;
-
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
-
-import org.bukkit.inventory.ItemStack;
-
import com.comphenix.attribute.NbtFactory.NbtCompound;
import com.comphenix.attribute.NbtFactory.NbtList;
-import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
+import org.bukkit.inventory.ItemStack;
+import javax.validation.constraints.NotNull;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentMap;
+
+@SuppressWarnings({"unused", "WeakerAccess"})
public class Attributes {
+ // This may be modified
+ private ItemStack stack;
+ private NbtList attributes;
+
+ public Attributes(ItemStack stack) {
+ // Create a CraftItemStack (under the hood)
+ this.stack = NbtFactory.getCraftItemStack(stack);
+ loadAttributes(false);
+ }
+
+ /**
+ * Load the NBT list from the TAG compound.
+ *
+ * @param createIfMissing - create the list if its missing.
+ */
+ private void loadAttributes(boolean createIfMissing) {
+ if (this.attributes == null) {
+ NbtCompound nbt = NbtFactory.fromItemTag(this.stack);
+ this.attributes = nbt.getList("AttributeModifiers", createIfMissing);
+ }
+ }
+
+ /**
+ * Remove the NBT list from the TAG compound.
+ */
+ private void removeAttributes() {
+ NbtCompound nbt = NbtFactory.fromItemTag(this.stack);
+ nbt.remove("AttributeModifiers");
+ this.attributes = null;
+ }
+
+ /**
+ * Retrieve the modified item stack.
+ *
+ * @return The modified item stack.
+ */
+ public ItemStack getStack() {
+ return stack;
+ }
+
+ /**
+ * Retrieve the number of attributes.
+ *
+ * @return Number of attributes.
+ */
+ public int size() {
+ return attributes != null ? attributes.size() : 0;
+ }
+
+ /**
+ * Add a new attribute to the list.
+ *
+ * @param attribute - the new attribute.
+ */
+ public void add(Attribute attribute) {
+ Preconditions.checkNotNull(attribute.getName(), "must specify an attribute name.");
+ loadAttributes(true);
+ attributes.add(attribute.data);
+ }
+
+ /**
+ * Remove the first instance of the given attribute.
+ *
+ * The attribute will be removed using its UUID.
+ *
+ * @param attribute - the attribute to remove.
+ * @return TRUE if the attribute was removed, FALSE otherwise.
+ */
+ public boolean remove(Attribute attribute) {
+ if (attributes == null)
+ return false;
+ UUID uuid = attribute.getUUID();
+
+ for (Iterator it = values().iterator(); it.hasNext(); ) {
+ if (Objects.equal(it.next().getUUID(), uuid)) {
+ it.remove();
+
+ // Last removed attribute?
+ if (size() == 0) {
+ removeAttributes();
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Remove every attribute.
+ */
+ public void clear() {
+ removeAttributes();
+ }
+
+ /**
+ * Retrieve the attribute at a given index.
+ *
+ * @param index - the index to look up.
+ * @return The attribute at that index.
+ */
+ public Attribute get(int index) {
+ if (size() == 0)
+ throw new IllegalStateException("Attribute list is empty.");
+ return new Attribute((NbtCompound) attributes.get(index));
+ }
+
+ // We can't make Attributes itself iterable without splitting it up into separate classes
+ public Iterable values() {
+ return () -> {
+ // Handle the empty case
+ if (size() == 0)
+ return Collections.emptyList().iterator();
+
+ return Iterators.transform(attributes.iterator(),
+ element -> new Attribute((NbtCompound) element));
+ };
+ }
+
public enum Operation {
ADD_NUMBER(0),
MULTIPLY_PERCENTAGE(1),
ADD_PERCENTAGE(2);
private int id;
-
- private Operation(int id) {
+
+ Operation(int id) {
this.id = id;
}
-
- public int getId() {
- return id;
- }
-
+
public static Operation fromId(int id) {
// Linear scan is very fast for small N
for (Operation op : values()) {
@@ -59,8 +170,12 @@ public static Operation fromId(int id) {
}
throw new IllegalArgumentException("Corrupt operation ID " + id + " detected.");
}
+
+ public int getId() {
+ return id;
+ }
}
-
+
public static class AttributeType {
private static ConcurrentMap LOOKUP = Maps.newConcurrentMap();
public static final AttributeType GENERIC_MAX_HEALTH = new AttributeType("generic.maxHealth").register();
@@ -68,53 +183,60 @@ public static class AttributeType {
public static final AttributeType GENERIC_ATTACK_DAMAGE = new AttributeType("generic.attackDamage").register();
public static final AttributeType GENERIC_MOVEMENT_SPEED = new AttributeType("generic.movementSpeed").register();
public static final AttributeType GENERIC_KNOCKBACK_RESISTANCE = new AttributeType("generic.knockbackResistance").register();
-
+ public static final AttributeType GENERIC_ARMOR = new AttributeType("generic.armor").register();
+ public static final AttributeType GENERIC_ARMOR_TOUGHNESS = new AttributeType("generic.armorToughness").register();
+
private final String minecraftId;
-
+
/**
* Construct a new attribute type.
*
* Remember to {@link #register()} the type.
+ *
* @param minecraftId - the ID of the type.
*/
public AttributeType(String minecraftId) {
this.minecraftId = minecraftId;
}
-
- /**
- * Retrieve the associated minecraft ID.
- * @return The associated ID.
- */
- public String getMinecraftId() {
- return minecraftId;
- }
-
- /**
- * Register the type in the central registry.
- * @return The registered type.
- */
- // Constructors should have no side-effects!
- public AttributeType register() {
- AttributeType old = LOOKUP.putIfAbsent(minecraftId, this);
- return old != null ? old : this;
- }
-
+
/**
* Retrieve the attribute type associated with a given ID.
+ *
* @param minecraftId The ID to search for.
* @return The attribute type, or NULL if not found.
*/
public static AttributeType fromId(String minecraftId) {
return LOOKUP.get(minecraftId);
}
-
+
/**
* Retrieve every registered attribute type.
+ *
* @return Every type.
*/
public static Iterable values() {
return LOOKUP.values();
}
+
+ /**
+ * Retrieve the associated minecraft ID.
+ *
+ * @return The associated ID.
+ */
+ public String getMinecraftId() {
+ return minecraftId;
+ }
+
+ /**
+ * Register the type in the central registry.
+ *
+ * @return The registered type.
+ */
+ // Constructors should have no side-effects!
+ public AttributeType register() {
+ AttributeType old = LOOKUP.putIfAbsent(minecraftId, this);
+ return old != null ? old : this;
+ }
}
public static class Attribute {
@@ -127,12 +249,22 @@ private Attribute(Builder builder) {
setAttributeType(builder.type);
setName(builder.name);
setUUID(builder.uuid);
+ setSlot(builder.slot);
}
-
+
private Attribute(NbtCompound data) {
this.data = data;
}
-
+
+ /**
+ * Construct a new attribute builder with a random UUID and default operation of adding numbers.
+ *
+ * @return The attribute builder.
+ */
+ public static Builder newBuilder() {
+ return new Builder().uuid(UUID.randomUUID()).operation(Operation.ADD_NUMBER);
+ }
+
public double getAmount() {
return data.getDouble("Amount", 0.0);
}
@@ -145,7 +277,7 @@ public Operation getOperation() {
return Operation.fromId(data.getInteger("Operation", 0));
}
- public void setOperation(@Nonnull Operation operation) {
+ public void setOperation(@NotNull Operation operation) {
Preconditions.checkNotNull(operation, "operation cannot be NULL.");
data.put("Operation", operation.getId());
}
@@ -154,7 +286,7 @@ public AttributeType getAttributeType() {
return AttributeType.fromId(data.getString("AttributeName", null));
}
- public void setAttributeType(@Nonnull AttributeType type) {
+ public void setAttributeType(@NotNull AttributeType type) {
Preconditions.checkNotNull(type, "type cannot be NULL.");
data.put("AttributeName", type.getMinecraftId());
}
@@ -163,7 +295,7 @@ public String getName() {
return data.getString("Name", null);
}
- public void setName(@Nonnull String name) {
+ public void setName(@NotNull String name) {
Preconditions.checkNotNull(name, "name cannot be NULL.");
data.put("Name", name);
}
@@ -172,20 +304,29 @@ public UUID getUUID() {
return new UUID(data.getLong("UUIDMost", null), data.getLong("UUIDLeast", null));
}
- public void setUUID(@Nonnull UUID id) {
+ public void setUUID(@NotNull UUID id) {
Preconditions.checkNotNull("id", "id cannot be NULL.");
data.put("UUIDLeast", id.getLeastSignificantBits());
data.put("UUIDMost", id.getMostSignificantBits());
}
- /**
- * Construct a new attribute builder with a random UUID and default operation of adding numbers.
- * @return The attribute builder.
- */
- public static Builder newBuilder() {
- return new Builder().uuid(UUID.randomUUID()).operation(Operation.ADD_NUMBER);
+ public String getSlot() {
+ return data.getString("Slot", null);
+ }
+
+ public void setSlot(String slot) {
+ Preconditions.checkNotNull(slot, "slot cannot be NULL.");
+ data.put("Slot", slot);
}
-
+
+ public NbtCompound getData() {
+ return data;
+ }
+
+ public void setData(NbtCompound data) {
+ this.data = data;
+ }
+
// Makes it easier to construct an attribute
public static class Builder {
private double amount;
@@ -193,154 +334,45 @@ public static class Builder {
private AttributeType type;
private String name;
private UUID uuid;
+ private String slot;
private Builder() {
// Don't make this accessible
}
-
+
public Builder amount(double amount) {
this.amount = amount;
return this;
}
+
public Builder operation(Operation operation) {
this.operation = operation;
return this;
}
+
public Builder type(AttributeType type) {
this.type = type;
return this;
}
+
public Builder name(String name) {
this.name = name;
return this;
}
+
public Builder uuid(UUID uuid) {
this.uuid = uuid;
return this;
}
+
+ public Builder slot(String slot) {
+ this.slot = slot;
+ return this;
+ }
+
public Attribute build() {
return new Attribute(this);
}
}
}
-
- // This may be modified
- public ItemStack stack;
- private NbtList attributes;
-
- public Attributes(ItemStack stack) {
- // Create a CraftItemStack (under the hood)
- this.stack = NbtFactory.getCraftItemStack(stack);
- loadAttributes(false);
- }
-
- /**
- * Load the NBT list from the TAG compound.
- * @param createIfMissing - create the list if its missing.
- */
- private void loadAttributes(boolean createIfMissing) {
- if (this.attributes == null) {
- NbtCompound nbt = NbtFactory.fromItemTag(this.stack);
- this.attributes = nbt.getList("AttributeModifiers", createIfMissing);
- }
- }
-
- /**
- * Remove the NBT list from the TAG compound.
- */
- private void removeAttributes() {
- NbtCompound nbt = NbtFactory.fromItemTag(this.stack);
- nbt.remove("AttributeModifiers");
- this.attributes = null;
- }
-
- /**
- * Retrieve the modified item stack.
- * @return The modified item stack.
- */
- public ItemStack getStack() {
- return stack;
- }
-
- /**
- * Retrieve the number of attributes.
- * @return Number of attributes.
- */
- public int size() {
- return attributes != null ? attributes.size() : 0;
- }
-
- /**
- * Add a new attribute to the list.
- * @param attribute - the new attribute.
- */
- public void add(Attribute attribute) {
- Preconditions.checkNotNull(attribute.getName(), "must specify an attribute name.");
- loadAttributes(true);
- attributes.add(attribute.data);
- }
-
- /**
- * Remove the first instance of the given attribute.
- *
- * The attribute will be removed using its UUID.
- * @param attribute - the attribute to remove.
- * @return TRUE if the attribute was removed, FALSE otherwise.
- */
- public boolean remove(Attribute attribute) {
- if (attributes == null)
- return false;
- UUID uuid = attribute.getUUID();
-
- for (Iterator it = values().iterator(); it.hasNext(); ) {
- if (Objects.equal(it.next().getUUID(), uuid)) {
- it.remove();
-
- // Last removed attribute?
- if (size() == 0) {
- removeAttributes();
- }
- return true;
- }
- }
- return false;
- }
-
- /**
- * Remove every attribute.
- */
- public void clear() {
- removeAttributes();
- }
-
- /**
- * Retrieve the attribute at a given index.
- * @param index - the index to look up.
- * @return The attribute at that index.
- */
- public Attribute get(int index) {
- if (size() == 0)
- throw new IllegalStateException("Attribute list is empty.");
- return new Attribute((NbtCompound) attributes.get(index));
- }
-
- // We can't make Attributes itself iterable without splitting it up into separate classes
- public Iterable values() {
- return new Iterable() {
- @Override
- public Iterator iterator() {
- // Handle the empty case
- if (size() == 0)
- return Collections.emptyList().iterator();
-
- return Iterators.transform(attributes.iterator(),
- new Function