Custom items refractoring

This commit is contained in:
Tomás F 2020-07-01 19:42:39 +01:00
parent 512b5c71e8
commit b85876c8b3
8 changed files with 34 additions and 51 deletions

View File

@ -73,8 +73,6 @@ public class BeesPlus extends JavaPlugin {
customItemManager.registerCustomItem("protection_chestplate", new BeeProtectionChestplate());
customItemManager.registerCustomItem("protection_helmet", new BeeProtectionHelmet());
customItemManager.registerRecipes();
getServer().getPluginManager().registerEvents(new DamageHandler(this), this);
}

View File

@ -9,13 +9,6 @@ public abstract class CustomItem {
public abstract String[] getRecipe();
public abstract Map<Character, Material> getIngredients();
public abstract String getName();
public abstract Material getMaterial();
public ItemStack getItem() {
return new ItemBuilder(getMaterial())
.setName(getName())
.build();
}
public abstract ItemStack getResult();
}

View File

@ -24,16 +24,12 @@ public class CustomItemManager {
public void registerCustomItem(String id, CustomItem customItem) {
customItems.put(id, customItem);
}
public void registerRecipes() {
customItems.forEach((id, customItem) -> {
NamespacedKey namespacedKey = new NamespacedKey(beesPlus, id);
ShapedRecipe recipe = new ShapedRecipe(namespacedKey, customItem.getItem());
recipe.shape(customItem.getRecipe());
customItem.getIngredients().forEach(recipe::setIngredient);
NamespacedKey namespacedKey = new NamespacedKey(beesPlus, id);
ShapedRecipe recipe = new ShapedRecipe(namespacedKey, customItem.getResult());
recipe.shape(customItem.getRecipe());
customItem.getIngredients().forEach(recipe::setIngredient);
Bukkit.addRecipe(recipe);
});
Bukkit.addRecipe(recipe);
}
}

View File

@ -42,10 +42,10 @@ public class DamageHandler implements Listener {
PlayerInventory playerInventory = player.getInventory();
if (Stream.of(playerInventory.getArmorContents()).allMatch(Objects::nonNull)) {
if (playerInventory.getHelmet().isSimilar(helmet.getItem()) &&
playerInventory.getChestplate().isSimilar(chestplate.getItem()) &&
playerInventory.getLeggings().isSimilar(leggings.getItem()) &&
playerInventory.getBoots().isSimilar(boots.getItem())) {
if (playerInventory.getHelmet().isSimilar(helmet.getResult()) &&
playerInventory.getChestplate().isSimilar(chestplate.getResult()) &&
playerInventory.getLeggings().isSimilar(leggings.getResult()) &&
playerInventory.getBoots().isSimilar(boots.getResult())) {
event.setDamage(reduction * event.getDamage());
}

View File

@ -1,8 +1,10 @@
package com.tomff.beesplus.items;
import com.tomff.beesplus.core.items.CustomItem;
import com.tomff.beesplus.core.items.ItemBuilder;
import com.tomff.beesplus.localization.Localization;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
@ -28,12 +30,9 @@ public class BeeProtectionBoots extends CustomItem {
}
@Override
public String getName() {
return Localization.get(Localization.BEE_PROTECTION_BOOTS);
}
@Override
public Material getMaterial() {
return Material.CHAINMAIL_BOOTS;
public ItemStack getResult() {
return new ItemBuilder(Material.CHAINMAIL_BOOTS)
.setName(Localization.get(Localization.BEE_PROTECTION_BOOTS))
.build();
}
}

View File

@ -1,8 +1,10 @@
package com.tomff.beesplus.items;
import com.tomff.beesplus.core.items.CustomItem;
import com.tomff.beesplus.core.items.ItemBuilder;
import com.tomff.beesplus.localization.Localization;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
@ -28,12 +30,9 @@ public class BeeProtectionChestplate extends CustomItem {
}
@Override
public String getName() {
return Localization.get(Localization.BEE_PROTECTION_CHESTPLATE);
}
@Override
public Material getMaterial() {
return Material.CHAINMAIL_CHESTPLATE;
public ItemStack getResult() {
return new ItemBuilder(Material.CHAINMAIL_CHESTPLATE)
.setName(Localization.get(Localization.BEE_PROTECTION_CHESTPLATE))
.build();
}
}

View File

@ -1,8 +1,10 @@
package com.tomff.beesplus.items;
import com.tomff.beesplus.core.items.CustomItem;
import com.tomff.beesplus.core.items.ItemBuilder;
import com.tomff.beesplus.localization.Localization;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
@ -28,12 +30,9 @@ public class BeeProtectionHelmet extends CustomItem {
}
@Override
public String getName() {
return Localization.get(Localization.BEE_PROTECTION_HELMET);
}
@Override
public Material getMaterial() {
return Material.CHAINMAIL_HELMET;
public ItemStack getResult() {
return new ItemBuilder(Material.CHAINMAIL_HELMET)
.setName(Localization.get(Localization.BEE_PROTECTION_HELMET))
.build();
}
}

View File

@ -1,8 +1,10 @@
package com.tomff.beesplus.items;
import com.tomff.beesplus.core.items.CustomItem;
import com.tomff.beesplus.core.items.ItemBuilder;
import com.tomff.beesplus.localization.Localization;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.HashMap;
import java.util.Map;
@ -28,12 +30,9 @@ public class BeeProtectionLeggings extends CustomItem {
}
@Override
public String getName() {
return Localization.get(Localization.BEE_PROTECTION_LEGGINGS);
}
@Override
public Material getMaterial() {
return Material.CHAINMAIL_LEGGINGS;
public ItemStack getResult() {
return new ItemBuilder(Material.CHAINMAIL_LEGGINGS)
.setName(Localization.get(Localization.BEE_PROTECTION_LEGGINGS))
.build();
}
}