Search in sources :

Example 6 with AmadronOffer

use of pneumaticCraft.common.recipes.AmadronOffer in project PneumaticCraft by MineMaarten.

the class EventHandlerPneumaticCraft method onAmadronSuccess.

@SubscribeEvent
public void onAmadronSuccess(AmadronRetrievalEvent event) {
    EntityDrone drone = (EntityDrone) event.drone;
    AmadronOffer offer = drone.getHandlingOffer();
    boolean shouldDeliver = false;
    if (offer instanceof AmadronOfferCustom) {
        AmadronOffer realOffer = AmadronOfferManager.getInstance().get(offer);
        if (realOffer != null) {
            //If we find the non-inverted offer, that means the Drone just has completed trading with a different player.
            ((AmadronOfferCustom) realOffer).addPayment(drone.getOfferTimes());
            ((AmadronOfferCustom) realOffer).addStock(-drone.getOfferTimes());
            realOffer.onTrade(drone.getOfferTimes(), drone.getBuyingPlayer());
            shouldDeliver = true;
        }
        realOffer = AmadronOfferManager.getInstance().get(((AmadronOfferCustom) offer).copy().invert());
        if (realOffer != null) {
            //If we find the inverted offer, that means the Drone has just restocked.
            ((AmadronOfferCustom) realOffer).addStock(drone.getOfferTimes());
        }
    } else {
        shouldDeliver = true;
    }
    if (shouldDeliver) {
        ItemStack usedTablet = drone.getUsedTablet();
        if (offer.getOutput() instanceof ItemStack) {
            ItemStack offeringItems = (ItemStack) offer.getOutput();
            int producedItems = offeringItems.stackSize * drone.getOfferTimes();
            List<ItemStack> stacks = new ArrayList<ItemStack>();
            while (producedItems > 0) {
                ItemStack stack = offeringItems.copy();
                stack.stackSize = Math.min(producedItems, stack.getMaxStackSize());
                stacks.add(stack);
                producedItems -= stack.stackSize;
            }
            ChunkPosition pos = ItemAmadronTablet.getItemProvidingLocation(usedTablet);
            if (pos != null) {
                World world = PneumaticCraftUtils.getWorldForDimension(ItemAmadronTablet.getItemProvidingDimension(usedTablet));
                PneumaticRegistry.getInstance().deliverItemsAmazonStyle(world, pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ, stacks.toArray(new ItemStack[stacks.size()]));
            }
        } else {
            FluidStack offeringFluid = ((FluidStack) offer.getOutput()).copy();
            offeringFluid.amount *= drone.getOfferTimes();
            ChunkPosition pos = ItemAmadronTablet.getLiquidProvidingLocation(usedTablet);
            if (pos != null) {
                World world = PneumaticCraftUtils.getWorldForDimension(ItemAmadronTablet.getLiquidProvidingDimension(usedTablet));
                PneumaticRegistry.getInstance().deliverFluidAmazonStyle(world, pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ, offeringFluid);
            }
        }
    }
}
Also used : EntityDrone(pneumaticCraft.common.entity.living.EntityDrone) FluidStack(net.minecraftforge.fluids.FluidStack) ChunkPosition(net.minecraft.world.ChunkPosition) AmadronOffer(pneumaticCraft.common.recipes.AmadronOffer) ArrayList(java.util.ArrayList) ItemStack(net.minecraft.item.ItemStack) World(net.minecraft.world.World) AmadronOfferCustom(pneumaticCraft.common.recipes.AmadronOfferCustom) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 7 with AmadronOffer

use of pneumaticCraft.common.recipes.AmadronOffer in project PneumaticCraft by MineMaarten.

the class AmadronOfferConfig method readFromJson.

@Override
protected final void readFromJson(JsonObject json) {
    readFromJsonCustom(json);
    JsonArray array = (JsonArray) json.get("offers");
    Collection<AmadronOffer> offers = getOffers();
    offers.clear();
    for (JsonElement element : array) {
        AmadronOffer offer = ((JsonObject) element).has("inStock") ? AmadronOfferCustom.fromJson((JsonObject) element) : AmadronOffer.fromJson((JsonObject) element);
        if (offer != null) {
            offers.add(offer);
        }
    }
}
Also used : JsonArray(com.google.gson.JsonArray) JsonElement(com.google.gson.JsonElement) AmadronOffer(pneumaticCraft.common.recipes.AmadronOffer) JsonObject(com.google.gson.JsonObject)

Example 8 with AmadronOffer

use of pneumaticCraft.common.recipes.AmadronOffer in project PneumaticCraft by MineMaarten.

the class ContainerAmadron method handleGUIButtonPress.

@Override
public void handleGUIButtonPress(int guiID, EntityPlayer player) {
    super.handleGUIButtonPress(guiID, player);
    if (guiID == 1) {
        for (int i = 0; i < shoppingItems.length; i++) {
            if (shoppingItems[i] >= 0) {
                AmadronOffer offer = offers.get(shoppingItems[i]);
                ChunkPosition itemPos = ItemAmadronTablet.getItemProvidingLocation(player.getCurrentEquippedItem());
                World itemWorld = null;
                if (itemPos == null) {
                    itemPos = new ChunkPosition((int) player.posX, (int) player.posY, (int) player.posZ);
                    itemWorld = player.worldObj;
                } else {
                    itemWorld = PneumaticCraftUtils.getWorldForDimension(ItemAmadronTablet.getItemProvidingDimension(player.getCurrentEquippedItem()));
                }
                ChunkPosition liquidPos = ItemAmadronTablet.getLiquidProvidingLocation(player.getCurrentEquippedItem());
                World liquidWorld = null;
                if (liquidPos != null) {
                    liquidWorld = PneumaticCraftUtils.getWorldForDimension(ItemAmadronTablet.getLiquidProvidingDimension(player.getCurrentEquippedItem()));
                }
                EntityDrone drone = retrieveOrderItems(offer, shoppingAmounts[i], itemWorld, itemPos, liquidWorld, liquidPos);
                if (drone != null)
                    drone.setHandlingOffer(offer, shoppingAmounts[i], player.getCurrentEquippedItem(), player.getCommandSenderName());
            }
        }
        Arrays.fill(shoppingAmounts, 0);
        Arrays.fill(shoppingItems, -1);
    } else if (guiID == 2) {
        player.openGui(PneumaticCraft.instance, CommonProxy.EnumGuiId.AMADRON_ADD_TRADE.ordinal(), player.worldObj, 0, 0, 0);
    }
}
Also used : EntityDrone(pneumaticCraft.common.entity.living.EntityDrone) ChunkPosition(net.minecraft.world.ChunkPosition) AmadronOffer(pneumaticCraft.common.recipes.AmadronOffer) World(net.minecraft.world.World)

Example 9 with AmadronOffer

use of pneumaticCraft.common.recipes.AmadronOffer in project PneumaticCraft by MineMaarten.

the class ItemAmadronTablet method setShoppingCart.

public static void setShoppingCart(ItemStack tablet, Map<AmadronOffer, Integer> cart) {
    NBTTagList list = new NBTTagList();
    for (Map.Entry<AmadronOffer, Integer> entry : cart.entrySet()) {
        NBTTagCompound tag = new NBTTagCompound();
        entry.getKey().writeToNBT(tag);
        tag.setInteger("amount", entry.getValue());
        list.appendTag(tag);
    }
    NBTUtil.setCompoundTag(tablet, "shoppingCart", list);
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) AmadronOffer(pneumaticCraft.common.recipes.AmadronOffer) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) HashMap(java.util.HashMap) Map(java.util.Map)

Example 10 with AmadronOffer

use of pneumaticCraft.common.recipes.AmadronOffer in project PneumaticCraft by MineMaarten.

the class PacketSyncAmadronOffers method toBytes.

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(offers.size());
    for (AmadronOffer offer : offers) {
        buf.writeBoolean(offer instanceof AmadronOfferCustom);
        writeFluidOrItemStack(offer.getInput(), buf);
        writeFluidOrItemStack(offer.getOutput(), buf);
        if (offer instanceof AmadronOfferCustom) {
            ((AmadronOfferCustom) offer).writeToBuf(buf);
        }
    }
}
Also used : AmadronOffer(pneumaticCraft.common.recipes.AmadronOffer) AmadronOfferCustom(pneumaticCraft.common.recipes.AmadronOfferCustom)

Aggregations

AmadronOffer (pneumaticCraft.common.recipes.AmadronOffer)10 ItemStack (net.minecraft.item.ItemStack)3 EntityDrone (pneumaticCraft.common.entity.living.EntityDrone)3 AmadronOfferCustom (pneumaticCraft.common.recipes.AmadronOfferCustom)3 JsonArray (com.google.gson.JsonArray)2 SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)2 NBTTagList (net.minecraft.nbt.NBTTagList)2 ChunkPosition (net.minecraft.world.ChunkPosition)2 World (net.minecraft.world.World)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 Point (java.awt.Point)1 IOException (java.io.IOException)1 Map (java.util.Map)1 FluidStack (net.minecraftforge.fluids.FluidStack)1 AmadronRetrievalEvent (pneumaticCraft.api.drone.AmadronRetrievalEvent)1 WidgetAmadronOffer (pneumaticCraft.client.gui.widget.WidgetAmadronOffer)1