How do I call an itemStack from another Class? - java

I'm making a Custom Horse plugin and this is the first real plugin that I'm making so please excuse my messy code.
In one class (HorseSaddles) I have created 4 different saddles:
package io.github.bxnie.Items;
import java.util.ArrayList;
import org.bukkit.Material;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.md_5.bungee.api.ChatColor;
public class HorseSaddles implements Listener {
public void DonkeyHorse() {
ItemStack dhorse = new ItemStack(Material.SADDLE);
ItemMeta dhorsemeta = dhorse.getItemMeta();
dhorsemeta.setDisplayName(ChatColor.RED + "Donkey");
ArrayList<String> dhorselore = new ArrayList<String>();
dhorselore.add(ChatColor.DARK_GRAY + "1/4");
dhorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Donkey!");
dhorsemeta.setLore(dhorselore);
dhorse.setItemMeta(dhorsemeta);
}
public void BrownHorse() {
ItemStack brhorse = new ItemStack(Material.SADDLE);
ItemMeta brhorsemeta = brhorse.getItemMeta();
brhorsemeta.setDisplayName(ChatColor.RED + "Brown Horse");
ArrayList<String> brhorselore = new ArrayList<String>();
brhorselore.add(ChatColor.DARK_GRAY + "2/4");
brhorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Horse!");
brhorsemeta.setLore(brhorselore);
brhorse.setItemMeta(brhorsemeta);
}
public void BlackHorse() {
ItemStack blhorse = new ItemStack(Material.SADDLE);
ItemMeta blhorsemeta = blhorse.getItemMeta();
blhorsemeta.setDisplayName(ChatColor.BLACK + "Black" + ChatColor.RED + "Horse");
ArrayList<String> blhorselore = new ArrayList<String>();
blhorselore.add(ChatColor.DARK_GRAY + "3/4");
blhorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Horse!");
blhorsemeta.setLore(blhorselore);
blhorse.setItemMeta(blhorsemeta);
}
public void WhiteHorse() {
ItemStack whorse = new ItemStack(Material.SADDLE);
ItemMeta whorsemeta = whorse.getItemMeta();
whorsemeta.setDisplayName(ChatColor.WHITE + "White" + ChatColor.RED + "Horse");
ArrayList<String> whorselore = new ArrayList<String>();
whorselore.add(ChatColor.DARK_GRAY + "4/4");
whorselore.add(ChatColor.DARK_GRAY + "Right click to spawn your Horse!");
whorsemeta.setLore(whorselore);
whorse.setItemMeta(whorsemeta);
}
}
And in another class (SaddleCommands) I am attempting to create a command which will give the user that an that item if they have the correct permissions
package io.github.bxnie.commands;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import net.minecraft.server.v1_12_R1.CommandExecute;
public class SaddleCommands extends CommandExecute implements Listener, CommandExecutor{
public String cmd1 = "fhdonkey";
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] arg3) {
if (!(sender instanceof Player)) {
sender.sendMessage("Only players may execute this command!");
return true;
}
Player p = (Player) sender;
if(cmd.getName().equalsIgnoreCase(cmd1)) {
if (p.hasPermission("fh.donkey")) {
p.sendMessage("You Have Purchased a Donkey");
p.getInventory().addItem(...);
return true;
} else {
p.sendMessage("§cInsufficient Permission!");
return false;
}
}
return false;
}
}
where it says p.getInventory().addItem(...); i put in the 3dots as a placeholder as I don't know how to retrieve an item from another class.

Welcome to the forum :)
You created an Class HorseSaddles. In there you got 3 functions which create an ItemStack, without doing anything with the object. Simply just return the ItemStack object.
i created an minimalistic solution:
public class Horse {
// we create an function which returns an ItemStack
public ItemStack getBrownHorseItemStack() {
// Create the ItemStack Object
ItemStack horseItem = new Itemstack(Material.SADDLE);
// return our ItemStack Object
return horseItem;
}
}
class Saddle {
private Horse horse = new Horse();
#Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
// an CommandSender object is NOT always an Player Object. Normally we would check if sender is a Player Object
// We cast the CommandSender to an player
Player player = (Player) sender;
// check the command received
if (cmd.getName().equalsIgnoreCase("fhdonkey")) {
// horse.getBrownHorseItemStack() will return an ItemStack Object
player.getInventory().addItem(horse.getBrownHorseItemStack());
}
return false;
}
}
for more informations: Returning a Value from a Method

Related

Why java class not do GET request

I made java script, it save/load player inventory when he leave/join, but someting goes wrong(GET request dont send)
here is classes.
InventorySync:
import com.google.gson.Gson;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.IOException;
import java.util.Arrays;
import java.util.Base64;
import java.util.UUID;
import InventoryGet.*;
public class InventorySync extends JavaPlugin implements Listener {
String saveInv = "*correct url*";
String loadInv = "*correct url*";
Gson gson = new Gson();
#EventHandler
public void onJoin(PlayerJoinEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
//InventoryGet.main(loadInv + "?username=" + uuid.toString());
}
#EventHandler
public void onQuit(PlayerQuitEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
Object[] contents = Arrays.stream(player.getInventory().getContents()).toArray();
String inv = Base64.getEncoder().encodeToString( gson.toJson(contents).getBytes() );
InventoryGet.main(saveInv + "?username=" + uuid.toString() + "&inventory=" + inv);
}
}
InventoryGet:
package InventoryGet;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
public class InventoryGet {
public static String main(String urlString) throws IOException {
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
Scanner scanner = new Scanner(connection.getInputStream());
String response = scanner.useDelimiter("\\A").next();
scanner.close();
return response;
} else {
return "GET request failed with response code " + responseCode;
}
} catch (Exception e) {
return "Exception occurred: " + e.getMessage();
}
}
}
The URL is correct.
For the PHP script:
Update inventory:
<?php
//Get player nickname
//check username for exist
if (!isset($_GET['username']))
{
die();
} else { //username exist:
$username = $_GET['username'];
}
//get player inventory(if exist)
if (!isset($_GET['inventory'])) {
die();
} else { //decode inventory from base64:
$inventory = base64_decode($_GET['inventory']);
} //save inventory to file
file_put_contents('inventory/' . $username . '.inv', $inventory);
Get inventory:
<?php
header("content-type: application/json");
//Get player nickname
//check username for exist
if (!isset($_GET['username'])) {
die('[]');
} else { // username exist
$username = $_GET['username'];
}
if (file_exists('inventory/' . $username . '.inv')) { //find player's inventory if exists
echo file_get_contents('inventory/' . $username . '.inv');
} else { //player file not found:
echo '[]';
}
When player disconnect from server, server save his inventory, but server not do a GET request
You said it's full code, so there is multiple issues :
You are in the JavaPlugin class, and there isn't onEnable() method that register listener. So, you should add:
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
}
I suggest you to create a new class for listeners, to have (I omitted import):
public class InventorySync extends JavaPlugin {
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(new ConnectionListener(), this);
}
}
public class ConnectionListener implements Listener {
String saveInv = "*correct url*";
String loadInv = "*correct url*";
Gson gson = new Gson();
#EventHandler
public void onJoin(PlayerJoinEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
//InventoryGet.main(loadInv + "?username=" + uuid.toString());
}
#EventHandler
public void onQuit(PlayerQuitEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
Object[] contents = Arrays.stream(player.getInventory().getContents()).toArray();
String inv = Base64.getEncoder().encodeToString( gson.toJson(contents).getBytes() );
InventoryGet.main(saveInv + "?username=" + uuid.toString() + "&inventory=" + inv);
}
}
You should not use GET request to update datas. The method POST is here.
You are wrongly using gson according to Spigot API. You should use ItemStack#serialize and ItemStack#deserialize, like that:
#EventHandler
public void onJoin(PlayerJoinEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
String inv = main(loadInv + "?username=" + uuid.toString());
List<Map<String, Object>> list = gson.fromJson(inv, List.class);
list.stream().map(ItemStack::deserialize).forEach(it -> {
// here you have items
});
}
#EventHandler
public void onQuit(PlayerQuitEvent event) throws IOException {
Player player = event.getPlayer();
UUID uuid = player.getUniqueId();
List<Map<String, Object>> contents = Arrays.stream(player.getInventory().getContents()).filter(Objects::nonNull).map(ItemStack::serialize).collect(Collectors.toList());
String inv = Base64.getEncoder().encodeToString(gson.toJson(contents).getBytes());
main(saveInv + "?username=" + uuid.toString() + "&inventory=" + inv);
}
I added filter(Objects::nonNull) to prevent empty slot

Modded Command in Minecraft 1.8.9 command doesn't exist in Multiplayer server

I am developing some minecraft mod for 1.8.9.
What I'm trying to create is command that simply send message to sender.
here's the code for command class and main class
command class:
package happyandjust.happymod.commands;
import java.util.HashMap;
import java.util.List;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.EnumChatFormatting;
public class Command extends CommandBase {
private HashMap<String, String> collection = new HashMap<String, String>();
#Override
public String getCommandName() {
return "collection";
}
#Override
public String getCommandUsage(ICommandSender sender) {
return "collection <enchant name>";
}
#Override
public void processCommand(ICommandSender sender, String[] args) throws CommandException {
collection.put("harvesting", "Wheat Collection Level 2");
collection.put("cubism", "Pumpkin Collection Level 5");
if (args.length < 1) {
sender.addChatMessage(new ChatComponentText(EnumChatFormatting.RED + "Usage: /collection [Enchant Name]"));
return;
}
if (args.length == 1) {
String enchant_name = args[0].toLowerCase();
String collec = collection.get(enchant_name);
if (collec == null) {
sender.addChatMessage(new ChatComponentText(
EnumChatFormatting.RED + enchant_name.toUpperCase() + " is not valid Enchant Name"));
return;
}
sender.addChatMessage(new ChatComponentText(
EnumChatFormatting.GREEN + enchant_name.toUpperCase() + " is at " + collection.get(enchant_name)));
}
}
#Override
public boolean canCommandSenderUseCommand(ICommandSender sender) {
return true;
}
}
main class:
package happyandjust.happymod.main;
import happyandjust.happymod.commands.Command;
import happyandjust.happymod.proxy.CommonProxy;
import happyandjust.happymod.util.Reference;
import net.minecraftforge.client.ClientCommandHandler;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
#Mod(modid = Reference.MOD_ID, name = Reference.NAME, version = Reference.VERSION)
public class HappyMod {
#Instance
public static HappyMod instance;
#SidedProxy(clientSide = Reference.CLIENT_PROXY_CLASS, serverSide = Reference.COMMON_PROXY_CLASS)
public static CommonProxy proxy;
#EventHandler
public static void preInit(FMLPostInitializationEvent e) {
}
#EventHandler
public static void init(FMLInitializationEvent e) {
ClientCommandHandler.instance.registerCommand(new Command());
}
#EventHandler
public static void postInit(FMLPostInitializationEvent e) {
}
}
It works fine in single player but if I went to the multi player server like hypixel.
It says "Unknown command"
I have no idea to do this
Can anyone help me to work this command in multi player server?
You need to override the getRequiredPermissionLevel() method from CommandBase for it to work on multiplayer.
#Override
public int getRequiredPermissionLevel() {
return 0;
}

The constructor Commands() is undefined - Java / SpigotMC

I know there are other questions like this but none that I could find are fully helpful to this case.
So basically on the line that says
getCommand("minealchemy").setExecutor(new Commands());
I get this error:
The constructor Commands() is undefined
Any help? When i put "null" in the () for Commands ... Commands(null)... then i get a NullPointerException error...
Here are all of my classes:
Main Class:
package me.zachbears27;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
import me.zachbears27.utils.Commands;
public class Main extends JavaPlugin implements Listener {
#Override
public void onDisable() {
}
#Override
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
registerConfig();
getCommand("minealchemy").setExecutor(new Commands());
}
public void registerConfig() {
saveDefaultConfig();
}
}
Commands Class:
package me.zachbears27.utils;
import java.io.File;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import me.zachbears27.utils.Methods;
import net.md_5.bungee.api.ChatColor;
public class Commands implements CommandExecutor {
private final Methods methods;
public Commands(Methods methods) {
this.methods = methods;
}
public boolean onCommand(CommandSender sender, Command label, String cmd, String[] args) {
Player p = (Player) sender;
if (cmd.equalsIgnoreCase("minealchemy")) {
if(args.length > 0) {
//Get File
File file = methods.getPlayerFile(p.getUniqueId());
FileConfiguration fileSettings = YamlConfiguration.loadConfiguration(file);
//Make Sure It's False
fileSettings.set("Enabled", false);
//Save Inventory
methods.savePlayerInv(p.getUniqueId(), p.getInventory().getContents());
//Clear It
p.getInventory().clear();
//Put In God Mode
p.setInvulnerable(true);
//Open Inventory
p.openWorkbench(p.getLocation(), true);
} else {
p.sendMessage(ChatColor.RED + "Incorrect Arguments... Please use \"start\" or \"list\".");
}
}
return true;
}
}
Methods Class:
package me.zachbears27.utils;
import java.io.File;
import java.util.ArrayList;
import java.util.UUID;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import me.zachbears27.Main;
import net.md_5.bungee.api.ChatColor;
public class Methods {
private final Main plugin;
public Methods(Main plugin) {
this.plugin = plugin;
}
public ArrayList<String> getAllItems() {
ArrayList<String> allItems = new ArrayList<String>();
//Basics
String Air = ChatColor.GREEN + "Air" + ChatColor.RESET;
String Earth = ChatColor.GREEN + "Earth" + ChatColor.RESET;
String Fire = ChatColor.GREEN + "Fire" + ChatColor.RESET;
String Metal = ChatColor.GREEN + "Metal" + ChatColor.RESET;
String Water = ChatColor.GREEN + "Water" + ChatColor.RESET;
String Motion = ChatColor.GREEN + "Motion" + ChatColor.RESET;
String Big = ChatColor.GREEN + "Big" + ChatColor.RESET;
String Time = ChatColor.GREEN + "Time" + ChatColor.RESET;
String Small = ChatColor.GREEN + "Small" + ChatColor.RESET;
allItems.add(Air);
allItems.add(Earth);
allItems.add(Fire);
allItems.add(Metal);
allItems.add(Water);
allItems.add(Motion);
allItems.add(Big);
allItems.add(Time);
allItems.add(Small);
//Animals
String Cat = ChatColor.GOLD + "Cat" + ChatColor.RESET;
allItems.add(Cat);
return allItems;
}
public String addItems(String item1, String item2) {
String returnStatement = ChatColor.RED + "X";
if(item1.equals("Air") && item2.equals("Fire")) {
returnStatement = "Mist";
}
return returnStatement;
}
public File getPlayerFile(UUID playerUUID) {
//Creates The Player File
File playerFile = new File (plugin.getDataFolder() + File.separator + "Player Data", playerUUID + ".yml");
return playerFile;
}
public void savePlayerInv(UUID playerUUID, ItemStack[] inv) {
File playerFile = getPlayerFile(playerUUID);
FileConfiguration playerData = YamlConfiguration.loadConfiguration(playerFile);
playerData.set("SavedInventory", inv);
}
}
Change the .setExecutor(new Commands()) in the onEnable() function to
.setExecutor(new Commands(new Methods(this)))

Android - Printing receipt to Bluetooth Printer

Most of the related answers and google contain pretty old contributions referring to this topic. So Im looking for a way to make my Android-Application print receipts (58 mm in width) via a bluetooth thermal receipt printer. Is it necessary to use a printer with a 3rd party API? I thought about to buy a regular bluetooth printer, that can be connected to my device and use the default Android print manager to make a layout for the receipt. Is that possible? Are there any samples, docs or tutorials? Would appreciate it. Thanks in advance
Yes, it's necesary to use a 3rd party SDK. I recommend you Zebra Printers, the have good documentation and code samples, that makes it easy compared to other brands. Download the SDK from here
To make your labels you can use ZebraDesigner that let you visualy create the layout and gives you as output ZPL code that you can send from your app to the Bluetooth printer. Click here to see it.
Hi here is an example of how to print using zebra from a cordova application
package com.custom.plugin;
import android.os.Build;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.Context;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import java.util.Set;
import com.zebra.android.discovery.*;
import com.zebra.sdk.comm.*;
import com.zebra.sdk.printer.*;
import com.zebra.sdk.printer.SGD;
/**
* This class echoes a string called from JavaScript.
*/
public class PrinterDanBreakingNews extends CordovaPlugin {
#Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("getListPairedDevices")) {
this.getListPairedDevices(callbackContext);
return true;
}else if(action.equals("print"))
{
this.print(callbackContext,args);
return true;
}
return false;
}
private void getListPairedDevices(CallbackContext callbackContext)
{
try
{
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if( !bluetoothAdapter.isEnabled() )
{
callbackContext.error("Favor encienda el bluetooth");
}
Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
JSONArray lista = new JSONArray();
for (BluetoothDevice device : pairedDevices) {
String bname = device.getName();
String bmac = device.getAddress();
String btype
= getBTMajorDeviceClass(
device.getBluetoothClass().getMajorDeviceClass()
);
JSONObject item = new JSONObject();
item.put("name", bname);
item.put("type", btype);
item.put("mac", bmac);
lista.put(item);
}
callbackContext.success(lista.toString());
}
callbackContext.error("Error. no hay dispositivos registrados");
}
catch( Exception ex )
{
callbackContext.error("Error. " + ex.toString());
}
}
private String getBTMajorDeviceClass(int major){
switch(major){
case BluetoothClass.Device.Major.AUDIO_VIDEO:
return "AUDIO_VIDEO";
case BluetoothClass.Device.Major.COMPUTER:
return "COMPUTER";
case BluetoothClass.Device.Major.HEALTH:
return "HEALTH";
case BluetoothClass.Device.Major.IMAGING:
return "IMAGING";
case BluetoothClass.Device.Major.MISC:
return "MISC";
case BluetoothClass.Device.Major.NETWORKING:
return "NETWORKING";
case BluetoothClass.Device.Major.PERIPHERAL:
return "PERIPHERAL";
case BluetoothClass.Device.Major.PHONE:
return "PHONE";
case BluetoothClass.Device.Major.TOY:
return "TOY";
case BluetoothClass.Device.Major.UNCATEGORIZED:
return "UNCATEGORIZED";
case BluetoothClass.Device.Major.WEARABLE:
return "WEARABLE";
default: return "unknown!";
}
}
public void print(final CallbackContext callbackContext, final JSONArray args){
try
{
new Thread(new Runnable() {
#Override
public void run() {
try {
String mac = "";
String template = "";
String length = "";
if( args.length() > 0 ) {
JSONArray row = args.getJSONArray(0);
JSONObject item = row.getJSONObject(0);
mac = item.getString("mac");
template = item.getString("template");
length = item.getString("length");
}
Connection thePrinterConn = new BluetoothConnectionInsecure(mac);
if (isPrinterReady(thePrinterConn)) {
thePrinterConn.open();
SGD.SET("device.languages", "zpl", thePrinterConn);
SGD.SET("ezpl.media_type", "continuous", thePrinterConn);
SGD.SET("zpl.label_length", length, thePrinterConn);
thePrinterConn.write(template.getBytes());
Thread.sleep(500);
thePrinterConn.close();
callbackContext.success("listo");
} else {
callbackContext.error("Error3, la impresora no esta lista");
}
} catch (Exception ex) {
callbackContext.error("Error2. " + ex.toString());
}
}
}).start();
}
catch(Exception ex)
{
callbackContext.error("Error1. " + ex.toString());
}
}
private Boolean isPrinterReady(Connection connection)
throws Exception {
Boolean isOK = false;
connection.open();
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
PrinterStatus printerStatus = printer.getCurrentStatus();
if (printerStatus.isReadyToPrint) {
isOK = true;
} else if (printerStatus.isPaused) {
//throw new ConnectionException("Cannot print because the printer is paused");
} else if (printerStatus.isHeadOpen) {
//throw new ConnectionException("Cannot print because the printer media door is open");
} else if (printerStatus.isPaperOut) {
//throw new ConnectionException("Cannot print because the paper is out");
} else {
//throw new ConnectionException("Cannot print");
}
//connection.open();
return isOK;
}
}
In the function getListPairedDevices you can get a list of mac addresses to use and then use function print to send your ZLP code.
Here is some ZPL test.
CT~~CD,~CC^~CT~
^XA~TA000~JSN^LT0^MNW^MTD^PON^PMN^LH0,0^JMA^PR4,4~SD0^JUS^LRN^CI0^XZ
^XA
^MMT
^PW575
^LL0799
^LS0
^FT35,720^A0N,20,19^FH\^FDHORA^FS
^FT35,680^A0N,20,19^FH\^FDFECHA^FS
^FT34,644^A0N,20,19^FH\^FDCOURRIER^FS
^FT34,609^A0N,20,19^FH\^FDADR2^FS
^FT34,572^A0N,20,19^FH\^FDADR1^FS
^FT34,533^A0N,20,19^FH\^FDSUCURSAL^FS
^FT34,498^A0N,20,19^FH\^FDDESTINATARIO^FS
^FT34,461^A0N,20,19^FH\^FDREMITENTE^FS
^FT165,720^A0N,20,19^FH\^FD: VHORA^FS
^FT165,680^A0N,20,19^FH\^FD: VFECHA^FS
^FT165,644^A0N,20,19^FH\^FD: VCOURRIER^FS
^FT166,534^A0N,20,19^FH\^FD: VSUCURSAL^FS
^FT166,426^A0N,20,19^FH\^FD: VEMPAQUE^FS
^FT166,461^A0N,20,19^FH\^FD: VREMITENTE^FS
^FT166,497^A0N,20,19^FH\^FD: VDESTINATARIO^FS
^FT34,425^A0N,20,19^FH\^FDEMPAQUE^FS
^FT136,365^A0N,23,24^FH\^FD1138 CHO-CHO-1-1-1-1^FS
^FT185,325^A0N,23,24^FH\^FDGUIA: 11389942705^FS
^FT165,46^A0N,28,28^FH\^FDDIRECTO LOGISTICS^FS
^FT25,214^A0N,138,139^FH\^FD1/2^FS
^FT380,265^BQN,2,8
^FH\^FDLA,101010^FS
^FO20,281^GB536,0,3^FS
^PQ1,0,1,Y^XZ

IB Java API: Extracting ticker data (real time bars) for multiple contracts

I'm doing some self-learning and experimentation with algorithmic trading and the IB API. I decided to use Java but I'm open to switching to C++. I went through an online tutorial that walks you through the code shown below but was wondering about extending it past just one stock. I want to go through all SP500 stocks and check ticker data to make decisions based on that.
The code below will create a contract for and get data for Microsoft but I'd like to get data for all 500 stocks. All of the other methods defined in the EWrapper interface were left out of the post for more ease of readability.
I'm thinking that I need to store the ticker symbols in a file, parse this, and add each contract one by one to a vector. However, I'm not sure about how to monitor the data after that. It would be nice if I could just sequentially loop through each ticker and make a request for data but I believe the stream is processed on an asynchronous thread (correct me if wrong.)
So how do I go through all 500 stocks and check their ticker data?
Code snippets and explanations would be appreciated. Thanks!
// Import Java utilities and Interactive Brokers API
import java.util.Vector;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;
import com.ib.client.TagValue;
import com.ib.client.CommissionReport;
import com.ib.client.UnderComp;
// RealTimeBars Class is an implementation of the
// IB API EWrapper class
public class RealTimeBars implements EWrapper
{
// Keep track of the next ID
private int nextOrderID = 0;
// The IB API Client Socket object
private EClientSocket client = null;
public RealTimeBars ()
{
// Create a new EClientSocket object
client = new EClientSocket (this);
// Connect to the TWS or IB Gateway application
// Leave null for localhost
// Port Number (should match TWS/IB Gateway configuration
client.eConnect (null, 7496, 0);
// Pause here for connection to complete
try
{
// Thread.sleep (1000);
while (! (client.isConnected()));
} catch (Exception e) {
e.printStackTrace ();
};
// Create a new contract
Contract contract = new Contract ();
contract.m_symbol = "MSFT";
contract.m_exchange = "SMART";
contract.m_secType = "STK";
contract.m_primaryExch = "NASDAQ";
contract.m_currency = "USD";
// Create a TagValue list
Vector<TagValue> realTimeBarsOptions = new Vector<TagValue>();
// Make a call to start off data retrieval
client.reqRealTimeBars(0, contract,
5, // Bar Size 5 seconds
"TRADES", // whatToShow
false, // useRTH
realTimeBarsOptions);
// At this point our call is done and any market data events
// will be returned via the realtimeBar method
}
public static void main (String args[])
{
try
{
// Create an instance
// At this time a connection will be made
// and the request for market data will happen
RealTimeBars myData = new RealTimeBars();
}
catch (Exception e)
{
e.printStackTrace ();
}
}
}
I don't know how this will work for all 500, but you can try. The data is from https://raw.githubusercontent.com/datasets/s-and-p-500-companies/master/data/constituents.csv
SP
package sp;
import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import com.ib.client.EWrapper;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
public class SP {
//just a sample, like this so you can just use Files.lines instead.
private static List<String> lines = Arrays.asList(new String[]{
"Symbol,Name,Sector",
"MMM,3M Company,Industrials",
"ABT,Abbott Laboratories,Health Care",
"ABBV,AbbVie,Health Care",
"ACN,Accenture plc,Information Technology",
"ATVI,Activision Blizzard,Information Technology",
"AYI,Acuity Brands Inc,Industrials",
"ADBE,Adobe Systems Inc,Information Technology",
"AAP,Advance Auto Parts,Consumer Discretionary",
"AES,AES Corp,Utilities",
"AET,Aetna Inc,Health Care",
"AMG,Affiliated Managers Group Inc,Financials",
"AFL,AFLAC Inc,Financials",
"A,Agilent Technologies Inc,Health Care",
"APD,Air Products & Chemicals Inc,Materials",
"AKAM,Akamai Technologies Inc,Information Technology",
});
public static void main(String[] args) throws InterruptedException{
EWrapper wrapper = new Wrapper();
EClientSocket socket = new EClientSocket(wrapper);
socket.eConnect("", 4001, 123);
//supposedly gives frozen last recorded value, not working!
socket.reqMarketDataType(2);
AtomicInteger tickerId = new AtomicInteger(0);
lines.stream().skip(1).forEach(line -> {
//new cont for every request
Contract cont = new Contract();
cont.m_currency = "usd";
cont.m_exchange = "smart";
cont.m_secType = "stk";
cont.m_symbol = line.split(",")[0];
Data data = new Data(cont, socket);
});
//need you own logic for when to end program
//Thread.sleep(5000);//this thread, Socket starts a reader thread
//socket.eDisconnect();
}
}
Wrapper
package sp;
import com.ib.client.CommissionReport;
import com.ib.client.Contract;
import com.ib.client.ContractDetails;
import com.ib.client.EWrapper;
import com.ib.client.Execution;
import com.ib.client.Order;
import com.ib.client.OrderState;
import com.ib.client.TickType;
import com.ib.client.UnderComp;
import java.util.HashMap;
import java.util.Map;
public class Wrapper implements EWrapper{
public Map<Integer, Data> dataMap = new HashMap<>();
public Map<Integer, Strat> orderMap = new HashMap<>();
//reqMktData snapshots are received here
#Override
public void tickPrice(int tickerId, int field, double price, int canAutoExecute) {
if (field == TickType.LAST) {
//if you just want the last price
dataMap.get(tickerId).dataRecd(price);
}
}
#Override
public void execDetails(int reqId, Contract contract, Execution execution) {
orderMap.get(execution.m_orderId).exec(execution);
}
//snip
}
Data
package sp;
import com.ib.client.Contract;
import com.ib.client.EClientSocket;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class Data {
final Contract cont;
private final EClientSocket socket;
private final Strat strat;
private static int nextId = 1; //auto increment for each request
private final int myId;
List<Double> prices = new ArrayList<>();
double lastPrice = -1;
public Data(Contract cont, EClientSocket socket) {
this.cont = cont;
this.socket = socket;
strat = new Strat(this, socket);
myId = nextId++;
((Wrapper) socket.wrapper()).dataMap.put(myId, this);
reqData();
// //call every 10 min
// Timer timer = new Timer();
// timer.schedule(new TimerTask() {
// #Override
// public void run() {
// reqData();
// }
// }, 10 * 60 * 1000);
}
private void reqData(){
socket.reqMktData(myId, cont, "", false /* true */, null);
}
public void dataRecd(double last){
lastPrice = last;
prices.add(last);
strat.check();
}
}
Strat
package sp;
import com.ib.client.EClientSocket;
import com.ib.client.Execution;
public class Strat {
public static final int NULL=0, LOOK=1<<0, LONG=1<<1, SHORT=1<<2, WAIT_FILL=1<<3, WAIT_CANCEL=1<<4;
public int sysState = NULL;
private final Data data;
private final EClientSocket socket;
private static int nextOrderId = 1;
Strat(Data data, EClientSocket socket) {
this.data = data;
this.socket = socket;
sysState = LOOK;
}
void check() {
System.out.println("should I buy? "+ data.cont.m_symbol + " # " + data.lastPrice);
/*if (false && sysState & LOOK == LOOK) {
((Wrapper) socket.wrapper()).orderMap.put(nextOrderId, this);
socket.placeOrder(nextOrderId++, data.cont, new Order());
sysState = WAIT_FILL;
nextOrderId++;
}*/
}
public void exec(Execution exec){
//will be called by wrapper after an exec.
//sysState = LONG; //or whatever
}
}

Categories