Search in sources :

Example 16 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class BrewingStandBlockEntityMixin method impl$onConsumeFuel.

// @Formatter:on
@Inject(method = "tick", locals = LocalCapture.CAPTURE_FAILEXCEPTION, slice = @Slice(to = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/entity/BrewingStandBlockEntity;isBrewable()Z")), at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/entity/BrewingStandBlockEntity;setChanged()V"))
private void impl$onConsumeFuel(CallbackInfo ci, ItemStack fuelStack) {
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    fuelStack.grow(1);
    final ItemStackSnapshot originalStack = ItemStackUtil.snapshotOf(fuelStack);
    fuelStack.shrink(1);
    final Transaction<ItemStackSnapshot> fuelTransaction = new Transaction<>(originalStack, ItemStackUtil.snapshotOf(fuelStack));
    final ItemStackSnapshot ingredientStack = ItemStackUtil.snapshotOf(this.items.get(3));
    final BrewingEvent.ConsumeFuel event = SpongeEventFactory.createBrewingEventConsumeFuel(currentCause, (BrewingStand) this, fuelTransaction, ingredientStack);
    if (Sponge.eventManager().post(event)) {
        fuelStack.grow(1);
        this.fuel = 0;
    } else if (event.fuel().custom().isPresent()) {
        final ItemStackSnapshot finalFuel = event.fuel().finalReplacement();
        this.items.set(4, ItemStackUtil.fromSnapshotToNative(finalFuel));
    }
}
Also used : Transaction(org.spongepowered.api.data.Transaction) BrewingEvent(org.spongepowered.api.event.block.entity.BrewingEvent) Cause(org.spongepowered.api.event.Cause) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) Inject(org.spongepowered.asm.mixin.injection.Inject)

Example 17 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class AbstractFurnaceBlockEntityMixin method impl$callInteruptSmeltEvent.

private void impl$callInteruptSmeltEvent() {
    if (this.cookingProgress > 0) {
        final ItemStackSnapshot fuel = ItemStackUtil.snapshotOf(this.items.get(1));
        final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
        final AbstractCookingRecipe recipe = this.impl$getCurrentRecipe();
        final CookingEvent.Interrupt event = SpongeEventFactory.createCookingEventInterrupt(cause, (FurnaceBlockEntity) this, Optional.of(fuel), Optional.ofNullable((CookingRecipe) recipe));
        SpongeCommon.post(event);
    }
}
Also used : CookingRecipe(org.spongepowered.api.item.recipe.cooking.CookingRecipe) AbstractCookingRecipe(net.minecraft.world.item.crafting.AbstractCookingRecipe) Cause(org.spongepowered.api.event.Cause) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) AbstractCookingRecipe(net.minecraft.world.item.crafting.AbstractCookingRecipe) CookingEvent(org.spongepowered.api.event.block.entity.CookingEvent)

Example 18 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class AbstractFurnaceBlockEntityMixin method impl$throwFuelEventIfOrShrink.

// @Formatter:on
// Shrink Fuel
@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;shrink(I)V"))
private void impl$throwFuelEventIfOrShrink(final ItemStack itemStack, final int quantity) {
    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
    final ItemStackSnapshot fuel = ItemStackUtil.snapshotOf(itemStack);
    final ItemStackSnapshot shrinkedFuel = ItemStackUtil.snapshotOf(ItemStackUtil.cloneDefensive(itemStack, itemStack.getCount() - 1));
    final Transaction<ItemStackSnapshot> transaction = new Transaction<>(fuel, shrinkedFuel);
    final AbstractCookingRecipe recipe = this.impl$getCurrentRecipe();
    final CookingEvent.ConsumeFuel event = SpongeEventFactory.createCookingEventConsumeFuel(cause, (FurnaceBlockEntity) this, Optional.of(fuel), Optional.of((CookingRecipe) recipe), Collections.singletonList(transaction));
    SpongeCommon.post(event);
    if (event.isCancelled()) {
        this.cookingTotalTime = 0;
        return;
    }
    if (!transaction.isValid()) {
        return;
    }
    if (transaction.custom().isPresent()) {
        this.items.set(1, ItemStackUtil.fromSnapshotToNative(transaction.finalReplacement()));
    } else {
        // vanilla
        itemStack.shrink(quantity);
    }
}
Also used : Transaction(org.spongepowered.api.data.Transaction) CookingRecipe(org.spongepowered.api.item.recipe.cooking.CookingRecipe) AbstractCookingRecipe(net.minecraft.world.item.crafting.AbstractCookingRecipe) Cause(org.spongepowered.api.event.Cause) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) AbstractCookingRecipe(net.minecraft.world.item.crafting.AbstractCookingRecipe) CookingEvent(org.spongepowered.api.event.block.entity.CookingEvent) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 19 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class SpongeCommandManager method init.

public void init() {
    final Cause cause = PhaseTracker.getCauseStackManager().currentCause();
    final Set<TypeToken<?>> usedTokens = new HashSet<>();
    Sponge.game().registry(RegistryTypes.COMMAND_REGISTRAR_TYPE).streamEntries().forEach(entry -> {
        final CommandRegistrarType<?> type = entry.value();
        // someone's gonna do it, let's not let them take us down.
        final TypeToken<?> handledType = type.handledType();
        if (handledType == null) {
            SpongeCommon.logger().error("Registrar '{}' did not provide a handledType, skipping...", type.getClass());
        } else if (usedTokens.add(handledType)) {
            // we haven't done it yet
            // Add the command registrar
            final CommandRegistrar<?> registrar = type.create(this);
            this.knownRegistrars.put(GenericTypeReflector.erase(type.handledType().getType()), registrar);
            if (registrar instanceof BrigadierCommandRegistrar) {
                this.brigadierRegistrar = (BrigadierCommandRegistrar) registrar;
            } else if (registrar instanceof SpongeParameterizedCommandRegistrar) {
                this.registerInternalCommands((SpongeParameterizedCommandRegistrar) registrar);
            }
            this.game.eventManager().post(this.createEvent(cause, this.game, registrar));
        } else {
            SpongeCommon.logger().warn("Command type '{}' has already been collected, skipping request from {}", handledType.toString(), type.getClass());
        }
    });
    if (this.brigadierRegistrar == null) {
        throw new IllegalStateException("Brigadier registrar was not detected");
    }
}
Also used : BrigadierCommandRegistrar(org.spongepowered.common.command.registrar.BrigadierCommandRegistrar) SpongeParameterizedCommandRegistrar(org.spongepowered.common.command.registrar.SpongeParameterizedCommandRegistrar) TypeToken(io.leangen.geantyref.TypeToken) Cause(org.spongepowered.api.event.Cause) CommandCause(org.spongepowered.api.command.CommandCause) SpongeParameterizedCommandRegistrar(org.spongepowered.common.command.registrar.SpongeParameterizedCommandRegistrar) CommandRegistrar(org.spongepowered.api.command.registrar.CommandRegistrar) BrigadierCommandRegistrar(org.spongepowered.common.command.registrar.BrigadierCommandRegistrar) HashSet(java.util.HashSet)

Example 20 with Cause

use of org.spongepowered.api.event.Cause in project SpongeCommon by SpongePowered.

the class QueryThreadGs4Mixin method impl$basicSendTo.

@Inject(method = "buildRuleResponse", cancellable = true, at = @At(value = "INVOKE", target = "Lnet/minecraft/server/rcon/NetworkDataOutputStream;reset()V"))
public void impl$basicSendTo(DatagramPacket datagramPacket, CallbackInfoReturnable<byte[]> cir) throws IOException {
    final Cause currentCause = Sponge.server().causeStackManager().currentCause();
    final QueryServerEvent.Full event = SpongeEventFactory.createQueryServerEventFull(currentCause, (InetSocketAddress) datagramPacket.getSocketAddress(), new LinkedHashMap<>(), "MINECRAFT", "SMP", this.worldName, this.serverName, Arrays.asList(this.serverInterface.getPlayerNames()), this.serverInterface.getPluginNames(), this.serverInterface.getServerVersion(), this.maxPlayers, this.serverInterface.getPlayerCount());
    Sponge.eventManager().post(event);
    this.rulesResponse.reset();
    this.rulesResponse.write(0);
    this.rulesResponse.writeBytes(this.shadow$getIdentBytes(event.address()));
    this.rulesResponse.writeString("splitnum");
    this.rulesResponse.write(128);
    this.rulesResponse.write(0);
    this.rulesResponse.writeString("hostname");
    this.rulesResponse.writeString(event.motd());
    this.rulesResponse.writeString("gametype");
    this.rulesResponse.writeString(event.gameType());
    this.rulesResponse.writeString("game_id");
    this.rulesResponse.writeString(event.gameId());
    this.rulesResponse.writeString("version");
    this.rulesResponse.writeString(event.version());
    this.rulesResponse.writeString("plugins");
    this.rulesResponse.writeString(event.plugins());
    this.rulesResponse.writeString("map");
    this.rulesResponse.writeString(event.map());
    this.rulesResponse.writeString("numplayers");
    this.rulesResponse.writeString("" + event.playerCount());
    this.rulesResponse.writeString("maxplayers");
    this.rulesResponse.writeString("" + event.maxPlayerCount());
    this.rulesResponse.writeString("hostport");
    this.rulesResponse.writeString("" + this.serverPort);
    this.rulesResponse.writeString("hostip");
    this.rulesResponse.writeString(this.hostIp);
    this.rulesResponse.write(0);
    this.rulesResponse.write(1);
    this.rulesResponse.writeString("player_");
    this.rulesResponse.write(0);
    final List<String> var3 = event.players();
    for (String var4 : var3) {
        this.rulesResponse.writeString(var4);
    }
    this.rulesResponse.write(0);
    cir.setReturnValue(this.rulesResponse.toByteArray());
}
Also used : Cause(org.spongepowered.api.event.Cause) QueryServerEvent(org.spongepowered.api.event.server.query.QueryServerEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

Cause (org.spongepowered.api.event.Cause)55 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)19 ServerWorld (org.spongepowered.api.world.server.ServerWorld)18 Inject (org.spongepowered.asm.mixin.injection.Inject)18 ArrayList (java.util.ArrayList)16 BlockPos (net.minecraft.core.BlockPos)15 CauseStackManager (org.spongepowered.api.event.CauseStackManager)15 List (java.util.List)13 Optional (java.util.Optional)12 ServerLocation (org.spongepowered.api.world.server.ServerLocation)12 Entity (net.minecraft.world.entity.Entity)11 Player (net.minecraft.world.entity.player.Player)11 Collections (java.util.Collections)10 Collection (java.util.Collection)9 Map (java.util.Map)9 LivingEntity (net.minecraft.world.entity.LivingEntity)9 EventContext (org.spongepowered.api.event.EventContext)9 HashMap (java.util.HashMap)8 BlockState (net.minecraft.world.level.block.state.BlockState)8 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)8