use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callInteractInventoryOpenEvent.
public static boolean callInteractInventoryOpenEvent(EntityPlayerMP player) {
ItemStackSnapshot newCursor = player.inventory.getItemStack().isEmpty() ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) player.inventory.getItemStack()).createSnapshot();
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, newCursor);
InteractInventoryEvent.Open event = SpongeEventFactory.createInteractInventoryEventOpen(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer);
SpongeImpl.postEvent(event);
if (event.isCancelled()) {
player.closeScreen();
return false;
}
// Custom cursor
if (event.getCursorTransaction().getCustom().isPresent()) {
handleCustomCursor(player, event.getCursorTransaction().getFinal());
}
return true;
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callCraftEventPost.
public static CraftItemEvent.Craft callCraftEventPost(EntityPlayer player, CraftingInventory inventory, ItemStackSnapshot result, @Nullable CraftingRecipe recipe, Container container, List<SlotTransaction> transactions) {
// Get previous cursor if captured
ItemStack previousCursor = ((IMixinContainer) container).getPreviousCursor();
if (previousCursor == null) {
// or get the current one
previousCursor = player.inventory.getItemStack();
}
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackUtil.snapshotOf(previousCursor), ItemStackUtil.snapshotOf(player.inventory.getItemStack()));
CraftItemEvent.Craft event = SpongeEventFactory.createCraftItemEventCraft(Sponge.getCauseStackManager().getCurrentCause(), result, inventory, cursorTransaction, Optional.ofNullable(recipe), ((org.spongepowered.api.item.inventory.Container) container), transactions);
SpongeImpl.postEvent(event);
((IMixinContainer) container).setCaptureInventory(false);
// handle slot-transactions
PacketPhaseUtil.handleSlotRestore(player, container, new ArrayList<>(transactions), event.isCancelled());
if (event.isCancelled() || !event.getCursorTransaction().isValid() || event.getCursorTransaction().getCustom().isPresent()) {
// handle cursor-transaction
ItemStackSnapshot newCursor = event.isCancelled() || event.getCursorTransaction().isValid() ? event.getCursorTransaction().getOriginal() : event.getCursorTransaction().getFinal();
player.inventory.setItemStack(ItemStackUtil.fromSnapshotToNative(newCursor));
if (player instanceof EntityPlayerMP) {
((EntityPlayerMP) player).connection.sendPacket(new SPacketSetSlot(-1, -1, player.inventory.getItemStack()));
}
}
transactions.clear();
((IMixinContainer) container).setCaptureInventory(true);
return event;
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callCreativeClickInventoryEvent.
public static ClickInventoryEvent.Creative callCreativeClickInventoryEvent(EntityPlayerMP player, CPacketCreativeInventoryAction packetIn) {
Sponge.getCauseStackManager().pushCause(player);
// Creative doesn't inform server of cursor status so there is no way of knowing what the final stack is
// Due to this, we can only send the original item that was clicked in slot
Transaction<ItemStackSnapshot> cursorTransaction = new Transaction<>(ItemStackSnapshot.NONE, ItemStackSnapshot.NONE);
if (((IMixinContainer) player.openContainer).getCapturedTransactions().size() == 0 && packetIn.getSlotId() >= 0 && packetIn.getSlotId() < player.openContainer.inventorySlots.size()) {
Slot slot = player.openContainer.getSlot(packetIn.getSlotId());
if (slot != null) {
ItemStackSnapshot clickedItem = slot.getStack() == null ? ItemStackSnapshot.NONE : ((org.spongepowered.api.item.inventory.ItemStack) slot.getStack()).createSnapshot();
SlotTransaction slotTransaction = new SlotTransaction(((org.spongepowered.api.item.inventory.Slot) slot), clickedItem, ItemStackSnapshot.NONE);
((IMixinContainer) player.openContainer).getCapturedTransactions().add(slotTransaction);
}
}
ClickInventoryEvent.Creative event = SpongeEventFactory.createClickInventoryEventCreative(Sponge.getCauseStackManager().getCurrentCause(), cursorTransaction, (org.spongepowered.api.item.inventory.Container) player.openContainer, ((IMixinContainer) player.openContainer).getCapturedTransactions());
SpongeImpl.postEvent(event);
Sponge.getCauseStackManager().popCause();
return event;
}
use of org.spongepowered.api.data.Transaction in project SpongeCommon by SpongePowered.
the class SpongeCommonEventFactory method callChangeBlockEventModifyLiquidBreak.
public static ChangeBlockEvent.Break callChangeBlockEventModifyLiquidBreak(net.minecraft.world.World worldIn, BlockPos pos, IBlockState state, int flags) {
final PhaseTracker phaseTracker = PhaseTracker.getInstance();
final PhaseData data = phaseTracker.getCurrentPhaseData();
BlockState fromState = BlockUtil.fromNative(worldIn.getBlockState(pos));
BlockState toState = BlockUtil.fromNative(state);
User owner = data.context.getOwner().orElse(null);
User notifier = data.context.getNotifier().orElse(null);
Object source = data.context.getSource(LocatableBlock.class).orElse(null);
if (source == null) {
// Fallback
source = worldIn;
}
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(source);
Sponge.getCauseStackManager().addContext(EventContextKeys.LIQUID_BREAK, (World) worldIn);
if (owner != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner);
}
if (notifier != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier);
}
WorldProperties world = ((World) worldIn).getProperties();
Vector3i position = new Vector3i(pos.getX(), pos.getY(), pos.getZ());
Transaction<BlockSnapshot> transaction = new Transaction<>(BlockSnapshot.builder().blockState(fromState).world(world).position(position).build(), BlockSnapshot.builder().blockState(toState).world(world).position(position).build());
ChangeBlockEvent.Break event = SpongeEventFactory.createChangeBlockEventBreak(Sponge.getCauseStackManager().getCurrentCause(), Collections.singletonList(transaction));
SpongeImpl.postEvent(event);
return event;
}
}
use of org.spongepowered.api.data.Transaction in project Skree by Skelril.
the class WildernessWorldWrapper method onBlockBreak.
@Listener
public void onBlockBreak(ChangeBlockEvent.Break event, @Named(NamedCause.SOURCE) Entity srcEnt) {
List<Transaction<BlockSnapshot>> transactions = event.getTransactions();
for (Transaction<BlockSnapshot> block : transactions) {
BlockSnapshot original = block.getOriginal();
Optional<Location<World>> optLoc = original.getLocation();
if (!optLoc.isPresent()) {
continue;
}
Optional<Integer> optLevel = getLevel(optLoc.get());
if (!optLevel.isPresent()) {
continue;
}
int level = optLevel.get();
Location<World> loc = optLoc.get();
BlockState state = original.getState();
BlockType type = state.getType();
// Prevent item dupe glitch by removing the position before subsequent breaks
markedOrePoints.remove(loc);
if (config.getDropAmplificationConfig().amplifies(state) && !original.getCreator().isPresent()) {
markedOrePoints.add(loc);
if (srcEnt instanceof Player) {
Optional<HighScoreService> optHighScores = Sponge.getServiceManager().provide(HighScoreService.class);
optHighScores.ifPresent(highScoreService -> highScoreService.update((Player) srcEnt, ScoreTypes.WILDERNESS_ORES_MINED, 1));
}
}
if (srcEnt instanceof Player && type.equals(BlockTypes.STONE) && Probability.getChance(Math.max(12, 250 - level))) {
Vector3d max = loc.getPosition().add(1, 1, 1);
Vector3d min = loc.getPosition().sub(1, 1, 1);
Extent world = loc.getExtent();
if (Probability.getChance(3)) {
Entity entity = world.createEntity(EntityTypes.SILVERFISH, loc.getPosition().add(.5, 0, .5));
world.spawnEntity(entity, Cause.source(SpawnCause.builder().type(SpawnTypes.BLOCK_SPAWNING).build()).build());
}
// Do this one tick later to guarantee no collision with transaction data
Task.builder().delayTicks(1).execute(() -> {
for (int x = min.getFloorX(); x <= max.getFloorX(); ++x) {
for (int z = min.getFloorZ(); z <= max.getFloorZ(); ++z) {
for (int y = min.getFloorY(); y <= max.getFloorY(); ++y) {
if (!world.containsBlock(x, y, z)) {
continue;
}
if (world.getBlockType(x, y, z) == BlockTypes.STONE) {
world.setBlockType(x, y, z, BlockTypes.MONSTER_EGG, BlockChangeFlag.NONE, Cause.source(SkreePlugin.container()).build());
}
}
}
}
}).submit(SkreePlugin.inst());
}
}
}
Aggregations