Search in sources :

Example 1 with PipeEventHandler

use of buildcraft.api.transport.pipe.PipeEventHandler in project BuildCraft by BuildCraft.

the class PipeEventBus method getHandlers.

private static List<Handler> getHandlers(Class<?> cls) {
    if (!allHandlers.containsKey(cls)) {
        List<Handler> list = new ArrayList<>();
        Class<?> superCls = cls.getSuperclass();
        if (superCls != null) {
            list.addAll(getHandlers(superCls));
        }
        for (Method m : cls.getDeclaredMethods()) {
            PipeEventHandler annot = m.getAnnotation(PipeEventHandler.class);
            if (annot == null) {
                continue;
            }
            Parameter[] params = m.getParameters();
            if (params.length != 1) {
                throw new IllegalStateException("Cannot annotate " + m + " with @PipeEventHandler as it had an incorrect number of parameters (" + Arrays.toString(params) + ")");
            }
            Parameter p = params[0];
            if (!PipeEvent.class.isAssignableFrom(p.getType())) {
                throw new IllegalStateException("Cannot annotate " + m + " with @PipeEventHandler as it did not take a pipe event! (" + p.getType() + ")");
            }
            MethodHandle mh;
            try {
                mh = MethodHandles.publicLookup().unreflect(m);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Cannot annotate " + m + " with @PipeEventHandler as there was a problem with it!", e);
            }
            boolean isStatic = Modifier.isStatic(m.getModifiers());
            String methodName = m.toString();
            list.add(new Handler(annot.priority(), annot.receiveCancelled(), isStatic, methodName, mh, p.getType()));
        }
        allHandlers.put(cls, list);
        return list;
    }
    return allHandlers.get(cls);
}
Also used : ArrayList(java.util.ArrayList) PipeEventHandler(buildcraft.api.transport.pipe.PipeEventHandler) Method(java.lang.reflect.Method) PipeEvent(buildcraft.api.transport.pipe.PipeEvent) PipeEventHandler(buildcraft.api.transport.pipe.PipeEventHandler) Parameter(java.lang.reflect.Parameter) MethodHandle(java.lang.invoke.MethodHandle)

Example 2 with PipeEventHandler

use of buildcraft.api.transport.pipe.PipeEventHandler in project BuildCraft by BuildCraft.

the class PipeBehaviourDiamondFluid method sideCheck.

@PipeEventHandler
public void sideCheck(PipeEventFluid.SideCheck sideCheck) {
    FluidStack toCompare = sideCheck.fluid;
    for (EnumFacing face : EnumFacing.VALUES) {
        if (sideCheck.isAllowed(face) && pipe.isConnected(face)) {
            int offset = FILTERS_PER_SIDE * face.ordinal();
            boolean sideAllowed = false;
            boolean foundItem = false;
            for (int i = 0; i < FILTERS_PER_SIDE; i++) {
                ItemStack compareTo = filters.getStackInSlot(offset + i);
                if (compareTo.isEmpty())
                    continue;
                FluidStack target = FluidUtil.getFluidContained(compareTo);
                if (target == null || target.amount <= 0) {
                    continue;
                }
                foundItem = true;
                if (target.isFluidEqual(toCompare)) {
                    sideAllowed = true;
                    break;
                }
            }
            if (foundItem) {
                if (sideAllowed) {
                    sideCheck.increasePriority(face, 12);
                } else {
                    sideCheck.disallow(face);
                }
            }
        }
    }
}
Also used : FluidStack(net.minecraftforge.fluids.FluidStack) EnumFacing(net.minecraft.util.EnumFacing) ItemStack(net.minecraft.item.ItemStack) PipeEventHandler(buildcraft.api.transport.pipe.PipeEventHandler)

Example 3 with PipeEventHandler

use of buildcraft.api.transport.pipe.PipeEventHandler in project BuildCraft by BuildCraft.

the class PipeBehaviourLapis method onActionActivated.

@PipeEventHandler
public void onActionActivated(PipeEventActionActivate event) {
    if (event.action instanceof ActionPipeColor) {
        ActionPipeColor action = ((ActionPipeColor) event.action);
        if (this.colour != action.color) {
            this.colour = action.color;
            pipe.getHolder().scheduleNetworkUpdate(PipeMessageReceiver.BEHAVIOUR);
        }
    }
}
Also used : ActionPipeColor(buildcraft.transport.statements.ActionPipeColor) PipeEventHandler(buildcraft.api.transport.pipe.PipeEventHandler)

Example 4 with PipeEventHandler

use of buildcraft.api.transport.pipe.PipeEventHandler in project BuildCraft by BuildCraft.

the class PipeBehaviourDiamondItem method sideCheck.

@PipeEventHandler
public void sideCheck(PipeEventItem.SideCheck sideCheck) {
    ItemStack toCompare = sideCheck.stack;
    for (EnumFacing face : EnumFacing.VALUES) {
        if (sideCheck.isAllowed(face) && pipe.isConnected(face)) {
            int offset = FILTERS_PER_SIDE * face.ordinal();
            boolean sideAllowed = false;
            boolean foundItem = false;
            for (int i = 0; i < FILTERS_PER_SIDE; i++) {
                ItemStack compareTo = filters.getStackInSlot(offset + i);
                if (compareTo.isEmpty())
                    continue;
                foundItem = true;
                if (StackUtil.isMatchingItemOrList(compareTo, toCompare)) {
                    sideAllowed = true;
                    break;
                }
            }
            if (foundItem) {
                if (sideAllowed) {
                    sideCheck.increasePriority(face, 12);
                } else {
                    sideCheck.disallow(face);
                }
            }
        }
    }
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) ItemStack(net.minecraft.item.ItemStack) PipeEventHandler(buildcraft.api.transport.pipe.PipeEventHandler)

Example 5 with PipeEventHandler

use of buildcraft.api.transport.pipe.PipeEventHandler in project BuildCraft by BuildCraft.

the class PipeBehaviourDiamondItem method split.

@PipeEventHandler
public void split(PipeEventItem.Split split) {
    EnumFacing[] allSides = split.getAllPossibleDestinations().toArray(new EnumFacing[0]);
    if (allSides.length == 0 || allSides.length == 1) {
        // Nothing to split
        return;
    }
    ItemEntry[] items = split.items.toArray(new ItemEntry[0]);
    split.items.clear();
    // Note that the order doesn't matter
    for (ItemEntry item : items) {
        int[] countPerSide = new int[allSides.length];
        int totalCount = 0;
        for (int s = 0; s < allSides.length; s++) {
            int offset = FILTERS_PER_SIDE * allSides[s].ordinal();
            for (int i = 0; i < FILTERS_PER_SIDE; i++) {
                ItemStack compareTo = filters.getStackInSlot(offset + i);
                if (compareTo.isEmpty())
                    continue;
                if (StackUtil.isMatchingItemOrList(compareTo, item.stack)) {
                    int count = compareTo.getCount();
                    totalCount += count;
                    countPerSide[s] += count;
                }
            }
        }
        if (totalCount == 0) {
            totalCount = allSides.length;
            Arrays.fill(countPerSide, 1);
        } else {
            int hcf = countPerSide[0];
            for (int c : countPerSide) {
                hcf = MathUtil.findHighestCommonFactor(hcf, c);
            }
            if (hcf != 1) {
                totalCount /= hcf;
                for (int i = 0; i < countPerSide.length; i++) {
                    countPerSide[i] /= hcf;
                }
            }
        }
        /* If the stack count is divisible by the possible directions then we can just split the stack up evenly -
             * making the distribution perfect. */
        ItemEntry[] entries = new ItemEntry[allSides.length];
        ItemStack toSplit = item.stack;
        if (toSplit.getCount() >= totalCount) {
            int leftOver = toSplit.getCount() % totalCount;
            int multiples = (toSplit.getCount() - leftOver) / totalCount;
            for (int s = 0; s < allSides.length; s++) {
                ItemStack toSide = toSplit.copy();
                toSide.setCount(countPerSide[s] * multiples);
                entries[s] = new ItemEntry(item.colour, toSide, item.from);
                List<EnumFacing> dests = new ArrayList<>(1);
                dests.add(allSides[s]);
                entries[s].to = dests;
            }
            toSplit.setCount(leftOver);
        }
        if (!toSplit.isEmpty()) {
            int[] randLookup = new int[totalCount];
            int j = 0;
            for (int s = 0; s < allSides.length; s++) {
                int len = countPerSide[s];
                Arrays.fill(randLookup, j, j + len, s);
                j += len;
            }
            while (!toSplit.isEmpty()) {
                // Pick a random number between 0 and total count.
                int rand = split.holder.getPipeWorld().rand.nextInt(totalCount);
                int face = randLookup[rand];
                if (entries[face] == null) {
                    ItemStack stack = toSplit.copy();
                    stack.setCount(1);
                    ItemEntry entry = new ItemEntry(item.colour, stack, item.from);
                    List<EnumFacing> dests = entry.to = new ArrayList<>(1);
                    dests.add(allSides[face]);
                    entries[face] = entry;
                } else {
                    entries[face].stack.grow(1);
                }
                toSplit.shrink(1);
            }
        }
        for (int s = 0; s < allSides.length; s++) {
            ItemEntry entry = entries[s];
            if (entry == null) {
                continue;
            }
            split.items.add(entry);
        }
    }
}
Also used : EnumFacing(net.minecraft.util.EnumFacing) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) ItemEntry(buildcraft.api.transport.pipe.PipeEventItem.ItemEntry) PipeEventHandler(buildcraft.api.transport.pipe.PipeEventHandler)

Aggregations

PipeEventHandler (buildcraft.api.transport.pipe.PipeEventHandler)7 ItemStack (net.minecraft.item.ItemStack)4 EnumFacing (net.minecraft.util.EnumFacing)4 ArrayList (java.util.ArrayList)2 IPipe (buildcraft.api.transport.pipe.IPipe)1 IPipeHolder (buildcraft.api.transport.pipe.IPipeHolder)1 PipeEvent (buildcraft.api.transport.pipe.PipeEvent)1 ItemEntry (buildcraft.api.transport.pipe.PipeEventItem.ItemEntry)1 PipePluggable (buildcraft.api.transport.pluggable.PipePluggable)1 ActionPipeColor (buildcraft.transport.statements.ActionPipeColor)1 MethodHandle (java.lang.invoke.MethodHandle)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 EnumDyeColor (net.minecraft.item.EnumDyeColor)1 BlockPos (net.minecraft.util.math.BlockPos)1 World (net.minecraft.world.World)1 FakePlayer (net.minecraftforge.common.util.FakePlayer)1 FluidStack (net.minecraftforge.fluids.FluidStack)1