[Bleeding] Added a Metadata framework for Entities, Blocks, and Worlds
This metadata implementation has the following features: - All metadata is lazy. Metadata values are not actually computed until another plugin requests them. Memory and CPU are conserved by not computing and storing unnecessary metadata values. - All metadata is cached. Once a metadata value is computed its value is cached in the metadata store to prevent further unnecessary computation. An invalidation mechanism is provided to flush the cache and force recompilation of metadata values. - All metadata is stored in basic data types. Convenience methods in the MetadataValue class allow for the conversion of metadata data types when possible. Restricting metadata to basic data types prevents the accidental linking of large object graphs into metadata. Metadata is persistent across the lifetime of the application and adding large object graphs would damage garbage collector performance. - Metadata access is thread safe. Care has been taken to protect the internal data structures and access them in a thread safe manner. - Metadata is exposed for all objects that descend from Entity, Block, and World. All Entity and World metadata is stored at the Server level and all Block metadata is stored at the World level. - Metadata is NOT keyed on references to original objects - instead metadata is keyed off of unique fields within those objects. Doing this allows metadata to exist for blocks that are in chunks not currently in memory. Additionally, Player objects are keyed off of player name so that Player metadata remains consistent between logins. - Metadata convenience methods have been added to all Entities, Players, Blocks, BlockStates, and World allowing direct access to an individual instance's metadata. - Players and OfflinePlayers share a single metadata store, allowing player metadata to be manipulated regardless of the player's current online status. By: rmichela <deltahat@gmail.com>
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
// Copyright (C) 2011 Ryan Michela
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package org.bukkit.metadata;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.messaging.TestPlugin;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class MetadataConversionTest {
|
||||
private Plugin plugin = new TestPlugin("x");
|
||||
private FixedMetadataValue subject;
|
||||
|
||||
private void setSubject(Object value) {
|
||||
subject = new FixedMetadataValue(plugin, value);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromInt() {
|
||||
setSubject(10);
|
||||
|
||||
assertEquals(10, subject.asInt());
|
||||
assertEquals(10, subject.asFloat(), 0.000001);
|
||||
assertEquals(10, subject.asDouble(), 0.000001);
|
||||
assertEquals(10, subject.asLong());
|
||||
assertEquals(10, subject.asShort());
|
||||
assertEquals(10, subject.asByte());
|
||||
assertEquals(true, subject.asBoolean());
|
||||
assertEquals("10", subject.asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromFloat() {
|
||||
setSubject(10.5);
|
||||
|
||||
assertEquals(10, subject.asInt());
|
||||
assertEquals(10.5, subject.asFloat(), 0.000001);
|
||||
assertEquals(10.5, subject.asDouble(), 0.000001);
|
||||
assertEquals(10, subject.asLong());
|
||||
assertEquals(10, subject.asShort());
|
||||
assertEquals(10, subject.asByte());
|
||||
assertEquals(true, subject.asBoolean());
|
||||
assertEquals("10.5", subject.asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromNumericString() {
|
||||
setSubject("10");
|
||||
|
||||
assertEquals(10, subject.asInt());
|
||||
assertEquals(10, subject.asFloat(), 0.000001);
|
||||
assertEquals(10, subject.asDouble(), 0.000001);
|
||||
assertEquals(10, subject.asLong());
|
||||
assertEquals(10, subject.asShort());
|
||||
assertEquals(10, subject.asByte());
|
||||
assertEquals(false, subject.asBoolean());
|
||||
assertEquals("10", subject.asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromNonNumericString() {
|
||||
setSubject("true");
|
||||
|
||||
assertEquals(0, subject.asInt());
|
||||
assertEquals(0, subject.asFloat(), 0.000001);
|
||||
assertEquals(0, subject.asDouble(), 0.000001);
|
||||
assertEquals(0, subject.asLong());
|
||||
assertEquals(0, subject.asShort());
|
||||
assertEquals(0, subject.asByte());
|
||||
assertEquals(true, subject.asBoolean());
|
||||
assertEquals("true", subject.asString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromNull() {
|
||||
setSubject(null);
|
||||
|
||||
assertEquals(0, subject.asInt());
|
||||
assertEquals(0, subject.asFloat(), 0.000001);
|
||||
assertEquals(0, subject.asDouble(), 0.000001);
|
||||
assertEquals(0, subject.asLong());
|
||||
assertEquals(0, subject.asShort());
|
||||
assertEquals(0, subject.asByte());
|
||||
assertEquals(false, subject.asBoolean());
|
||||
assertEquals("", subject.asString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user