Skip to content

Commit

Permalink
Implement Generator Bundles.
Browse files Browse the repository at this point in the history
This allows to group several generators together and limit users to use only them. It could be done via permissions.
  • Loading branch information
BONNe committed Oct 6, 2020
1 parent 1982e49 commit 5304b31
Show file tree
Hide file tree
Showing 10 changed files with 535 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ public Optional<List<String>> tabComplete(User user, String alias, List<String>

final List<String> returnList = new ArrayList<>();

// Create suggestions with all biomes that is available for users.
// Create suggestions with all generators that is available for users.

this.<StoneGeneratorAddon>getAddon().getAddonManager().getAllGeneratorTiers(this.getWorld()).
this.<StoneGeneratorAddon>getAddon().getAddonManager().getIslandGeneratorTiers(this.getWorld(), user).
forEach(generatorTier -> returnList.add(generatorTier.getUniqueId()));

return Optional.of(Util.tabLimit(returnList, args.get(args.size() - 1)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
//
// Created by BONNe
// Copyright - 2020
//


package world.bentobox.magiccobblestonegenerator.database.objects;


import com.google.gson.annotations.Expose;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.*;

import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;


/**
* The type Generator bundle object.
*/
@Table(name = "GeneratorBundle")
public class GeneratorBundleObject implements DataObject
{
/**
* Instantiates a new Generator bundle object.
*/
public GeneratorBundleObject()
{
}


// ---------------------------------------------------------------------
// Section: Getters and Setters
// ---------------------------------------------------------------------


/**
* Gets uniqueId
*
* @return the unique id
*/
@Override
public String getUniqueId()
{
return uniqueId;
}


/**
* Sets the uniqueId
* @param uniqueId the uniqueId
*/
@Override
public void setUniqueId(String uniqueId)
{
this.uniqueId = uniqueId;
}


/**
* Gets friendly name.
*
* @return the friendly name
*/
public String getFriendlyName()
{
return friendlyName;
}


/**
* Sets friendly name.
*
* @param friendlyName the friendly name
*/
public void setFriendlyName(String friendlyName)
{
this.friendlyName = friendlyName;
}


/**
* Gets description.
*
* @return the description
*/
public List<String> getDescription()
{
return description;
}


/**
* Sets description.
*
* @param description the description
*/
public void setDescription(List<String> description)
{
this.description = description;
}


/**
* Gets generator icon.
*
* @return the generator icon
*/
public ItemStack getGeneratorIcon()
{
return generatorIcon;
}


/**
* Sets generator icon.
*
* @param generatorIcon the generator icon
*/
public void setGeneratorIcon(ItemStack generatorIcon)
{
this.generatorIcon = generatorIcon;
}


/**
* Gets generator tiers.
*
* @return the generator tiers
*/
public Set<String> getGeneratorTiers()
{
return generatorTiers;
}


/**
* Sets generator tiers.
*
* @param generatorTiers the generator tiers
*/
public void setGeneratorTiers(Set<String> generatorTiers)
{
this.generatorTiers = generatorTiers;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------


/**
* The Unique id.
*/
@Expose
private String uniqueId;

/**
* The Friendly name.
*/
@Expose
private String friendlyName;

/**
* The Description.
*/
@Expose
private List<String> description = Collections.emptyList();

/**
* The Generator icon.
*/
@Expose
private ItemStack generatorIcon = new ItemStack(Material.STONE);

/**
* The Generator tiers.
*/
@Expose
private Set<String> generatorTiers = new HashSet<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import com.google.gson.annotations.Expose;

import org.jetbrains.annotations.Nullable;
import java.util.HashSet;
import java.util.Set;

Expand Down Expand Up @@ -184,6 +185,50 @@ public int getWorkingRange()
}


/**
* Gets owner bundle.
*
* @return the owner bundle
*/
public @Nullable String getOwnerBundle()
{
return ownerBundle;
}


/**
* Sets owner bundle.
*
* @param ownerBundle the owner bundle
*/
public void setOwnerBundle(String ownerBundle)
{
this.ownerBundle = ownerBundle;
}


/**
* Gets island bundle.
*
* @return the island bundle
*/
public @Nullable String getIslandBundle()
{
return islandBundle;
}


/**
* Sets island bundle.
*
* @param islandBundle the island bundle
*/
public void setIslandBundle(String islandBundle)
{
this.islandBundle = islandBundle;
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -230,4 +275,16 @@ public int getWorkingRange()
*/
@Expose
private int workingRange;

/**
* Stores active bundle.
*/
@Expose
private @Nullable String ownerBundle;

/**
* Stores active bundle.
*/
@Expose
private @Nullable String islandBundle;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.util.ItemParser;
import world.bentobox.magiccobblestonegenerator.StoneGeneratorAddon;
import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorBundleObject;
import world.bentobox.magiccobblestonegenerator.database.objects.GeneratorTierObject;
import world.bentobox.magiccobblestonegenerator.utils.Constants;
import world.bentobox.magiccobblestonegenerator.utils.Utils;
Expand Down Expand Up @@ -53,7 +54,7 @@ public StoneGeneratorImportManager(StoneGeneratorAddon addon)
}

// ---------------------------------------------------------------------
// Section: Methods
// Section: Template Methods
// ---------------------------------------------------------------------


Expand Down Expand Up @@ -130,9 +131,9 @@ public boolean importFile(@Nullable User user, World world)
*/
private void createGenerators(YamlConfiguration config, @Nullable User user, GameModeAddon gameMode)
{
final String name = gameMode.getDescription().getName().toLowerCase();
final String prefix = gameMode.getDescription().getName().toLowerCase() + "_";

int size = 0;
int generatorSize = 0;

ConfigurationSection reader = config.getConfigurationSection("tiers");

Expand All @@ -143,14 +144,14 @@ private void createGenerators(YamlConfiguration config, @Nullable User user, Gam
for (String generatorId : reader.getKeys(false))
{
GeneratorTierObject generatorTier = new GeneratorTierObject();
generatorTier.setUniqueId(name + "-" + generatorId.toLowerCase());
generatorTier.setUniqueId(prefix + generatorId.toLowerCase());

ConfigurationSection details = reader.getConfigurationSection(generatorId);

if (details != null)
{
// Read name
generatorTier.setFriendlyName(details.getString("name",
// Read prefix
generatorTier.setFriendlyName(details.getString("prefix",
generatorId.replaceAll("_", " ")));
// Read description
generatorTier.setDescription(details.getStringList("description"));
Expand Down Expand Up @@ -195,19 +196,54 @@ private void createGenerators(YamlConfiguration config, @Nullable User user, Gam
// Save object in database.
this.addon.getAddonManager().saveGeneratorTier(generatorTier);
this.addon.getAddonManager().loadGeneratorTier(generatorTier, false, null, true);
size++;
generatorSize++;
}

reader = config.getConfigurationSection("bundles");
int bundleSize = 0;

for (String bundleId : reader.getKeys(false))
{
GeneratorBundleObject generatorBundle = new GeneratorBundleObject();
generatorBundle.setUniqueId(prefix + bundleId.toLowerCase());

ConfigurationSection details = reader.getConfigurationSection(bundleId);

if (details != null)
{
// Read prefix
generatorBundle.setFriendlyName(details.getString("prefix",
bundleId.replaceAll("_", " ")));
// Read description
generatorBundle.setDescription(details.getStringList("description"));
// Read icon
ItemStack icon = ItemParser.parse(details.getString("icon"));
generatorBundle.setGeneratorIcon(icon == null ? new ItemStack(Material.PAPER) : icon);
// Read generators
generatorBundle.setGeneratorTiers(
details.getStringList("generators").stream().
map(id -> prefix + id).
collect(Collectors.toSet()));
}

// Save object in database.
this.addon.getAddonManager().saveGeneratorBundle(generatorBundle);
this.addon.getAddonManager().loadGeneratorBundle(generatorBundle, false, null, true);
bundleSize++;
}

if (user != null)
{
user.sendMessage(Constants.MESSAGE + "import-count",
TextVariables.NUMBER,
String.valueOf(size));
}
else
{
this.addon.log("Imported " + size + " generator tiers into database.");
String.valueOf(generatorSize));
user.sendMessage(Constants.MESSAGE + "import-bundle-count",
TextVariables.NUMBER,
String.valueOf(bundleSize));
}

this.addon.log("Imported " + generatorSize + " generator tiers and " +
bundleSize + " bundles into database.");
}


Expand Down

0 comments on commit 5304b31

Please sign in to comment.