use of org.spongepowered.api.data.Transaction in project modules-extra by CubeEngine.
the class ChangeInventoryReport method showReport.
@Override
public void showReport(List<Action> actions, Receiver receiver) {
Text cause = Recall.cause(actions.get(0));
LinkedList<Transaction<ItemStack>> transactions = new LinkedList<>();
for (Action action : actions) {
List<Map<String, Object>> changes = action.getData(INVENTORY_CHANGES);
for (Map<String, Object> change : changes) {
ItemStack originStack = Recall.item(((Map<String, Object>) change.get(ORIGINAL))).get().createStack();
ItemStack finalStack = Recall.item(((Map<String, Object>) change.get(REPLACEMENT))).get().createStack();
if (COMPARATOR.compare(originStack, finalStack) == 0) {
if (originStack.getQuantity() > finalStack.getQuantity()) {
ItemStack stack = originStack;
stack.setQuantity(originStack.getQuantity() - finalStack.getQuantity());
originStack = stack;
finalStack = ItemStack.empty();
} else {
ItemStack stack = finalStack;
stack.setQuantity(finalStack.getQuantity() - originStack.getQuantity());
finalStack = stack;
originStack = ItemStack.empty();
}
}
boolean added = false;
for (Transaction<ItemStack> trans : transactions) {
if (originStack.isEmpty()) {
if (COMPARATOR.compare(trans.getFinal(), finalStack) == 0 && trans.getOriginal().isEmpty()) {
trans.getFinal().setQuantity(trans.getFinal().getQuantity() + finalStack.getQuantity());
added = true;
break;
} else if (trans.getFinal().isEmpty() && COMPARATOR.compare(trans.getOriginal(), finalStack) == 0) {
trans.getOriginal().setQuantity(trans.getOriginal().getQuantity() - finalStack.getQuantity());
added = true;
break;
} else if (COMPARATOR.compare(trans.getOriginal(), finalStack) == 0) {
break;
}
}
if (finalStack.isEmpty()) {
if (COMPARATOR.compare(trans.getOriginal(), originStack) == 0) {
trans.getOriginal().setQuantity(trans.getOriginal().getQuantity() + originStack.getQuantity());
added = true;
break;
} else if (trans.getOriginal().isEmpty() && COMPARATOR.compare(trans.getFinal(), originStack) == 0) {
trans.getFinal().setQuantity(trans.getFinal().getQuantity() - originStack.getQuantity());
added = true;
break;
} else if (COMPARATOR.compare(trans.getFinal(), originStack) == 0) {
break;
}
}
}
if (!added) {
transactions.addFirst(new Transaction<>(originStack, finalStack));
}
}
}
Collections.reverse(transactions);
for (Transaction<ItemStack> trans : transactions) {
ItemStack stack1 = trans.getOriginal();
ItemStack stack2 = trans.getFinal();
if (stack1.isEmpty() && stack2.isEmpty()) {
continue;
}
if (stack1.getType() == ItemTypes.AIR) {
receiver.sendReport(this, actions, "{txt} inserted {txt}", cause, ReportUtil.name(stack2.createSnapshot()));
} else if (stack2.getType() == ItemTypes.AIR) {
receiver.sendReport(this, actions, "{txt} took {txt}", cause, ReportUtil.name(stack1.createSnapshot()));
} else {
receiver.sendReport(this, actions, "{txt} swapped {txt} with {txt}", cause, ReportUtil.name(stack1.createSnapshot()), ReportUtil.name(stack2.createSnapshot()));
}
}
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class LivingEntityMixin method impl$onSetHeldItem.
@Redirect(method = "completeUsingItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;setItemInHand(Lnet/minecraft/world/InteractionHand;Lnet/minecraft/world/item/ItemStack;)V"))
private void impl$onSetHeldItem(final LivingEntity self, final InteractionHand hand, final ItemStack stack) {
if (this.level.isClientSide) {
self.setItemInHand(hand, stack);
return;
}
// Unforunately, ItemFood calls ItemStack#shrink in Item#onItemUseFinish.
// To ensure that we provide the original ItemStack in the event,
// we make a copy of in our onUpdateItemUse redirect
// If the event or transaction is cancelled, we make sure to explicitly
// set the copy back in the player's hand, since it may have been already
// modified if an ItemFood is being used.
final ItemStackSnapshot activeItemStackSnapshot = ItemStackUtil.snapshotOf(this.impl$activeItemStackCopy == null ? ItemStack.EMPTY : this.impl$activeItemStackCopy);
final UseItemStackEvent.Replace event;
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(stack == null ? ItemStack.EMPTY : stack);
final HandType handType = (HandType) (Object) hand;
this.impl$addSelfToFrame(frame, activeItemStackSnapshot, handType);
final Ticks useItemRemainingTicks = Ticks.of(this.useItemRemaining);
event = SpongeEventFactory.createUseItemStackEventReplace(PhaseTracker.getCauseStackManager().currentCause(), useItemRemainingTicks, useItemRemainingTicks, activeItemStackSnapshot, new Transaction<>(ItemStackUtil.snapshotOf(this.impl$activeItemStackCopy), snapshot));
}
if (SpongeCommon.post(event)) {
this.shadow$setItemInHand(hand, this.impl$activeItemStackCopy.copy());
return;
}
if (!event.itemStackResult().isValid()) {
this.shadow$setItemInHand(hand, this.impl$activeItemStackCopy.copy());
return;
}
this.shadow$setItemInHand(hand, ItemStackUtil.fromSnapshotToNative(event.itemStackResult().finalReplacement()));
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class ServerLevelMixin method impl$onSetWeatherParameters.
@Inject(method = "tick", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "FIELD", target = "Lnet/minecraft/server/level/ServerLevel;oRainLevel:F", shift = At.Shift.BEFORE, ordinal = 1))
public void impl$onSetWeatherParameters(final BooleanSupplier param0, final CallbackInfo ci, final ProfilerFiller var0, final boolean wasRaining) {
final boolean isRaining = this.shadow$isRaining();
if (this.oRainLevel != this.rainLevel || this.oThunderLevel != this.thunderLevel || wasRaining != isRaining) {
Weather newWeather = ((ServerWorld) this).properties().weather();
final Cause currentCause = Sponge.server().causeStackManager().currentCause();
final Transaction<Weather> weatherTransaction = new Transaction<>(this.impl$prevWeather, newWeather);
final ChangeWeatherEvent event = SpongeEventFactory.createChangeWeatherEvent(currentCause, ((ServerWorld) this), weatherTransaction);
if (Sponge.eventManager().post(event)) {
newWeather = event.weather().original();
} else {
newWeather = event.weather().finalReplacement();
}
// Set event results
this.impl$prevWeather = newWeather;
if (newWeather.type() == WeatherTypes.CLEAR.get()) {
this.serverLevelData.setThunderTime(0);
this.serverLevelData.setRainTime(0);
this.serverLevelData.setClearWeatherTime((int) newWeather.remainingDuration().ticks());
this.serverLevelData.setThundering(false);
this.serverLevelData.setRaining(false);
} else {
final int newTime = (int) newWeather.remainingDuration().ticks();
this.serverLevelData.setRaining(true);
this.serverLevelData.setClearWeatherTime(0);
this.serverLevelData.setRainTime(newTime);
if (newWeather.type() == WeatherTypes.THUNDER.get()) {
this.serverLevelData.setThunderTime(newTime);
this.serverLevelData.setThundering(true);
} else {
this.serverLevelData.setThunderTime(0);
this.serverLevelData.setThundering(false);
}
}
}
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class InventoryEventFactory method callChangeEntityEquipmentEvent.
public static ChangeEntityEquipmentEvent callChangeEntityEquipmentEvent(final LivingEntity entity, final ItemStackSnapshot before, final ItemStackSnapshot after, final Slot slot) {
final ChangeEntityEquipmentEvent event;
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.pushCause(entity);
final Transaction<ItemStackSnapshot> transaction = new Transaction<>(before, after);
if (after.isEmpty()) {
event = SpongeEventFactory.createChangeEntityEquipmentEventBreak(frame.currentCause(), (Entity) entity, slot, transaction);
} else {
event = SpongeEventFactory.createChangeEntityEquipmentEvent(frame.currentCause(), (Entity) entity, slot, transaction);
}
SpongeCommon.post(event);
return event;
}
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class InventoryEventFactory method callEnchantEventLevelRequirement.
public static int callEnchantEventLevelRequirement(EnchantmentMenu container, int seed, int option, int power, ItemStack itemStack, int levelRequirement) {
org.spongepowered.api.item.inventory.Container enchantContainer = ContainerUtil.fromNative(container);
Player viewer = (Player) enchantContainer.viewer();
ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(viewer.inventory.getCarried());
Transaction<ItemStackSnapshot> cursorTrans = new Transaction<>(cursor, cursor);
EnchantItemEvent.CalculateLevelRequirement event = SpongeEventFactory.createEnchantItemEventCalculateLevelRequirement(PhaseTracker.getCauseStackManager().currentCause(), levelRequirement, levelRequirement, enchantContainer, cursorTrans, ItemStackUtil.snapshotOf(itemStack), option, power, seed);
SpongeCommon.post(event);
return event.levelRequirement();
}
Aggregations