use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class ServerGamePacketListenerImplMixin method impl$onRightClickAtEntity.
@Inject(method = "handleInteract", cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;interactAt(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/phys/Vec3;Lnet/minecraft/world/InteractionHand;)Lnet/minecraft/world/InteractionResult;"))
public void impl$onRightClickAtEntity(final ServerboundInteractPacket p_147340_1, final CallbackInfo ci) {
final Entity entity = p_147340_1.getTarget(this.player.getLevel());
final ItemStack itemInHand = p_147340_1.getHand() == null ? ItemStack.EMPTY : this.player.getItemInHand(p_147340_1.getHand());
final InteractEntityEvent.Secondary event = SpongeCommonEventFactory.callInteractEntityEventSecondary(this.player, itemInHand, entity, p_147340_1.getHand(), VecHelper.toVector3d(p_147340_1.getLocation()));
if (event.isCancelled()) {
ci.cancel();
} else {
this.impl$ignorePackets++;
}
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class ServerLevelMixin method impl$throwBroadcastEvent.
@Inject(method = "levelEvent", at = @At("HEAD"), cancellable = true)
private void impl$throwBroadcastEvent(final Player player, final int eventID, final BlockPos pos, final int dataID, CallbackInfo ci) {
if (eventID == Constants.WorldEvents.PLAY_RECORD_EVENT && ShouldFire.PLAY_SOUND_EVENT_RECORD) {
try (final CauseStackManager.StackFrame frame = Sponge.server().causeStackManager().pushCauseFrame()) {
final BlockEntity tileEntity = this.shadow$getBlockEntity(pos);
if (tileEntity instanceof JukeboxBlockEntity) {
final JukeboxBlockEntity jukebox = (JukeboxBlockEntity) tileEntity;
final ItemStack record = jukebox.getRecord();
frame.pushCause(jukebox);
frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(record));
if (!record.isEmpty()) {
final Optional<MusicDisc> recordProperty = ((org.spongepowered.api.item.inventory.ItemStack) (Object) record).get(Keys.MUSIC_DISC);
if (!recordProperty.isPresent()) {
// Safeguard for https://github.com/SpongePowered/SpongeCommon/issues/2337
return;
}
final MusicDisc recordType = recordProperty.get();
final PlaySoundEvent.Record event = SpongeCommonEventFactory.callPlaySoundRecordEvent(frame.currentCause(), jukebox, recordType, dataID);
if (event.isCancelled()) {
ci.cancel();
}
}
}
}
}
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class ItemStackData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.APPLICABLE_POTION_EFFECTS).get(h -> {
if (h.isEdible()) {
final List<Pair<MobEffectInstance, Float>> itemEffects = h.getItem().getFoodProperties().getEffects();
final WeightedTable<PotionEffect> effects = new WeightedTable<>();
final ChanceTable<PotionEffect> chance = new ChanceTable<>();
for (final Pair<MobEffectInstance, Float> effect : itemEffects) {
chance.add((PotionEffect) effect.getFirst(), effect.getSecond());
}
effects.add(new NestedTableEntry<>(1, chance));
return effects;
}
return null;
}).create(Keys.BURN_TIME).get(h -> {
final Integer burnTime = AbstractFurnaceBlockEntity.getFuel().get(h.getItem());
if (burnTime != null && burnTime > 0) {
return burnTime;
}
return null;
}).create(Keys.CAN_HARVEST).get(h -> {
final Item item = h.getItem();
if (item instanceof DiggerItemAccessor && !(item instanceof PickaxeItem)) {
final Set<Block> blocks = ((DiggerItemAccessor) item).accessor$blocks();
return ImmutableSet.copyOf((Set<BlockType>) (Object) blocks);
}
final Set<BlockType> blockTypes = Registry.BLOCK.stream().filter(b -> item.isCorrectToolForDrops(b.defaultBlockState())).map(BlockType.class::cast).collect(ImmutableSet.toImmutableSet());
return blockTypes.isEmpty() ? null : blockTypes;
}).create(Keys.CONTAINER_ITEM).get(h -> (ItemType) h.getItem().getCraftingRemainingItem()).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(h.getDisplayName())).create(Keys.CUSTOM_MODEL_DATA).get(h -> {
final CompoundTag tag = h.getTag();
if (tag == null || !tag.contains(Constants.Item.CUSTOM_MODEL_DATA, Constants.NBT.TAG_INT)) {
return null;
}
return tag.getInt(Constants.Item.CUSTOM_MODEL_DATA);
}).set((h, v) -> {
final CompoundTag tag = h.getOrCreateTag();
tag.putInt(Constants.Item.CUSTOM_MODEL_DATA, v);
}).delete(h -> {
final CompoundTag tag = h.getTag();
if (tag != null) {
tag.remove(Constants.Item.CUSTOM_MODEL_DATA);
}
}).create(Keys.CUSTOM_NAME).get(h -> {
if (h.hasCustomHoverName()) {
return SpongeAdventure.asAdventure(h.getHoverName());
}
if (h.getItem() == Items.WRITTEN_BOOK) {
// When no custom name is set on a written book fallback to its title
// The custom name has a higher priority than the title so no setter is needed.
final CompoundTag tag = h.getTag();
if (tag != null) {
final String title = tag.getString(Constants.Item.Book.ITEM_BOOK_TITLE);
return LegacyComponentSerializer.legacySection().deserialize(title);
}
}
return null;
}).set((h, v) -> h.setHoverName(SpongeAdventure.asVanilla(v))).delete(ItemStack::resetHoverName).create(Keys.IS_UNBREAKABLE).get(h -> {
final CompoundTag tag = h.getTag();
if (tag == null || !tag.contains(Constants.Item.ITEM_UNBREAKABLE, Constants.NBT.TAG_BYTE)) {
return false;
}
return tag.getBoolean(Constants.Item.ITEM_UNBREAKABLE);
}).set(ItemStackData::setIsUnbrekable).delete(h -> ItemStackData.setIsUnbrekable(h, false)).create(Keys.LORE).get(h -> {
final CompoundTag tag = h.getTag();
if (tag == null || !tag.contains(Constants.Item.ITEM_DISPLAY)) {
return null;
}
final CompoundTag displayCompound = tag.getCompound(Constants.Item.ITEM_DISPLAY);
final ListTag list = displayCompound.getList(Constants.Item.ITEM_LORE, Constants.NBT.TAG_STRING);
return list.isEmpty() ? null : SpongeAdventure.json(list.stream().collect(NBTCollectors.toStringList()));
}).set((h, v) -> {
if (v.isEmpty()) {
ItemStackData.deleteLore(h);
return;
}
final ListTag list = SpongeAdventure.listTagJson(v);
h.getOrCreateTagElement(Constants.Item.ITEM_DISPLAY).put(Constants.Item.ITEM_LORE, list);
}).delete(ItemStackData::deleteLore).create(Keys.MAX_DURABILITY).get(h -> h.getItem().canBeDepleted() ? h.getItem().getMaxDamage() : null).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_DURABILITY).get(stack -> stack.getMaxDamage() - stack.getDamageValue()).set((stack, durability) -> stack.setDamageValue(stack.getMaxDamage() - durability)).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_RARITY).get(stack -> (ItemRarity) (Object) stack.getRarity()).create(Keys.REPLENISHED_FOOD).get(h -> {
if (h.getItem().isEdible()) {
final FoodProperties food = h.getItem().getFoodProperties();
return food == null ? null : food.getNutrition();
}
return null;
}).supports(h -> h.getItem().isEdible()).create(Keys.REPLENISHED_SATURATION).get(h -> {
if (h.getItem().isEdible()) {
final FoodProperties food = h.getItem().getFoodProperties();
if (food != null) {
// Translate's Minecraft's weird internal value to the actual saturation value
return food.getSaturationModifier() * food.getNutrition() * 2.0;
}
}
return null;
}).supports(h -> h.getItem().isEdible());
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class MapInfoItemStackData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.MAP_INFO).supports(item -> item.getItem() instanceof MapItem).get(itemStack -> {
if (itemStack.getTag() == null) {
return null;
}
return (MapInfo) ((Level) Sponge.server().worldManager().defaultWorld()).getMapData(Constants.Map.MAP_PREFIX + itemStack.getTag().getInt(Constants.Map.MAP_ID));
}).set((itemStack, mapInfo) -> {
@Nullable CompoundTag nbt = itemStack.getTag();
if (nbt == null) {
nbt = new CompoundTag();
}
nbt.putInt(Constants.Map.MAP_ID, ((MapItemSavedDataBridge) mapInfo).bridge$getMapId());
itemStack.setTag(nbt);
});
}
use of net.minecraft.world.item.ItemStack in project SpongeCommon by SpongePowered.
the class BlockTypeItemStackData method set.
private static boolean set(final ItemStack stack, final String nbtKey, final Set<? extends BlockType> value) {
if (value.isEmpty()) {
stack.removeTagKey(nbtKey);
return true;
}
final CompoundTag tag = stack.getOrCreateTag();
final ListTag list = value.stream().map(type -> Registry.BLOCK.getKey((Block) type).toString()).collect(NBTCollectors.toStringTagList());
tag.put(nbtKey, list);
return true;
}
Aggregations