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);
}
}
}
}
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);
}
}
}
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);
}
}
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);
}
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);
}
}
}
Aggregations