I have a class structure:
class BonusCard{
}
class AddResourceCard extends BonusCard{
}
class AddGoldCard extends BonusCard{
}
Now, I also have a function in which I want to pass AddResourceCard or AddGoldCard and in someones Inventory I want to check if an object in that Inventory is an instance of the class that I put into the function.
private void removeCardFromPlayer(Player player, BonusCard cardToRemove){
for(BonusCard card : player.getInventory().getBonusCards()){
if(card instanceof cardToRemove.getClass()){ //this line doesn't work sadly
player.getInventory().getBonusCards().remove(card);
break;
}
}
}
A function call should look like this:
removeCardFromPlayer(player, AddResourceCard);
or
removeCardFromPlayer(player, AddGoldCard);
There should be an easy straightforward way to accomplish what I'm trying to do, I just don't really know what to search for to be completely honest.
private void removeCardFromPlayer(Player player, Class<?>cardToRemoveClass)
or don't use instanceof but rathercard.getClass().equals(cardToRemove.getClass())