use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class BrewingStandBlockEntityMixin method impl$onTick.
@Inject(method = "tick", cancellable = true, locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "FIELD", target = "Lnet/minecraft/world/level/block/entity/BrewingStandBlockEntity;brewTime:I", ordinal = 1))
private void impl$onTick(CallbackInfo ci, ItemStack fuelStack, boolean isBrewable, boolean isBrewing, ItemStack ingredientStack) {
if (this.brewTime != 0 && isBrewable && this.ingredient == ingredientStack.getItem()) {
final Cause currentCause = Sponge.server().causeStackManager().currentCause();
final BrewingEvent.Tick event = SpongeEventFactory.createBrewingEventTick(currentCause, (BrewingStand) this, ItemStackUtil.snapshotOf(ingredientStack));
if (Sponge.eventManager().post(event)) {
this.brewTime++;
}
}
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class BlockTransactionType method consumeEventsAndMarker.
@Override
protected void consumeEventsAndMarker(PhaseContext<@NonNull ?> context, final Collection<? extends ChangeBlockEvent.All> changeBlockEvents) {
final Multimap<ResourceKey, ChangeBlockEvent.All> eventsByWorld = LinkedListMultimap.create();
changeBlockEvents.forEach(event -> eventsByWorld.put(event.world().key(), event));
eventsByWorld.asMap().forEach((key, events) -> {
final Optional<ServerWorld> serverWorld = ((SpongeServer) SpongeCommon.server()).worldManager().world(key);
if (!serverWorld.isPresent()) {
return;
}
final ListMultimap<BlockPos, SpongeBlockSnapshot> positions = LinkedListMultimap.create();
// Gather transactions that were valid
events.stream().filter(event -> !event.isCancelled()).flatMap(event -> event.transactions().stream()).filter(BlockTransaction::isValid).forEach(transactions -> {
// Then "put" the most recent transactions such that we have a complete rebuild of
// each position according to what originally existed and then
// the ultimate final block on that position
final SpongeBlockSnapshot original = (SpongeBlockSnapshot) transactions.original();
positions.put(original.getBlockPos(), original);
positions.put(original.getBlockPos(), (SpongeBlockSnapshot) transactions.finalReplacement());
});
// just return.
if (positions.isEmpty()) {
return;
}
final ImmutableList<BlockTransactionReceipt> transactions = positions.asMap().values().stream().map(spongeBlockSnapshots -> {
final List<SpongeBlockSnapshot> snapshots = new ArrayList<>(spongeBlockSnapshots);
if (snapshots.isEmpty() || snapshots.size() < 2) {
// Error case
return Optional.<BlockTransactionReceipt>empty();
}
final SpongeBlockSnapshot original = snapshots.get(0);
final SpongeBlockSnapshot result = snapshots.get(snapshots.size() - 1);
final Operation operation = context.getBlockOperation(original, result);
final BlockTransactionReceipt eventTransaction = new BlockTransactionReceipt(original, result, operation);
context.postBlockTransactionApplication(original.blockChange, eventTransaction);
return Optional.of(eventTransaction);
}).filter(Optional::isPresent).map(Optional::get).collect(ImmutableList.toImmutableList());
final Cause cause = PhaseTracker.getInstance().currentCause();
SpongeCommon.post(SpongeEventFactory.createChangeBlockEventPost(cause, transactions, serverWorld.get()));
});
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class InventoryBasedTransaction method generateEvent.
@Override
public Optional<ChangeInventoryEvent> generateEvent(final PhaseContext<@NonNull ?> context, @Nullable final GameTransaction<@NonNull ?> parent, final ImmutableList<GameTransaction<ChangeInventoryEvent>> gameTransactions, final Cause currentCause) {
final ImmutableList<InventoryBasedTransaction> containerBasedTransactions = gameTransactions.stream().filter(tx -> tx instanceof InventoryBasedTransaction).map(tx -> (InventoryBasedTransaction) tx).filter(tx -> !tx.used).collect(ImmutableList.toImmutableList());
final List<SlotTransaction> slotTransactions = containerBasedTransactions.stream().map(InventoryBasedTransaction::getSlotTransactions).flatMap(List::stream).collect(Collectors.toList());
for (InventoryBasedTransaction transaction : containerBasedTransactions) {
transaction.used = true;
}
final List<Entity> entities = containerBasedTransactions.stream().map(InventoryBasedTransaction::getEntitiesSpawned).flatMap(List::stream).collect(Collectors.toList());
// TODO on pickup grouping does not work?
final Map<Slot, List<SlotTransaction>> collected = slotTransactions.stream().collect(Collectors.groupingBy(SlotTransaction::slot));
slotTransactions.clear();
collected.values().forEach(list -> {
final SlotTransaction first = list.get(0);
if (list.size() > 1) {
final ItemStackSnapshot last = list.get(list.size() - 1).defaultReplacement();
slotTransactions.add(new SlotTransaction(first.slot(), first.original(), last));
} else {
slotTransactions.add(first);
}
});
return containerBasedTransactions.stream().map(t -> t.createInventoryEvent(slotTransactions, entities, context, currentCause)).filter(Optional::isPresent).map(Optional::get).findFirst();
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class DropFromPlayerInventoryTransaction method createInventoryEvent.
@Override
Optional<ChangeInventoryEvent> createInventoryEvent(final List<SlotTransaction> slotTransactions, final List<Entity> entities, final PhaseContext<@NonNull ?> context, final Cause currentCause) {
TrackingUtil.setCreatorReference(entities, this.player);
final Cause causeWithSpawnType = Cause.builder().from(currentCause).build(EventContext.builder().from(currentCause.context()).add(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM.get()).build());
if (this.dropAll) {
return Optional.of(SpongeEventFactory.createChangeInventoryEventDropFull(causeWithSpawnType, entities, this.inventory, this.slot, slotTransactions));
}
return Optional.of(SpongeEventFactory.createChangeInventoryEventDropSingle(causeWithSpawnType, entities, this.inventory, this.slot, slotTransactions));
}
use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.
the class DamageEventUtil method createAttackEnchantmentFunction.
public static List<DamageFunction> createAttackEnchantmentFunction(final net.minecraft.world.item.ItemStack heldItem, final MobType creatureAttribute, final float attackStrength) {
if (heldItem.isEmpty()) {
return Collections.emptyList();
}
final ListTag enchantmentCompounds = heldItem.getEnchantmentTags();
if (enchantmentCompounds.isEmpty()) {
return Collections.emptyList();
}
final Map<Enchantment, Collection<Integer>> enchantments = new HashMap<>();
for (int i = 0; i < enchantmentCompounds.size(); ++i) {
final String id = enchantmentCompounds.getCompound(i).getString("id");
final int enchantmentLevel = enchantmentCompounds.getCompound(i).getInt("lvl");
final Enchantment enchantment = Registry.ENCHANTMENT.get(new ResourceLocation(id));
if (enchantment != null) {
enchantments.computeIfAbsent(enchantment, k -> new ArrayList<>()).add(enchantmentLevel);
}
}
if (enchantments.isEmpty()) {
return Collections.emptyList();
}
final List<DamageFunction> damageModifierFunctions = new ArrayList<>();
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(heldItem);
for (final Map.Entry<Enchantment, Collection<Integer>> enchantment : enchantments.entrySet()) {
final DamageModifier enchantmentModifier = DamageModifier.builder().type(DamageModifierTypes.WEAPON_ENCHANTMENT).cause(Cause.of(EventContext.empty(), snapshot, enchantment)).build();
final DoubleUnaryOperator enchantmentFunction = (damage) -> {
double totalDamage = 0;
for (final int level : enchantment.getValue()) {
totalDamage += (double) enchantment.getKey().getDamageBonus(level, creatureAttribute) * attackStrength;
}
return totalDamage;
};
damageModifierFunctions.add(new DamageFunction(enchantmentModifier, enchantmentFunction));
}
return damageModifierFunctions;
}
Aggregations