Skip to content

Commit

Permalink
GH-8 Add heart item crafting (#12)
Browse files Browse the repository at this point in the history
* GH-8 Add heart item crafting

* Remove useless variable
  • Loading branch information
zrdzn committed May 22, 2022
1 parent f2d146d commit 40772a7
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.github.zrdzn.minecraft.greatlifesteal.spigot.V1_8SpigotAdapter;
import io.github.zrdzn.minecraft.greatlifesteal.spigot.V1_9SpigotAdapter;
import io.github.zrdzn.minecraft.greatlifesteal.user.UserListener;
import org.bukkit.Server;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
Expand All @@ -21,7 +22,9 @@ public class GreatLifeStealPlugin extends JavaPlugin {
public void onEnable() {
Logger logger = LoggerFactory.getLogger("GreatLifeSteal");

PluginManager pluginManager = this.getServer().getPluginManager();
Server server = this.getServer();

PluginManager pluginManager = server.getPluginManager();

Configuration configuration = this.getConfig();

Expand All @@ -41,6 +44,8 @@ public void onEnable() {
return;
}

server.addRecipe(pluginConfig.getHeartItem().getCraftingRecipe());

DamageableAdapter damageableAdapter = this.prepareSpigotAdapter().getDamageableAdapter();

UserListener userListener = new UserListener(pluginConfig, damageableAdapter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.zrdzn.minecraft.greatlifesteal.config;

import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartItem;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationMode;

import java.util.Map.Entry;
Expand All @@ -10,14 +11,16 @@ public class PluginConfig {
private final int healthChange;
private final Entry<Integer, Integer> healthRange;
private final boolean killByPlayerOnly;
private final HeartItem heartItem;
private final EliminationMode eliminationMode;

public PluginConfig(int defaultHealth, int healthChange, Entry<Integer, Integer> healthRange,
boolean killByPlayerOnly, EliminationMode eliminationMode) {
boolean killByPlayerOnly, HeartItem heartItem, EliminationMode eliminationMode) {
this.defaultHealth = defaultHealth;
this.healthChange = healthChange;
this.healthRange = healthRange;
this.killByPlayerOnly = killByPlayerOnly;
this.heartItem = heartItem;
this.eliminationMode = eliminationMode;
}

Expand All @@ -37,6 +40,10 @@ public boolean isKillByPlayerOnly() {
return this.killByPlayerOnly;
}

public HeartItem getHeartItem() {
return this.heartItem;
}

public EliminationMode getEliminationMode() {
return this.eliminationMode;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package io.github.zrdzn.minecraft.greatlifesteal.config;

import io.github.zrdzn.minecraft.greatlifesteal.heart.HeartItem;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationMode;
import io.github.zrdzn.minecraft.greatlifesteal.elimination.EliminationModeAction;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.List;
import java.util.stream.Collectors;

public class PluginConfigParser {

Expand Down Expand Up @@ -36,6 +44,61 @@ public PluginConfig parse(ConfigurationSection section) throws InvalidConfigurat

boolean killByPlayerOnly = section.getBoolean("killByPlayerOnly");

ConfigurationSection heartItemSection = section.getConfigurationSection("heartItem");
if (heartItemSection == null) {
throw new InvalidConfigurationException("Section 'heartItem' cannot be null.");
}

HeartItem heartItem = null;
if (heartItemSection.getBoolean("enabled")) {
int heartItemHealthAmount = heartItemSection.getInt("healthAmount");
if (heartItemHealthAmount < 0) {
throw new InvalidConfigurationException("Property 'healthAmount' cannot be lower than 0.");
}

Material heartItemType = Material.matchMaterial("type");
if (heartItemType == null) {
throw new InvalidConfigurationException("Property 'type' is not a valid item type.");
}

ItemStack heartItemStack = new ItemStack(heartItemType);

ConfigurationSection heartItemMetaSection = heartItemSection.getConfigurationSection("meta");
if (heartItemMetaSection != null) {
ItemMeta heartItemMeta = heartItemStack.getItemMeta();

String displayName = heartItemSection.getString("meta.displayName");
if (displayName == null) {
throw new InvalidConfigurationException("Property 'displayName' cannot be null.");
}

heartItemMeta.setDisplayName(formatColor(displayName));
heartItemMeta.setLore(formatColor(heartItemMetaSection.getStringList("lore")));

heartItemStack.setItemMeta(heartItemMeta);
}

ShapedRecipe heartItemRecipe = new ShapedRecipe(heartItemStack.clone());

heartItemRecipe.shape("123", "456", "789");

ConfigurationSection heartItemRecipeSection = heartItemSection.getConfigurationSection("craftingRecipe");
if (heartItemRecipeSection == null) {
throw new InvalidConfigurationException("Section 'craftingRecipe' cannot be null.");
}

for (String key : heartItemRecipeSection.getKeys(false)) {
Material material = Material.matchMaterial(heartItemRecipeSection.getString(key));
if (material == null) {
throw new InvalidConfigurationException("Item type in the recipe section is invalid.");
}

heartItemRecipe.setIngredient(key.charAt(0), material);
}

heartItem = new HeartItem(heartItemHealthAmount, heartItemRecipe);
}

ConfigurationSection eliminationSection = section.getConfigurationSection("eliminationMode");
if (eliminationSection == null) {
throw new InvalidConfigurationException("Section 'eliminationMode' cannot be null.");
Expand Down Expand Up @@ -66,7 +129,17 @@ public PluginConfig parse(ConfigurationSection section) throws InvalidConfigurat
}

return new PluginConfig(defaultHealth, healthChange, new SimpleImmutableEntry<>(minimumHealth, maximumHealth),
killByPlayerOnly, elimination);
killByPlayerOnly, heartItem, elimination);
}

private static String formatColor(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}

private static List<String> formatColor(List<String> strings) {
return strings.stream()
.map(PluginConfigParser::formatColor)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.github.zrdzn.minecraft.greatlifesteal.heart;

import org.bukkit.inventory.ShapedRecipe;

public class HeartItem {

private final int healthAmount;
private final ShapedRecipe craftingRecipe;

public HeartItem(int healthAmount, ShapedRecipe craftingRecipe) {
this.healthAmount = healthAmount;
this.craftingRecipe = craftingRecipe;
}

public int getHealthAmount() {
return this.healthAmount;
}

public ShapedRecipe getCraftingRecipe() {
return this.craftingRecipe;
}

}
29 changes: 29 additions & 0 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,35 @@ baseSettings:
# Health points will be changed only if the player was killed by the other player.
killByPlayerOnly: true

# Item that can be used by a player to give him a specified amount of health points.
heartItem:
# If any heart item should be enabled on the server.
enabled: false

# Amount of health points that should be given to a player on item consume.
healthAmount: 2

# Type of the item that the heart item should be.
type: APPLE

# Meta for the heart item.
meta:
displayName: "&aThe Heart of an Elk"
lore:
- "&aUse this item to give yourself health points."

# Recipe for the heart item creation. Each number is an ordered slot in the workbench. (1-9)
craftingRecipe:
1: DIAMOND_BLOCK
2: DIAMOND_BLOCK
3: DIAMOND_BLOCK
4: DIAMOND_BLOCK
5: OBSIDIAN
6: DIAMOND_BLOCK
7: DIAMOND_BLOCK
8: DIAMOND_BLOCK
9: DIAMOND_BLOCK

# Define what will happen if a player reaches specific amount of maximum health points.
eliminationMode:
# If any action below should be executed.
Expand Down

0 comments on commit 40772a7

Please sign in to comment.