Saving Inventory with Dependency Injection











up vote
0
down vote

favorite












I'm trying to open someone's Inventory in a different class than were I created the Inventory. I don't want to use statics so I tried a workaround with Dependency Injections. I made an ArrayList inventories, after that I add the created Inventory to the ArrayList. Here's the problem though, on reload the ArrayList gets wiped (of course), would there be a solution to still keep the Inventory in life without having it to be written down in a config or database. Because now players won't be able to open this created Inventory anymore after a reload, I really wonder what other plugins use to counter this problem (if they don't use statics of course ;)).



Here's some code I use:



private final Inventory inv;
private ArrayList<Inventory> inventories;
private KitPvP kitPvP;
private String name;
private Material material;

public KitManager(KitPvP kitPvP, String name, Material material, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.name = ChatColor.translateAlternateColorCodes('&', name);
this.material = material;
this.inventories = inventories;

this.inv = Bukkit.createInventory(null, 9, this.name);
}

public void createKit(Player p) {
ItemStack itemStack = new ItemStack(this.material);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(this.name);
itemStack.setItemMeta(itemMeta);
p.getInventory().addItem(itemStack);

this.inventories.add(this.inv);

p.openInventory(this.inv);
}


Event class



    private KitPvP kitPvP;
private ArrayList<Inventory> inventories;

public InteractListener(KitPvP kitPvP, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.inventories = inventories;
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();

p.sendMessage(this.inventories.toString());
}









share|improve this question






















  • You could always use the Cache to store it there. When someone reload then in the init process you can set the Inventory back. Dependency Injection will not help you because every new object which will be injected will not contain the old data you want.
    – Σωτήρης Ραφαήλ
    Nov 8 at 11:29















up vote
0
down vote

favorite












I'm trying to open someone's Inventory in a different class than were I created the Inventory. I don't want to use statics so I tried a workaround with Dependency Injections. I made an ArrayList inventories, after that I add the created Inventory to the ArrayList. Here's the problem though, on reload the ArrayList gets wiped (of course), would there be a solution to still keep the Inventory in life without having it to be written down in a config or database. Because now players won't be able to open this created Inventory anymore after a reload, I really wonder what other plugins use to counter this problem (if they don't use statics of course ;)).



Here's some code I use:



private final Inventory inv;
private ArrayList<Inventory> inventories;
private KitPvP kitPvP;
private String name;
private Material material;

public KitManager(KitPvP kitPvP, String name, Material material, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.name = ChatColor.translateAlternateColorCodes('&', name);
this.material = material;
this.inventories = inventories;

this.inv = Bukkit.createInventory(null, 9, this.name);
}

public void createKit(Player p) {
ItemStack itemStack = new ItemStack(this.material);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(this.name);
itemStack.setItemMeta(itemMeta);
p.getInventory().addItem(itemStack);

this.inventories.add(this.inv);

p.openInventory(this.inv);
}


Event class



    private KitPvP kitPvP;
private ArrayList<Inventory> inventories;

public InteractListener(KitPvP kitPvP, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.inventories = inventories;
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();

p.sendMessage(this.inventories.toString());
}









share|improve this question






















  • You could always use the Cache to store it there. When someone reload then in the init process you can set the Inventory back. Dependency Injection will not help you because every new object which will be injected will not contain the old data you want.
    – Σωτήρης Ραφαήλ
    Nov 8 at 11:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to open someone's Inventory in a different class than were I created the Inventory. I don't want to use statics so I tried a workaround with Dependency Injections. I made an ArrayList inventories, after that I add the created Inventory to the ArrayList. Here's the problem though, on reload the ArrayList gets wiped (of course), would there be a solution to still keep the Inventory in life without having it to be written down in a config or database. Because now players won't be able to open this created Inventory anymore after a reload, I really wonder what other plugins use to counter this problem (if they don't use statics of course ;)).



Here's some code I use:



private final Inventory inv;
private ArrayList<Inventory> inventories;
private KitPvP kitPvP;
private String name;
private Material material;

public KitManager(KitPvP kitPvP, String name, Material material, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.name = ChatColor.translateAlternateColorCodes('&', name);
this.material = material;
this.inventories = inventories;

this.inv = Bukkit.createInventory(null, 9, this.name);
}

public void createKit(Player p) {
ItemStack itemStack = new ItemStack(this.material);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(this.name);
itemStack.setItemMeta(itemMeta);
p.getInventory().addItem(itemStack);

this.inventories.add(this.inv);

p.openInventory(this.inv);
}


Event class



    private KitPvP kitPvP;
private ArrayList<Inventory> inventories;

public InteractListener(KitPvP kitPvP, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.inventories = inventories;
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();

p.sendMessage(this.inventories.toString());
}









share|improve this question













I'm trying to open someone's Inventory in a different class than were I created the Inventory. I don't want to use statics so I tried a workaround with Dependency Injections. I made an ArrayList inventories, after that I add the created Inventory to the ArrayList. Here's the problem though, on reload the ArrayList gets wiped (of course), would there be a solution to still keep the Inventory in life without having it to be written down in a config or database. Because now players won't be able to open this created Inventory anymore after a reload, I really wonder what other plugins use to counter this problem (if they don't use statics of course ;)).



Here's some code I use:



private final Inventory inv;
private ArrayList<Inventory> inventories;
private KitPvP kitPvP;
private String name;
private Material material;

public KitManager(KitPvP kitPvP, String name, Material material, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.name = ChatColor.translateAlternateColorCodes('&', name);
this.material = material;
this.inventories = inventories;

this.inv = Bukkit.createInventory(null, 9, this.name);
}

public void createKit(Player p) {
ItemStack itemStack = new ItemStack(this.material);
ItemMeta itemMeta = itemStack.getItemMeta();
itemMeta.setDisplayName(this.name);
itemStack.setItemMeta(itemMeta);
p.getInventory().addItem(itemStack);

this.inventories.add(this.inv);

p.openInventory(this.inv);
}


Event class



    private KitPvP kitPvP;
private ArrayList<Inventory> inventories;

public InteractListener(KitPvP kitPvP, ArrayList<Inventory> inventories) {
this.kitPvP = kitPvP;
this.inventories = inventories;
}

@EventHandler
public void onPlayerInteract(PlayerInteractEvent e) {
Player p = e.getPlayer();

p.sendMessage(this.inventories.toString());
}






java dependency-injection bukkit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 10:59









Ezrab_

207




207












  • You could always use the Cache to store it there. When someone reload then in the init process you can set the Inventory back. Dependency Injection will not help you because every new object which will be injected will not contain the old data you want.
    – Σωτήρης Ραφαήλ
    Nov 8 at 11:29


















  • You could always use the Cache to store it there. When someone reload then in the init process you can set the Inventory back. Dependency Injection will not help you because every new object which will be injected will not contain the old data you want.
    – Σωτήρης Ραφαήλ
    Nov 8 at 11:29
















You could always use the Cache to store it there. When someone reload then in the init process you can set the Inventory back. Dependency Injection will not help you because every new object which will be injected will not contain the old data you want.
– Σωτήρης Ραφαήλ
Nov 8 at 11:29




You could always use the Cache to store it there. When someone reload then in the init process you can set the Inventory back. Dependency Injection will not help you because every new object which will be injected will not contain the old data you want.
– Σωτήρης Ραφαήλ
Nov 8 at 11:29

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206366%2fsaving-inventory-with-dependency-injection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53206366%2fsaving-inventory-with-dependency-injection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Schultheiß

Verwaltungsgliederung Dänemarks

Liste der Kulturdenkmale in Wilsdruff