Skip to content

Commit

Permalink
Merge pull request #38
Browse files Browse the repository at this point in the history
Implemented bulk crafting for ResultExtensions
  • Loading branch information
WolfyScript authored Dec 22, 2021
2 parents 2099189 + 4836729 commit 30ac101
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import me.wolfyscript.customcrafting.recipes.CraftingRecipeShapeless;
import me.wolfyscript.customcrafting.recipes.items.Ingredient;
import me.wolfyscript.customcrafting.recipes.items.Result;
import me.wolfyscript.customcrafting.recipes.items.extension.CommandResultExtension;
import me.wolfyscript.customcrafting.recipes.items.extension.MythicMobResultExtension;
import me.wolfyscript.customcrafting.recipes.items.extension.ResultExtensionAdvancement;
import me.wolfyscript.customcrafting.recipes.items.extension.SoundResultExtension;
import me.wolfyscript.customcrafting.recipes.items.extension.*;
import me.wolfyscript.customcrafting.utils.ItemLoader;
import me.wolfyscript.customcrafting.utils.NamespacedKeyUtils;
import me.wolfyscript.utilities.api.WolfyUtilities;
Expand Down Expand Up @@ -143,7 +140,9 @@ public void loadDefaults() {
Result result = workbenchCraft.getResult();
result.put(0, CustomItem.with(new WolfyUtilitiesRef(CustomCrafting.INTERNAL_ADVANCED_CRAFTING_TABLE)));
if (WolfyUtilities.isDevEnv()) {
result.addExtension(new CommandResultExtension(Arrays.asList("say hi %player%", "effect give %player% minecraft:strength 100 100"), new ArrayList<>(), true, true));
var commandExecution = new CommandResultExtension(Arrays.asList("say hi %player%", "effect give %player% minecraft:strength 100 100"), new ArrayList<>(), true, true);
commandExecution.setExecutionType(ExecutionType.BULK);
result.addExtension(commandExecution);
result.addExtension(new SoundResultExtension(Sound.BLOCK_ANVIL_USE));
result.addExtension(new MythicMobResultExtension("SkeletalKnight", 1));
result.addExtension(new ResultExtensionAdvancement(NamespacedKey.minecraft("husbandry/tactical_fishing"), false, null, false, false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import me.wolfyscript.customcrafting.CustomCrafting;
import me.wolfyscript.customcrafting.recipes.data.RecipeData;
import me.wolfyscript.customcrafting.recipes.items.extension.ExecutionType;
import me.wolfyscript.customcrafting.recipes.items.extension.ResultExtension;
import me.wolfyscript.customcrafting.recipes.items.target.ResultTarget;
import me.wolfyscript.utilities.api.inventory.custom_items.CustomItem;
Expand All @@ -45,6 +46,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.stream.Collectors;

@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
Expand All @@ -56,6 +58,8 @@ public class Result extends RecipeItemStack {
private final Map<Vector, CustomItem> cachedBlockItems = new HashMap<>();
private ResultTarget target;
private List<ResultExtension> extensions;
@JsonIgnore
private List<ResultExtension> bulkExtensions = new ArrayList<>();

public Result() {
super();
Expand All @@ -64,7 +68,7 @@ public Result() {

public Result(Result result) {
super(result);
this.extensions = result.extensions;
setExtensions(result.extensions);
this.target = result.target;
}

Expand Down Expand Up @@ -114,22 +118,27 @@ public List<ResultExtension> getExtensions() {
@JsonProperty("extensions")
private void setExtensions(List<ResultExtension> extensions) {
this.extensions = extensions;
this.bulkExtensions = this.extensions.stream().filter(resultExtension -> resultExtension.getExecutionType().equals(ExecutionType.BULK)).collect(Collectors.toList());
}

public void addExtension(ResultExtension extension) {
this.extensions.add(extension);
if (extension.getExecutionType().equals(ExecutionType.BULK)) {
this.bulkExtensions.add(extension);
}
}

public void removeExtension(ResultExtension extension) {
this.extensions.remove(extension);
this.bulkExtensions.remove(extension);
}

public void removeExtension(int index) {
this.extensions.remove(index);
removeExtension(this.extensions.get(index));
}

public RandomCollection<CustomItem> getRandomChoices(@Nullable Player player) {
return (player == null ? getChoices() : getChoices(player)).stream().collect(RandomCollection.getCollector((rdmCollection, customItem) -> rdmCollection.add(customItem.getRarityPercentage(), customItem)));
return (player == null ? getChoices() : getChoices(player)).stream().collect(RandomCollection.getCollector((rdmCollection, customItem) -> rdmCollection.add(customItem.getWeight(), customItem)));
}

/**
Expand Down Expand Up @@ -216,6 +225,20 @@ public void removeCachedItem(Block block) {
}

public void executeExtensions(@NotNull Location location, boolean isWorkstation, @Nullable Player player) {
executeExtensions(location, isWorkstation, player, 1);
}

public void executeExtensions(@NotNull Location location, boolean isWorkstation, @Nullable Player player, int amountOfExecutions) {
executeExtensions(this.extensions, location, isWorkstation, player);
if (amountOfExecutions > 1) {
for (int i = 0; i < amountOfExecutions - 1; i++) {
executeExtensions(this.bulkExtensions, location, isWorkstation, player);
}
}
}

private void executeExtensions(List<ResultExtension> extensions, @NotNull Location location, boolean isWorkstation, @Nullable Player player) {
Bukkit.getScheduler().runTaskLater(CustomCrafting.inst(), () -> extensions.forEach(resultExtension -> resultExtension.onCraft(location, isWorkstation, player)), 2);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
package me.wolfyscript.customcrafting.recipes.items.extension;

import me.clip.placeholderapi.PlaceholderAPI;
import me.wolfyscript.customcrafting.utils.NamespacedKeyUtils;
import me.wolfyscript.customcrafting.CustomCrafting;
import me.wolfyscript.utilities.api.WolfyUtilities;
import me.wolfyscript.utilities.util.NamespacedKey;
import org.bukkit.Bukkit;
Expand All @@ -44,7 +44,7 @@ public class CommandResultExtension extends ResultExtension {
private boolean nearWorkstation = false;

public CommandResultExtension() {
super(new NamespacedKey(NamespacedKeyUtils.NAMESPACE, "command"));
super(new NamespacedKey(CustomCrafting.inst(), "command"));
}

public CommandResultExtension(CommandResultExtension extension) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* ____ _ _ ____ ___ ____ _ _ ____ ____ ____ ____ ___ _ _ _ ____
* | | | [__ | | | |\/| | |__/ |__| |___ | | |\ | | __
* |___ |__| ___] | |__| | | |___ | \ | | | | | | \| |__]
*
* CustomCrafting Recipe creation and management tool for Minecraft
* Copyright (C) 2021 WolfyScript
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package me.wolfyscript.customcrafting.recipes.items.extension;

/**
* Defines the way a {@link ResultExtension} will be executed in recipes.
*/
public enum ExecutionType {

/**
* If possible the extension will be executed in bulk.<br>
* e.g. in crafting recipes.
*/
BULK,
/**
* The default value.<br>
* The extension will always be executed once.
*/
ONCE

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package me.wolfyscript.customcrafting.recipes.items.extension;

import me.wolfyscript.customcrafting.CustomCrafting;
import me.wolfyscript.customcrafting.utils.NamespacedKeyUtils;
import me.wolfyscript.utilities.api.WolfyUtilCore;
import me.wolfyscript.utilities.compatibility.plugins.MythicMobsIntegration;
Expand All @@ -42,7 +43,7 @@ public class MythicMobResultExtension extends ResultExtension {
private Vector offset = new Vector(0.5, 1, 0.5);

public MythicMobResultExtension() {
super(new NamespacedKey(NamespacedKeyUtils.NAMESPACE, "mythicmobs/mob_spawn"));
super(new NamespacedKey(CustomCrafting.inst(), "mythicmobs/mob_spawn"));
}

public MythicMobResultExtension(MythicMobResultExtension extension) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@
@JsonPropertyOrder(value = {"key", "outerRadius", "innerRadius"})
public abstract class ResultExtension implements Keyed {

protected @JsonIgnore
Material icon = Material.CHAIN_COMMAND_BLOCK;
protected @JsonIgnore
String title = "&6&lExtension";
protected @JsonIgnore
List<String> description;
@JsonIgnore
protected Material icon = Material.CHAIN_COMMAND_BLOCK;
@JsonIgnore
protected String title = "&6&lExtension";
@JsonIgnore
protected List<String> description;

@JsonProperty(value = "key")
@JsonInclude
private final NamespacedKey namespacedKey;
@JsonAlias(value = "outer_radius")
protected Vector outerRadius = new Vector(0, 0, 0);
@JsonAlias(value = "inner_radius")
protected Vector innerRadius = new Vector(0, 0, 0);
protected ExecutionType executionType = ExecutionType.ONCE;

protected ResultExtension(ResultExtension extension) {
this.namespacedKey = extension.namespacedKey;
Expand All @@ -71,6 +73,7 @@ protected ResultExtension(ResultExtension extension) {
this.description = extension.description;
this.outerRadius = extension.outerRadius;
this.innerRadius = extension.innerRadius;
this.executionType = extension.executionType;
}

protected ResultExtension(NamespacedKey namespacedKey) {
Expand Down Expand Up @@ -157,12 +160,20 @@ public void setOuterRadius(Vector outerRadius) {
this.outerRadius = outerRadius;
}

public ExecutionType getExecutionType() {
return executionType;
}

public void setExecutionType(ExecutionType executionType) {
this.executionType = executionType;
}

protected <E extends Entity> List<E> getEntitiesInRange(Class<E> entityType, Location location, Vector outerRadius, Vector innerRadius) {
var world = location.getWorld();
if (world != null) {
List<E> outerEntities = world.getNearbyEntities(location, outerRadius.getX(), outerRadius.getZ(), outerRadius.getZ(), entityType::isInstance).stream().map(entityType::cast).collect(Collectors.toList());
if (innerRadius.getX() != 0 || innerRadius.getY() != 0 || innerRadius.getZ() != 0) {
List<E> innerEntities = world.getNearbyEntities(location, innerRadius.getX(), innerRadius.getZ(), innerRadius.getZ(), entityType::isInstance).stream().map(entityType::cast).collect(Collectors.toList());
List<E> innerEntities = world.getNearbyEntities(location, innerRadius.getX(), innerRadius.getZ(), innerRadius.getZ(), entityType::isInstance).stream().map(entityType::cast).toList();
outerEntities.removeAll(innerEntities);
}
return outerEntities;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import me.wolfyscript.customcrafting.utils.NamespacedKeyUtils;
import me.wolfyscript.customcrafting.CustomCrafting;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
Expand All @@ -46,7 +46,7 @@ public class ResultExtensionAdvancement extends ResultExtension {
private boolean nearWorkstation = false;

public ResultExtensionAdvancement() {
super(new me.wolfyscript.utilities.util.NamespacedKey(NamespacedKeyUtils.NAMESPACE, "advancement"));
super(new me.wolfyscript.utilities.util.NamespacedKey(CustomCrafting.inst(), "advancement"));
}

public ResultExtensionAdvancement(ResultExtensionAdvancement extension) {
Expand Down Expand Up @@ -90,7 +90,7 @@ public ResultExtensionAdvancement clone() {

@JsonGetter
private String getAdvancement() {
return advancement.toString();
return this.advancement.toString();
}

@JsonSetter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package me.wolfyscript.customcrafting.recipes.items.extension;

import me.wolfyscript.customcrafting.CustomCrafting;
import me.wolfyscript.customcrafting.utils.NamespacedKeyUtils;
import me.wolfyscript.utilities.util.NamespacedKey;
import org.bukkit.Location;
Expand All @@ -44,7 +45,7 @@ public class SoundResultExtension extends ResultExtension {
private boolean onBlock = true;

public SoundResultExtension() {
super(new NamespacedKey(NamespacedKeyUtils.NAMESPACE, "sound"));
super(new NamespacedKey(CustomCrafting.inst(), "sound"));
}

public SoundResultExtension(SoundResultExtension extension) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ public void consumeRecipe(ItemStack result, InventoryClickEvent event) {
Result recipeResult = craftingData.getResult();
editStatistics(player, inventory, recipe);
setPlayerCraftTime(player, recipe);
recipeResult.executeExtensions(inventory.getLocation() == null ? event.getWhoClicked().getLocation() : inventory.getLocation(), inventory.getLocation() != null, (Player) event.getWhoClicked());
calculateClick(player, event, craftingData, recipe, recipeResult);
}
remove(event.getWhoClicked().getUniqueId());
Expand All @@ -143,9 +142,11 @@ private void setPlayerCraftTime(Player player, CraftingRecipe<?, ?> recipe) {
}

private void calculateClick(Player player, InventoryClickEvent event, CraftingData craftingData, CraftingRecipe<?, ?> recipe, Result recipeResult) {
ItemStack result = recipeResult.getItem(craftingData, player, null);
var result = recipeResult.getItem(craftingData, player, null);
var inventory = event.getClickedInventory();
int possible = event.isShiftClick() ? Math.min(InventoryUtils.getInventorySpace(player.getInventory(), result) / result.getAmount(), recipe.getAmountCraftable(craftingData)) : 1;
recipe.removeMatrix(player, event.getClickedInventory(), possible, craftingData);
recipeResult.executeExtensions(inventory.getLocation() == null ? event.getWhoClicked().getLocation() : inventory.getLocation(), inventory.getLocation() != null, (Player) event.getWhoClicked(), possible);
if (event.isShiftClick()) {
if (possible > 0) {
RandomCollection<CustomItem> results = recipeResult.getRandomChoices(player);
Expand Down

0 comments on commit 30ac101

Please sign in to comment.