use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class VanishableData method register.
// @formatter:off
@SuppressWarnings("unchecked")
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(VanishableBridge.class).create(Keys.IS_INVISIBLE).get(VanishableBridge::bridge$isInvisible).set(VanishableBridge::bridge$setInvisible).create(Keys.VANISH).get(bridge -> bridge.bridge$vanishState().invisible()).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
h.bridge$vanishState(v ? VanishState.vanished() : VanishState.unvanished());
return true;
}).create(Keys.VANISH_IGNORES_COLLISION).get(b -> b.bridge$vanishState().ignoresCollisions()).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
if (!h.bridge$vanishState().invisible()) {
return false;
}
h.bridge$vanishState(h.bridge$vanishState().ignoreCollisions(v));
return true;
}).create(Keys.VANISH_PREVENTS_TARGETING).get(b -> b.bridge$vanishState().untargetable()).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
if (!h.bridge$vanishState().invisible()) {
return false;
}
h.bridge$vanishState(h.bridge$vanishState().untargetable(v));
return true;
}).create(Keys.VANISH_STATE).get(VanishableBridge::bridge$vanishState).setAnd((h, v) -> {
if (h instanceof Entity && ((Entity) h).level.isClientSide) {
return false;
}
h.bridge$vanishState(v);
return true;
});
final ResourceKey dataStoreKey = ResourceKey.sponge("invisibility");
registrator.spongeDataStore(dataStoreKey, VanishableBridge.class, Keys.IS_INVISIBLE, Keys.VANISH_STATE);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_INVISIBLE, dataStoreKey, Keys.IS_INVISIBLE);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_VANISHED, dataStoreKey, Keys.VANISH_STATE);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNCOLLIDEABLE, dataStoreKey, Keys.VANISH_IGNORES_COLLISION);
SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNTARGETABLE, dataStoreKey, Keys.VANISH_PREVENTS_TARGETING);
}
use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class PotionItemStackData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.COLOR).get(h -> Color.ofRgb(PotionUtils.getColor(h))).set((h, v) -> {
final CompoundTag tag = h.getOrCreateTag();
tag.putInt(Constants.Item.CUSTOM_POTION_COLOR, v.rgb());
}).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_COLOR)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION).create(Keys.POTION_EFFECTS).get(h -> {
final List<MobEffectInstance> effects = PotionUtils.getMobEffects(h);
return effects.isEmpty() ? null : ImmutableList.copyOf((List<PotionEffect>) (Object) effects);
}).set((h, v) -> {
final CompoundTag tag = h.getOrCreateTag();
final ListTag list = v.stream().map(effect -> {
final CompoundTag potionTag = new CompoundTag();
((MobEffectInstance) effect).save(potionTag);
return potionTag;
}).collect(NBTCollectors.toTagList());
tag.put(Constants.Item.CUSTOM_POTION_EFFECTS, list);
}).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_EFFECTS)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW).create(Keys.POTION_TYPE).get(h -> (PotionType) PotionUtils.getPotion(h)).set((h, v) -> {
h.getOrCreateTag();
PotionUtils.setPotion(h, (Potion) v);
}).delete(h -> {
if (h.hasTag()) {
PotionUtils.setPotion(h, Potions.EMPTY);
}
}).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW);
}
use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.
the class SignItemStackData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.SIGN_LINES).get(h -> {
final CompoundTag tag = h.getTagElement(Constants.Item.BLOCK_ENTITY_TAG);
if (tag == null) {
return null;
}
final String id = tag.getString(Constants.Item.BLOCK_ENTITY_ID);
if (!id.equalsIgnoreCase(Constants.TileEntity.SIGN)) {
return null;
}
final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
final List<Component> texts = Lists.newArrayListWithCapacity(4);
for (int i = 0; i < 4; i++) {
texts.add(gcs.deserialize(tag.getString("Text" + (i + 1))));
}
return texts;
}).set((h, v) -> {
final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
final CompoundTag tag = h.getOrCreateTagElement(Constants.Item.BLOCK_ENTITY_TAG);
tag.putString(Constants.Item.BLOCK_ENTITY_ID, Constants.TileEntity.SIGN);
for (int i = 0; i < 4; i++) {
final Component line = v.size() > i ? v.get(i) : Component.empty();
if (line == null) {
throw new IllegalArgumentException("A null line was given at index " + i);
}
tag.putString("Text" + (i + 1), gcs.serialize(line));
}
}).delete(h -> h.removeTagKey(Constants.Item.BLOCK_ENTITY_TAG));
}
Aggregations