1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Fixed unbreakable Flint and Steel still being damaged in Smeltery

This commit is contained in:
TheBusyBiscuit 2021-07-25 23:41:47 +02:00
parent c46cfa6686
commit 55bbe05961
3 changed files with 120 additions and 49 deletions

View File

@ -30,6 +30,7 @@
## Release Candidate 27 (TBD)
#### Additions
* A couple more items have their wiki page linked ingame now
#### Changes
* Copper wire can no longer be placed down
@ -38,6 +39,7 @@
#### Fixes
* Fixed #3164
* Fixed #3177
* Fixed unbreakable Flint and Steel still being damaged in Ignition Chambers
## Release Candidate 26 (20 Jul 2021)
https://thebusybiscuit.github.io/builds/TheBusyBiscuit/Slimefun4/stable/#26

View File

@ -1,20 +1,38 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.blocks;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Dropper;
import org.bukkit.block.data.type.Dispenser;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.handlers.VanillaInventoryDropHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.Smeltery;
import io.papermc.lib.PaperLib;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* The {@link IgnitionChamber} is used to re-ignite a {@link Smeltery}.
*
* @author AtomicScience
* @author TheBusyBiscuit
*
* @see Smeltery
@ -22,6 +40,15 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
*/
public class IgnitionChamber extends SlimefunItem {
// @formatter:off
private static final BlockFace[] ADJACENT_FACES = {
BlockFace.NORTH,
BlockFace.EAST,
BlockFace.SOUTH,
BlockFace.WEST
};
// @formatter:on
@ParametersAreNonnullByDefault
public IgnitionChamber(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
@ -29,4 +56,73 @@ public class IgnitionChamber extends SlimefunItem {
addItemHandler(new VanillaInventoryDropHandler<>(Dropper.class));
}
/**
* This triggers an {@link IgnitionChamber} to be used from the given {@link Smeltery}
* block and {@link Player}.
*
* @param p
* The {@link Player} who triggered this action
* @param smelteryBlock
* The {@link Dispenser} block of our {@link Smeltery}
*
* @return Whether the operation completed successfully.
* This will return <code>false</code> when there is no
* chamber or no flint and steel present
*/
@ParametersAreNonnullByDefault
public static boolean useFlintAndSteel(Player p, Block smelteryBlock) {
Validate.notNull(p, "The Player must not be null!");
Validate.notNull(smelteryBlock, "The smeltery block cannot be null!");
Inventory inv = findIgnitionChamber(smelteryBlock);
// Check if there even is a chamber nearby
if (inv == null) {
return false;
}
// Check if the chamber contains a Flint and Steel
if (inv.contains(Material.FLINT_AND_STEEL)) {
ItemStack item = inv.getItem(inv.first(Material.FLINT_AND_STEEL));
ItemMeta meta = item.getItemMeta();
// Only damage the Flint and Steel if it isn't unbreakable.
if (!meta.isUnbreakable()) {
// Update the damage value
((Damageable) meta).setDamage(((Damageable) meta).getDamage() + 1);
if (((Damageable) meta).getDamage() >= item.getType().getMaxDurability()) {
// The Flint and Steel broke
item.setAmount(0);
smelteryBlock.getWorld().playSound(smelteryBlock.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
} else {
item.setItemMeta(meta);
}
}
smelteryBlock.getWorld().playSound(smelteryBlock.getLocation(), Sound.ITEM_FLINTANDSTEEL_USE, 1, 1);
return true;
} else {
// Notify the Player there is a chamber but without any Flint and Steel
SlimefunPlugin.getLocalization().sendMessage(p, "machines.ignition-chamber-no-flint", true);
return false;
}
}
private static @Nullable Inventory findIgnitionChamber(@Nonnull Block b) {
for (BlockFace face : ADJACENT_FACES) {
Block block = b.getRelative(face);
if (block.getType() == Material.DROPPER && BlockStorage.check(block) instanceof IgnitionChamber) {
BlockState state = PaperLib.getBlockState(b.getRelative(face), false).getState();
if (state instanceof Dropper) {
return ((Dropper) state).getInventory();
}
}
}
return null;
}
}

View File

@ -7,36 +7,46 @@ import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Dropper;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.api.items.settings.IntRangeSetting;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunItems;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.blocks.IgnitionChamber;
import io.papermc.lib.PaperLib;
import io.github.thebusybiscuit.slimefun4.implementation.items.misc.AlloyIngot;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* The {@link Smeltery} is an upgraded version of the {@link MakeshiftSmeltery}
* with the additional capabilities to create {@link AlloyIngot}s and utilise an
* {@link IgnitionChamber}.
*
* @author TheBusyBiscuit
* @author AtomicScience
* @author Sfiguz7
* @author Liruxo
* @author emanueljg
* @author SoSeDiK
* @author Redemption198
*
* @see AbstractSmeltery
* @see MakeshiftSmeltery
* @see IgnitionChamber
*
*/
public class Smeltery extends AbstractSmeltery {
private final BlockFace[] faces = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
private final ItemSetting<Integer> fireBreakingChance = new IntRangeSetting(this, "fire-breaking-chance", 0, 34, 100);
@ParametersAreNonnullByDefault
@ -79,50 +89,13 @@ public class Smeltery extends AbstractSmeltery {
@ParametersAreNonnullByDefault
private void consumeFire(Player p, Block dispenser, Block b) {
Inventory chamber = findIgnitionChamber(dispenser);
boolean isFireRenewed = IgnitionChamber.useFlintAndSteel(p, dispenser);
if (chamber != null) {
if (chamber.contains(Material.FLINT_AND_STEEL)) {
ItemStack item = chamber.getItem(chamber.first(Material.FLINT_AND_STEEL));
ItemMeta meta = item.getItemMeta();
((Damageable) meta).setDamage(((Damageable) meta).getDamage() + 1);
item.setItemMeta(meta);
if (((Damageable) item.getItemMeta()).getDamage() >= item.getType().getMaxDurability()) {
item.setAmount(0);
p.getWorld().playSound(p.getLocation(), Sound.ENTITY_ITEM_BREAK, 1, 1);
}
p.getWorld().playSound(p.getLocation(), Sound.ITEM_FLINTANDSTEEL_USE, 1, 1);
} else {
SlimefunPlugin.getLocalization().sendMessage(p, "machines.ignition-chamber-no-flint", true);
Block fire = b.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN);
fire.getWorld().playEffect(fire.getLocation(), Effect.STEP_SOUND, fire.getType());
fire.setType(Material.AIR);
}
} else {
if (!isFireRenewed) {
Block fire = b.getRelative(BlockFace.DOWN).getRelative(BlockFace.DOWN);
fire.getWorld().playEffect(fire.getLocation(), Effect.STEP_SOUND, fire.getType());
fire.setType(Material.AIR);
}
}
@Nullable
private Inventory findIgnitionChamber(@Nonnull Block b) {
for (BlockFace face : faces) {
Block block = b.getRelative(face);
if (block.getType() == Material.DROPPER && BlockStorage.check(block) instanceof IgnitionChamber) {
BlockState state = PaperLib.getBlockState(b.getRelative(face), false).getState();
if (state instanceof Dropper) {
return ((Dropper) state).getInventory();
}
}
}
return null;
}
}