Search in sources :

Example 6 with Tristate

use of org.spongepowered.api.util.Tristate in project CmdScheduler by SimonFlash.

the class Create method create.

public static CommandResult create(CommandSource src, CommandContext args, Function<CommandContext, Schedule> function) throws CommandException {
    String name = args.<String>getOne("name").get();
    if (Config.tasks.containsKey(name.toLowerCase())) {
        throw new CommandException(Text.of("A task already exists with name " + name + "."));
    }
    try {
        CommandTask task = new CommandTask(name, args.<String>getOne("command").get(), function.apply(args), args.<Tristate>getOne("async").orElse(Tristate.UNDEFINED));
        Config.tasks.put(task.getName().toLowerCase(), task);
        src.sendMessage(Text.of("Successfully created task " + task.getName() + ". For this task to persist, it must be added to the config manually."));
        if (args.hasAny("start")) {
            task.getTask().start(CmdScheduler.get().getContainer());
            src.sendMessage(Text.of("Task " + task.getName() + " has been started."));
        }
        return CommandResult.success();
    } catch (IllegalArgumentException e) {
        throw new CommandException(Text.of(e.getMessage()));
    }
}
Also used : CommandTask(com.mcsimonflash.sponge.cmdscheduler.task.CommandTask) Tristate(org.spongepowered.api.util.Tristate) CommandException(org.spongepowered.api.command.CommandException)

Example 7 with Tristate

use of org.spongepowered.api.util.Tristate in project SpongeCommon by SpongePowered.

the class SpongeNodeTree method get.

@Override
public Tristate get(final String node) {
    final String[] parts = SpongeNodeTree.NODE_SPLIT.split(node.toLowerCase(), -1);
    Node currentNode = this.rootNode;
    Tristate lastUndefinedVal = Tristate.UNDEFINED;
    for (String str : parts) {
        if (!currentNode.children.containsKey(str)) {
            break;
        }
        currentNode = currentNode.children.get(str);
        if (currentNode.value != Tristate.UNDEFINED) {
            lastUndefinedVal = currentNode.value;
        }
    }
    return lastUndefinedVal;
}
Also used : Tristate(org.spongepowered.api.util.Tristate)

Example 8 with Tristate

use of org.spongepowered.api.util.Tristate in project SpongeCommon by SpongePowered.

the class ServerPlayerGameModeMixin_Tracker method useItemOn.

/**
 * @author Morph
 * @reason Fire interact block event.
 */
@Overwrite
public InteractionResult useItemOn(final ServerPlayer playerIn, final Level worldIn, final ItemStack stackIn, final InteractionHand handIn, final BlockHitResult blockRaytraceResultIn) {
    final BlockPos blockpos = blockRaytraceResultIn.getBlockPos();
    final BlockState blockstate = worldIn.getBlockState(blockpos);
    // Sponge start
    final BlockSnapshot snapshot = ((ServerWorld) (worldIn)).createSnapshot(VecHelper.toVector3i(blockpos));
    final Vector3d hitVec = Vector3d.from(blockRaytraceResultIn.getBlockPos().getX(), blockRaytraceResultIn.getBlockPos().getY(), blockRaytraceResultIn.getBlockPos().getZ());
    final org.spongepowered.api.util.Direction direction = DirectionFacingProvider.INSTANCE.getKey(blockRaytraceResultIn.getDirection()).get();
    final InteractBlockEvent.Secondary event = SpongeCommonEventFactory.callInteractBlockEventSecondary(playerIn, stackIn, hitVec, snapshot, direction, handIn);
    if (event.isCancelled()) {
        return InteractionResult.FAIL;
    }
    final Tristate useItem = event.useItemResult();
    final Tristate useBlock = event.useBlockResult();
    // Sponge end
    if (this.gameModeForPlayer == GameType.SPECTATOR) {
        final MenuProvider inamedcontainerprovider = blockstate.getMenuProvider(worldIn, blockpos);
        if (inamedcontainerprovider != null) {
            playerIn.openMenu(inamedcontainerprovider);
            final Vector3i pos = VecHelper.toVector3i(blockRaytraceResultIn.getBlockPos());
            final ServerLocation location = ServerLocation.of((ServerWorld) worldIn, pos);
            try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
                frame.pushCause(playerIn);
                frame.addContext(EventContextKeys.BLOCK_HIT, ((ServerWorld) (worldIn)).createSnapshot(pos));
                ((ContainerBridge) playerIn.containerMenu).bridge$setOpenLocation(location);
                if (!InventoryEventFactory.callInteractContainerOpenEvent(playerIn)) {
                    return InteractionResult.SUCCESS;
                }
            }
            return InteractionResult.SUCCESS;
        } else {
            return InteractionResult.PASS;
        }
    } else {
        final boolean flag = !playerIn.getMainHandItem().isEmpty() || !playerIn.getOffhandItem().isEmpty();
        final boolean flag1 = playerIn.isSecondaryUseActive() && flag;
        final ItemStack copiedStack = stackIn.copy();
        if (useBlock != Tristate.FALSE && !flag1) {
            // Sponge check useBlock
            final AbstractContainerMenu lastOpenContainer = playerIn.containerMenu;
            final InteractionResult result = blockstate.use(worldIn, playerIn, handIn, blockRaytraceResultIn);
            if (result.consumesAction() && lastOpenContainer != playerIn.containerMenu) {
                final Vector3i pos = VecHelper.toVector3i(blockRaytraceResultIn.getBlockPos());
                final ServerLocation location = ServerLocation.of((ServerWorld) worldIn, pos);
                try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
                    frame.pushCause(playerIn);
                    frame.addContext(EventContextKeys.BLOCK_HIT, ((ServerWorld) (worldIn)).createSnapshot(pos));
                    ((ContainerBridge) playerIn.containerMenu).bridge$setOpenLocation(location);
                    if (!InventoryEventFactory.callInteractContainerOpenEvent(playerIn)) {
                        return InteractionResult.FAIL;
                    }
                }
            }
            if (result.consumesAction()) {
                CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(playerIn, blockpos, copiedStack);
                return result;
            }
        }
        if (!stackIn.isEmpty() && !playerIn.getCooldowns().isOnCooldown(stackIn.getItem())) {
            // Sponge start
            if (useItem == Tristate.FALSE) {
                return InteractionResult.PASS;
            }
            // Sponge end
            final UseOnContext itemusecontext = new UseOnContext(playerIn, handIn, blockRaytraceResultIn);
            final InteractionResult result;
            if (this.isCreative()) {
                final int i = stackIn.getCount();
                result = stackIn.useOn(itemusecontext);
                stackIn.setCount(i);
            } else {
                result = stackIn.useOn(itemusecontext);
                // Sponge start - log change in hand
                final PhaseContext<@NonNull ?> context = PhaseTracker.SERVER.getPhaseContext();
                final TransactionalCaptureSupplier transactor = context.getTransactor();
                transactor.logPlayerInventoryChange(this.player, PlayerInventoryTransaction.EventCreator.STANDARD);
                this.player.inventoryMenu.broadcastChanges();
            // Sponge end
            }
            if (result.consumesAction()) {
                CriteriaTriggers.ITEM_USED_ON_BLOCK.trigger(playerIn, blockpos, copiedStack);
            }
            return result;
        } else {
            return InteractionResult.PASS;
        }
    }
}
Also used : ServerLocation(org.spongepowered.api.world.server.ServerLocation) Tristate(org.spongepowered.api.util.Tristate) ContainerBridge(org.spongepowered.common.bridge.world.inventory.container.ContainerBridge) ServerWorld(org.spongepowered.api.world.server.ServerWorld) InteractionResult(net.minecraft.world.InteractionResult) InteractBlockEvent(org.spongepowered.api.event.block.InteractBlockEvent) TransactionalCaptureSupplier(org.spongepowered.common.event.tracking.context.transaction.TransactionalCaptureSupplier) AbstractContainerMenu(net.minecraft.world.inventory.AbstractContainerMenu) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockPos(net.minecraft.core.BlockPos) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) UseOnContext(net.minecraft.world.item.context.UseOnContext) MenuProvider(net.minecraft.world.MenuProvider) BlockState(net.minecraft.world.level.block.state.BlockState) Vector3d(org.spongepowered.math.vector.Vector3d) Vector3i(org.spongepowered.math.vector.Vector3i) ItemStack(net.minecraft.world.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 9 with Tristate

use of org.spongepowered.api.util.Tristate in project SpongeCommon by SpongePowered.

the class DedicatedPlayerListMixin method impl$checkForPlayerLimitBypassPermission.

@Inject(method = "canBypassPlayerLimit", at = @At("HEAD"), cancellable = true)
private void impl$checkForPlayerLimitBypassPermission(final GameProfile profile, final CallbackInfoReturnable<Boolean> ci) {
    final PermissionService permissionService = Sponge.server().serviceProvider().permissionService();
    Subject subject = permissionService.userSubjects().subject(profile.getId().toString()).orElse(null);
    if (subject == null) {
        subject = permissionService.userSubjects().defaults();
    }
    final Tristate tristate = subject.permissionValue(LoginPermissions.BYPASS_PLAYER_LIMIT_PERMISSION);
    // if we are still using the default permission service
    if (tristate == Tristate.UNDEFINED && permissionService instanceof SpongePermissionService) {
        return;
    }
    ci.setReturnValue(tristate.asBoolean());
}
Also used : SpongePermissionService(org.spongepowered.common.service.server.permission.SpongePermissionService) PermissionService(org.spongepowered.api.service.permission.PermissionService) Tristate(org.spongepowered.api.util.Tristate) SpongePermissionService(org.spongepowered.common.service.server.permission.SpongePermissionService) Subject(org.spongepowered.api.service.permission.Subject) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

Tristate (org.spongepowered.api.util.Tristate)9 Subject (org.spongepowered.api.service.permission.Subject)3 CommandTask (com.mcsimonflash.sponge.cmdscheduler.task.CommandTask)2 PermissionService (org.spongepowered.api.service.permission.PermissionService)2 Inject (org.spongepowered.asm.mixin.injection.Inject)2 HashMap (java.util.HashMap)1 BlockPos (net.minecraft.core.BlockPos)1 InteractionResult (net.minecraft.world.InteractionResult)1 MenuProvider (net.minecraft.world.MenuProvider)1 AbstractContainerMenu (net.minecraft.world.inventory.AbstractContainerMenu)1 ItemStack (net.minecraft.world.item.ItemStack)1 UseOnContext (net.minecraft.world.item.context.UseOnContext)1 BlockState (net.minecraft.world.level.block.state.BlockState)1 Test (org.junit.jupiter.api.Test)1 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)1 CommandException (org.spongepowered.api.command.CommandException)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 InteractBlockEvent (org.spongepowered.api.event.block.InteractBlockEvent)1 NodeTree (org.spongepowered.api.service.permission.NodeTree)1 ServerLocation (org.spongepowered.api.world.server.ServerLocation)1