use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.
the class SpongeGameRegistry method preRegistryInit.
public void preRegistryInit() {
CommonModuleRegistry.getInstance().registerDefaultModules();
final DirectedGraph<Class<? extends RegistryModule>> graph = new DirectedGraph<>();
for (RegistryModule module : this.registryModules) {
this.classMap.put(module.getClass(), module);
addToGraph(module, graph);
}
// Now we need ot do the catalog ones
for (CatalogRegistryModule<?> module : this.catalogRegistryMap.values()) {
this.classMap.put(module.getClass(), module);
addToGraph(module, graph);
}
try {
this.orderedModules.addAll(TopologicalOrder.createOrderedLoad(graph));
} catch (CyclicGraphException e) {
StringBuilder msg = new StringBuilder();
msg.append("Registry module dependencies are cyclical!\n");
msg.append("Dependency loops are:\n");
for (DataNode<?>[] cycle : e.getCycles()) {
msg.append("[");
for (DataNode<?> node : cycle) {
msg.append(node.getData().toString()).append(" ");
}
msg.append("]\n");
}
SpongeImpl.getLogger().fatal(msg.toString());
throw new RuntimeException("Registry modules dependencies error.");
}
registerModulePhase();
SpongeVillagerRegistry.registerVanillaTrades();
DataRegistrar.setupSerialization();
final List<Tuple<Class<? extends CatalogType>, CatalogRegistryModule<?>>> modules = new ArrayList<>();
for (Map.Entry<Class<? extends CatalogType>, CatalogRegistryModule<?>> entry : this.catalogRegistryMap.entrySet()) {
modules.add(new Tuple<>(entry.getKey(), entry.getValue()));
}
modules.sort(Comparator.comparing(tuple -> tuple.getFirst().getSimpleName()));
if (PRINT_CATALOG_TYPES) {
// Lol... this gets spammy really fast.... Probably at some point should be put to file.
final PrettyPrinter printer = new PrettyPrinter(100).add("Printing all Catalogs and their ID's").centre().hr().addWrapped("This is a test to print out all registered catalogs during initialization for their mapping, id's, and objects themselves.");
for (Tuple<Class<? extends CatalogType>, CatalogRegistryModule<?>> module : modules) {
printer.add(" %s : %s", "CatalogType", module.getFirst().getSimpleName());
final Collection<? extends CatalogType> all = module.getSecond().getAll();
final List<CatalogType> catalogTypes = new ArrayList<>(all);
catalogTypes.sort(Comparator.comparing(CatalogType::getId));
for (CatalogType catalogType : catalogTypes) {
printer.add(" -%s", catalogType.getId());
}
printer.hr();
}
printer.trace(System.err, SpongeImpl.getLogger(), Level.DEBUG);
}
}
use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.
the class SpongeHooks method getFriendlyCauseName.
public static String getFriendlyCauseName(Cause cause) {
String causedBy = "Unknown";
Object rootCause = cause.root();
if (rootCause instanceof User) {
User user = (User) rootCause;
causedBy = user.getName();
} else if (rootCause instanceof EntityItem) {
EntityItem item = (EntityItem) rootCause;
causedBy = item.getItem().getDisplayName();
} else if (rootCause instanceof Entity) {
Entity causeEntity = (Entity) rootCause;
causedBy = causeEntity.getName();
} else if (rootCause instanceof BlockSnapshot) {
BlockSnapshot snapshot = (BlockSnapshot) rootCause;
causedBy = snapshot.getState().getType().getId();
} else if (rootCause instanceof CatalogType) {
CatalogType type = (CatalogType) rootCause;
causedBy = type.getId();
} else if (rootCause instanceof PluginContainer) {
PluginContainer plugin = (PluginContainer) rootCause;
causedBy = plugin.getId();
} else {
causedBy = rootCause.getClass().getName();
}
return causedBy;
}
use of org.spongepowered.api.CatalogType in project SpongeCommon by SpongePowered.
the class CatalogTypeClassesTest method testCatalogFieldExists.
@Test
public void testCatalogFieldExists() throws Exception {
try {
final CatalogType o = (CatalogType) this.targetedField.get(null);
// Validates that the field is not a dummy object. If it is, it will throw an exception.
o.getId();
} catch (Exception e) {
this.isDummy = true;
throw e;
}
}
use of org.spongepowered.api.CatalogType in project LanternServer by LanternPowered.
the class MemoryDataView method setCollection.
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setCollection(String key, Collection<?> value) {
final ImmutableList.Builder<Object> builder = ImmutableList.builder();
final LanternDataManager manager = getDataManager();
for (Object object : value) {
if (object instanceof DataSerializable) {
builder.add(((DataSerializable) object).toContainer());
} else if (object instanceof DataView) {
if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
final MemoryDataView view = new MemoryDataContainer(this.safety);
final DataView internalView = (DataView) object;
for (Map.Entry<DataQuery, Object> entry : internalView.getValues(false).entrySet()) {
view.set(entry.getKey(), entry.getValue());
}
builder.add(view);
} else {
builder.add(object);
}
} else if (object instanceof CatalogType) {
builder.add(((CatalogType) object).getId());
} else if (object instanceof Map) {
builder.add(ensureSerialization((Map) object));
} else if (object instanceof Collection) {
builder.add(ensureSerialization((Collection) object));
} else {
final TypeToken<?> typeToken = TypeToken.of(object.getClass());
final DataTypeSerializer serializer = manager == null ? null : manager.getTypeSerializer(typeToken).orElse(null);
if (serializer != null) {
final Object result = serializer.serialize(typeToken, manager.getTypeSerializerContext(), object);
checkArgument(!result.equals(this), "Cannot insert self-referencing Objects!");
builder.add(result);
} else {
builder.add(object);
}
}
}
this.map.put(key, builder.build());
}
use of org.spongepowered.api.CatalogType in project LanternServer by LanternPowered.
the class MemoryDataView method set.
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public DataView set(DataQuery path, Object value) {
checkNotNull(path, "path");
checkNotNull(value, "value");
final LanternDataManager manager = getDataManager();
final List<String> parts = path.getParts();
final String key = parts.get(0);
if (parts.size() > 1) {
final DataQuery subQuery = of(key);
final Optional<DataView> subViewOptional = getUnsafeView(subQuery);
final DataView subView;
if (!subViewOptional.isPresent()) {
createView(subQuery);
subView = (DataView) this.map.get(key);
} else {
subView = subViewOptional.get();
}
subView.set(path.popFirst(), value);
return this;
}
Optional<DataTypeSerializer> optDataTypeSerializer;
TypeToken typeToken;
if (value instanceof DataView) {
checkArgument(value != this, "Cannot set a DataView to itself.");
// always have to copy a data view to avoid overwriting existing
// views and to set the interior path correctly.
copyDataView(path, (DataView) value);
} else if (value instanceof DataSerializable) {
final DataContainer valueContainer = ((DataSerializable) value).toContainer();
checkArgument(!(valueContainer).equals(this), "Cannot insert self-referencing DataSerializable");
// see above for why this is copied
copyDataView(path, valueContainer);
} else if (value instanceof CatalogType) {
return set(path, ((CatalogType) value).getId());
} else if (value instanceof Integer || value instanceof Byte || value instanceof Short || value instanceof Float || value instanceof Double || value instanceof Long || value instanceof String || value instanceof Character || value instanceof Boolean) {
this.map.put(key, value);
return this;
} else if (manager != null && (optDataTypeSerializer = manager.getTypeSerializer(typeToken = TypeToken.of(value.getClass()))).isPresent()) {
final DataTypeSerializer serializer = optDataTypeSerializer.get();
final Object serialized = serializer.serialize(typeToken, manager.getTypeSerializerContext(), value);
if (serialized instanceof DataContainer) {
final DataContainer container = (DataContainer) serialized;
checkArgument(!container.equals(this), "Cannot insert self-referencing Objects!");
// see above for why this is copied
copyDataView(path, container);
} else {
this.map.put(key, serialized);
}
} else if (value instanceof Collection) {
setCollection(key, (Collection) value);
} else if (value instanceof Map) {
setMap(key, (Map) value);
} else if (value.getClass().isArray()) {
if (this.safety == SafetyMode.ALL_DATA_CLONED || this.safety == SafetyMode.CLONED_ON_SET) {
if (value instanceof byte[]) {
this.map.put(key, ArrayUtils.clone((byte[]) value));
} else if (value instanceof short[]) {
this.map.put(key, ArrayUtils.clone((short[]) value));
} else if (value instanceof int[]) {
this.map.put(key, ArrayUtils.clone((int[]) value));
} else if (value instanceof long[]) {
this.map.put(key, ArrayUtils.clone((long[]) value));
} else if (value instanceof float[]) {
this.map.put(key, ArrayUtils.clone((float[]) value));
} else if (value instanceof double[]) {
this.map.put(key, ArrayUtils.clone((double[]) value));
} else if (value instanceof boolean[]) {
this.map.put(key, ArrayUtils.clone((boolean[]) value));
} else {
this.map.put(key, ArrayUtils.clone((Object[]) value));
}
} else {
this.map.put(key, value);
}
} else {
this.map.put(key, value);
}
return this;
}
Aggregations