Skip to content

Commit

Permalink
Merge pull request #36 from Th3Shadowbroker/feature/drops
Browse files Browse the repository at this point in the history
Feature/drops
  • Loading branch information
Th3Shadowbroker authored Sep 19, 2020
2 parents 471e3e6 + 45a6a15 commit 787e776
Show file tree
Hide file tree
Showing 15 changed files with 539 additions and 25 deletions.
2 changes: 0 additions & 2 deletions TODO.md

This file was deleted.

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>dev.th3shadowbroker.spigot</groupId>
<artifactId>OuroborosMines</artifactId>
<version>1.7.1-SNAPSHOT</version>
<version>1.8.0-SNAPSHOT</version>

<properties>
<plugin.name>${project.artifactId}</plugin.name>
Expand Down Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.15.1-R0.1-SNAPSHOT</version>
<version>1.16.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.flags.StateFlag;
import dev.th3shadowbroker.ouroboros.mines.commands.OmCommand;
import dev.th3shadowbroker.ouroboros.mines.drops.DropManager;
import dev.th3shadowbroker.ouroboros.mines.exceptions.InvalidMineMaterialException;
import dev.th3shadowbroker.ouroboros.mines.listeners.BlockBreakListener;
import dev.th3shadowbroker.ouroboros.mines.listeners.DepositDiscoveryListener;
import dev.th3shadowbroker.ouroboros.mines.listeners.ExperienceListener;
import dev.th3shadowbroker.ouroboros.mines.listeners.TimeSkipListener;
import dev.th3shadowbroker.ouroboros.mines.listeners.*;
import dev.th3shadowbroker.ouroboros.mines.thirdparty.JobsRebornSupport;
import dev.th3shadowbroker.ouroboros.mines.thirdparty.PlaceholderAPISupport;
import dev.th3shadowbroker.ouroboros.mines.thirdparty.QuestsSupport;
Expand All @@ -42,6 +40,7 @@
import org.th3shadowbroker.ouroboros.update.comparison.Comparator;
import org.th3shadowbroker.ouroboros.update.spiget.SpigetUpdater;

import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
Expand All @@ -64,6 +63,8 @@ public class OuroborosMines extends JavaPlugin {

private AnnouncementManager announcementManager;

private DropManager dropManager;

private boolean worldGuardFound = false;

@Override
Expand Down Expand Up @@ -105,10 +106,13 @@ public void onEnable() {
getServer().getPluginManager().registerEvents( new DepositDiscoveryListener(), this );
getServer().getPluginManager().registerEvents( new ExperienceListener(), this );
getServer().getPluginManager().registerEvents( new TimeSkipListener(), this );
getServer().getPluginManager().registerEvents( new PlayerInteractListener(), this );

announcementManager = new AnnouncementManager();
announcementManager.createTasks();

dropManager = new DropManager(new File(getDataFolder(), "drops.yml"));

getCommand("om").setExecutor(new OmCommand());

checkForSupportedPlugins();
Expand Down Expand Up @@ -211,7 +215,15 @@ private void updateConfig() {
"autoPickup",
"openingHours",
"timezone",
"placeholders"
"placeholders",
"chat.messages.reloadingDropGroups",
"chat.messages.reloadedDropGroups",
"chat.messages.awaitingRightClick",
"chat.messages.awaitingRightClickCancelled",
"chat.messages.dropGroupCreated",
"chat.messages.dropGroupExists",
"chat.messages.missingDropGroupName",
"chat.messages.consoleNotAllowed"
);
pathsToCopy.forEach(path -> {
if (!getConfig().isSet(path)) {
Expand Down Expand Up @@ -271,5 +283,9 @@ public TaskManager getTaskManager() {
public AnnouncementManager getAnnouncementManager() {
return announcementManager;
}


public DropManager getDropManager() {
return dropManager;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import dev.th3shadowbroker.ouroboros.mines.OuroborosMines;
import dev.th3shadowbroker.ouroboros.mines.drops.DropGroupCreator;
import dev.th3shadowbroker.ouroboros.mines.util.Permissions;
import dev.th3shadowbroker.ouroboros.mines.util.RegionConfiguration;
import dev.th3shadowbroker.ouroboros.mines.util.TemplateMessage;
Expand All @@ -37,7 +38,7 @@

public class OmCommand implements CommandExecutor {

private final String consoleNotAllowed = TemplateMessage.from("chat.messages.consoleOnly").colorize().toString();
private final String consoleNotAllowed = TemplateMessage.from("chat.messages.consoleNotAllowed").colorize().toString();

private final OuroborosMines plugin = OuroborosMines.INSTANCE;

Expand Down Expand Up @@ -95,6 +96,9 @@ public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, Str
plugin.getMaterialManager().reloadRegionConfigurations();
sender.sendMessage(TemplateMessage.from("chat.messages.reloadedRegionConfigurations").insert("count", String.valueOf(plugin.getMaterialManager().getMineableMaterialOverrides().size())).colorize().toString());
plugin.getAnnouncementManager().flush();
sender.sendMessage(TemplateMessage.from("chat.messages.reloadingDropGroups").colorize().toString());
plugin.getDropManager().reloadGroups();
sender.sendMessage(TemplateMessage.from("chat.messages.reloadedDropGroups").insert("count", String.valueOf(plugin.getDropManager().getGroups().length)).colorize().toString());
} catch (Exception ex) {
sender.sendMessage(TemplateMessage.from("chat.messages.error").colorize().insert("error", ex.getMessage()).toString());
}
Expand All @@ -103,6 +107,38 @@ public boolean onCommand(CommandSender sender, Command cmd, String cmdLabel, Str
}
break;

case "dropgroup":
case "dg":
if (sender.hasPermission(Permissions.COMMAND_DROP_GROUP.permission)) {
if (sender instanceof Player) {
Player player = (Player) sender;

if (args.length >= 2) {
String dropGroupName = args[1];

// Drop group does not exist
if (!OuroborosMines.INSTANCE.getDropManager().getDropGroup(dropGroupName).isPresent()) {
DropGroupCreator.awaitCreation(player, dropGroupName);
player.sendMessage(TemplateMessage.from("chat.messages.awaitingRightClick").colorize().toString());
} else {
player.sendMessage(TemplateMessage.from("chat.messages.dropGroupExists").insert("name", dropGroupName).colorize().toString());
}
} else {
if (DropGroupCreator.creationPending(player)) {
DropGroupCreator.clearCreationStatus(player);
sender.sendMessage(TemplateMessage.from("chat.messages.awaitingRightClickCancelled").colorize().toString());
} else {
sender.sendMessage(TemplateMessage.from("chat.messages.missingDropGroupName").colorize().toString());
}
}
} else {
sender.sendMessage(consoleNotAllowed);
}
} else {
sender.sendMessage(cmd.getPermissionMessage());
}
break;

default:
sender.sendMessage(TemplateMessage.from("chat.messages.unrecognizedArgument").colorize().toString());
sender.sendMessage(cmd.getUsage());
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/dev/th3shadowbroker/ouroboros/mines/drops/Drop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2020 Jens Fischer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.th3shadowbroker.ouroboros.mines.drops;

import dev.th3shadowbroker.ouroboros.mines.util.Range;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;

public class Drop {

private final ItemStack itemStack;

private final double dropChance;

private final Range dropAmount;

public Drop(ItemStack itemStack, double dropChance, Range dropAmount) {
this.itemStack = itemStack;
this.dropChance = dropChance;
this.dropAmount = dropAmount;
}

public ItemStack getItemStack() {
return itemStack;
}

public double getDropChance() {
return dropChance;
}

public Range getDropAmount() {
return dropAmount;
}

public ItemStack drawDropstack() {
ItemStack dropStack = itemStack.clone();
dropStack.setAmount(dropAmount.isRange() ? dropAmount.getRandomWithin() : dropAmount.getMin());
return dropStack;
}

public static Drop fromSection(ConfigurationSection section) {
ItemStack itemStack = section.getItemStack("item");
double dropChance = section.getDouble("chance", 1);
Range dropAmount = Range.fromString(section.getString("amount", "1"));
return new Drop(itemStack, dropChance, dropAmount);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2020 Jens Fischer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of
* the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package dev.th3shadowbroker.ouroboros.mines.drops;

import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class DropGroup {

private final List<Drop> drops;

private final boolean multidrop;

public DropGroup(List<Drop> drops, boolean multidrop) {
this.drops = drops;
this.multidrop = multidrop;
}

public boolean isValid() {
return multidrop || drops.stream().mapToDouble(Drop::getDropChance).sum() <= 1;
}

public List<Drop> getDrops() {
return drops;
}

public boolean isMultidrop() {
return multidrop;
}

private ItemStack[] drawMultidrop() {
final List<ItemStack> drops = new ArrayList<>();
this.drops.forEach(
drop -> {

// Drop chance 100 or higher
if (drop.getDropChance() >= 1) {
drops.add(drop.drawDropstack());

// Drop chance below 100
} else {
Random rnd = new Random();
boolean shallDrop = ((double) rnd.nextInt(100) / 100) <= drop.getDropChance();
if (shallDrop) {
drops.add(drop.drawDropstack());
}
}

}
);
return drops.stream().toArray(ItemStack[]::new);
}

private ItemStack[] drawSingledrop() {
ItemStack dropStack = null;
Random rnd = new Random();

double drawnChance = ((double) rnd.nextInt(100) / 100);
double offset = 0;

for (Drop drop : drops) {
boolean shallDrop = drawnChance <= drop.getDropChance() + offset;

if (shallDrop) {
dropStack = drop.drawDropstack();
break;
} else {
offset += drop.getDropChance();
}
}

return dropStack == null ? new ItemStack[0] : new ItemStack[]{ dropStack };
}

public ItemStack[] drawDrops() {
return multidrop ? drawMultidrop() : drawSingledrop();
}

}
Loading

0 comments on commit 787e776

Please sign in to comment.