I'm missing something with instances

Ok, so i have list of Shop Objects, and i want to make it so that list can be accessed from any other class in the plugin so i pass it into the constructor of a class but the problem is that in the other classes the list is empty while in the main plugin class it’s full. (Warning: Messy code where i tried figuring out what was wrong for a good 4 hours and now i’m here)

My Main Class:

This is the main plugin class

package site.drunkripper.drunkshops;

import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.world.World;

import com.arckenver.nations.object.Point;
import com.arckenver.nations.object.Rect;
import com.arckenver.nations.object.Zone;
import com.google.inject.Inject;

import site.drunkripper.drunkshops.objects.ShopObject;
import site.drunkripper.drunkshops.objects.ShopObjectStorage;

@Plugin(id="drunkshops", name="Drunk Shops", version="1.0-SNAPSHOT")
public class DrunkShops {
	
	@Inject private Logger logger;
	
	ShopObjectStorage shopObjStorage = new ShopObjectStorage();
	
	public Logger getLogger() {
		return this.logger;
	}

	CommandSpec createShop = CommandSpec.builder()
			.description(LANGUAGEHANDLER.COMMAND_DESC_CREATE)
			.permission("drunkshops.command.create")
			.arguments(GenericArguments.string(Text.of("Name")))
			.arguments(GenericArguments.string(Text.of("Price")))
			.executor(new DrunkShopsCommandCreate(this))
			.build();
	CommandSpec listShops = CommandSpec.builder()
			.description(LANGUAGEHANDLER.COMMAND_DESC_LIST)
			.permission("")
			.executor(new DrunkShopsCommandList(this, shopObjStorage))
			.build();
	CommandSpec drunkShops = CommandSpec.builder()
			.description(LANGUAGEHANDLER.COMMAND_DESC_MAIN)
			.permission("drunkshops.command")
			.executor(new DrunkShopsCommand(this))
			.child(createShop, "create")
			.child(listShops, "list")
			.build();
	
	@Listener public void checkAndInit (GameInitializationEvent event) {
		if(!Sponge.getPluginManager().isLoaded("nations")) {
			getLogger().info(LANGUAGEHANDLER.NATIONS_NOT_FOUND.toPlain());
		} else {
			getLogger().info(LANGUAGEHANDLER.NATIONS_FOUND.toPlain());
		}
		Sponge.getCommandManager().register(this, drunkShops, "drunkshops", "DS", "shops");
	}
		
	@Listener public void onPlayerJoin(ClientConnectionEvent.Join conn) {
		World world = Sponge.getServer().getWorld(Sponge.getServer().getDefaultWorldName()).get();
		Point a = new Point(world, 0, 0);
		Point b = new Point(world, 10, 10);
		Rect rect1 = new Rect(a, b);
		Zone zone1 = new Zone(conn.getTargetEntity().getPlayer().get().getUniqueId(), "zone1" ,rect1);
		
		Point a1 = new Point(world, 10, 10);
		Point b1 = new Point(world, 20, 20);
		Rect rect2 = new Rect(a1, b1);
		Zone zone2 = new Zone(conn.getTargetEntity().getPlayer().get().getUniqueId(), "zone1" ,rect2);
				
		
		ShopObject shop1 = new ShopObject(zone1, conn.getTargetEntity().getPlayer().get().getUniqueId(),(double) 50);
		shopObjStorage.addShops(shop1);
		ShopObject shop2 = new ShopObject(zone2, conn.getTargetEntity().getPlayer().get().getUniqueId(),(double) 50);
		shopObjStorage.addShops(shop2);
		conn.getTargetEntity().getPlayer().get().sendMessage(Text.of("Amount of shops: " + String.valueOf(shopObjStorage.getShops().size())));
		for(ShopObject shop : shopObjStorage.getShops()) {
			Rect rect = shop.getZone().getRect();
			
			String blank = "x1: %X1%, z1: %Z1%, x2: %X2%, z2: %Z2%, Owner UUID: %UUID%, Price: %PRICE%";
			blank = blank.replace("%X1%", String.valueOf(rect.getMinX()));
			blank = blank.replace("%Z1%", String.valueOf(rect.getMinY()));
			blank = blank.replace("%X2%", String.valueOf(rect.getMaxX()));
			blank = blank.replace("%Z2%", String.valueOf(rect.getMaxY()));
			blank = blank.replace("%UUID%", shop.getOwner().toString());
			blank = blank.replace("%PRICE%", String.valueOf(shop.getPrice()));
			conn.getTargetEntity().getPlayer().get().sendMessage(Text.of(TextColors.BLACK, blank, Text.NEW_LINE));
		}

		conn.getTargetEntity().getPlayer().get().sendMessage(LANGUAGEHANDLER.LOGO);	
		for(ShopObject shop: shopObjStorage.getShops()) {
			if(conn.getTargetEntity().getPlayer().get().getUniqueId().equals(shop.getOwner())) {
				conn.getTargetEntity().getPlayer().get().sendMessage(Text.of(TextColors.AQUA, LANGUAGEHANDLER.OWN_SHOP));
				return;
			}
		}
	}

My DrunkShopsCommandList:

package site.drunkripper.drunkshops;

import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import com.arckenver.nations.object.Rect;

import site.drunkripper.drunkshops.objects.ShopObject;
import site.drunkripper.drunkshops.objects.ShopObjectStorage;

public class DrunkShopsCommandList implements CommandExecutor {

	private DrunkShops plugin;
	private ShopObjectStorage shopObjStorage;	
	
	public DrunkShopsCommandList(DrunkShops drunkShops, ShopObjectStorage shopObjStorage) {
		this.plugin = plugin;
		this.shopObjStorage = shopObjStorage;
	}

	@Override
	public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
		src.sendMessage(Text.of(TextColors.AQUA, "Here is a list of all shops on the server that are registered.", Text.NEW_LINE));
		plugin.getLogger().info("Amount of shops: " + String.valueOf(shopObjStorage.getShops().size()));
		for(ShopObject shop : shopObjStorage.getShops()) {
			Rect rect = shop.getZone().getRect();
			
			String blank = "x1: %X1%, z1: %Z1%, x2: %X2%, z2: %Z2%, Owner UUID: %UUID%, Price: %PRICE%";
			blank = blank.replace("%X1%", String.valueOf(rect.getMinX()));
			blank = blank.replace("%Z1%", String.valueOf(rect.getMinY()));
			blank = blank.replace("%X2%", String.valueOf(rect.getMaxX()));
			blank = blank.replace("%Z2%", String.valueOf(rect.getMaxY()));
			blank = blank.replace("%UUID%", shop.getOwner().toString());
			blank = blank.replace("%PRICE%", String.valueOf(shop.getPrice()));
			src.sendMessage(Text.of(TextColors.BLACK, blank, Text.NEW_LINE));
		}
		return CommandResult.success();
	}

}

Ok 2 things.

Could we see the code for ShopObjectStorage?

Im also curious on why you are bringing the plugin object and the storage object with you? Why don’t you have it in one place and reference it there?

I’ve left the house so I’ll get it to you later but ShopObjectStorage just has a List and a getter and setter that consists of this.shops.add and then get returns this.shops. also I tried storing it in the main plugin class file to and it still wouldn’t work.

Why dont you just use the shops as a parameter instead of a class?

CommandSpec listShops = CommandSpec.builder()
		.description(LANGUAGEHANDLER.COMMAND_DESC_LIST)
		.permission("")
		.executor(new DrunkShopsCommandList(this, shopObjStorage.**getShops()**))
		.build();

Would be good if you upload every class.

public class Main {
public static void main(String args[]) {
	
	ShopList list = new ShopList();
	
	list.setMap("Test", 0);
	System.out.println(list.getMap());
	
	ShopGet get = new ShopGet(list);
	get.getList();
}
}

public class ShopList {

private HashMap<String, Integer> map = new HashMap<String, Integer>();

public HashMap<String, Integer> getMap() {
	return map;
}

public void setMap(String k, Integer v) {
	map.put(k, v);
}
}

public class ShopGet {

private ShopList list;
public ShopGet(ShopList list) {
	this.list = list;
	System.out.println(list.getMap());
}

public void getList() {
	System.out.println(list.getMap());
	ShopList list2 = new ShopList();
	
	System.out.println(list2.getMap());
}
}

The output. Obviously the last one cant work.

{Test=0}
{Test=0}
{Test=0}
{}

Also where do you save it?
Simply putting chunks/positions/coordinates in a list wont work without saving it.

Passing as a parameter in that class won’t work as the list of shops will (eventually) grow on after the server start and that class would get an empty list as i don’t add to it til after the commands are built.

DrunkShops.getShopObjectStorage().getShops() ?

Could you post all your code then?

Ill try the same later and see if i can get it to work.

As posted I don’t see anything wrong?

Are you sure you are not re-initializing or clearing that list anywhere?
I’d suggest making it final unless you want to replace the reference as part of a reload support.

I’m sure i don’t because i never even add to it besides on player join as of right now. Anyway, I’ve decided i’m gonna start over because the code was so messy anymore it’s not worth prettying and fixing but if we figure out why it wouldn’t work, I’ll mark it as solved, til then we leave it open so people can figure out if they have the same issue and maybe compare what we did that might have broken it.