Skip to content

Instantly share code, notes, and snippets.

@Jikoo
Created April 1, 2015 01:39
Show Gist options
  • Save Jikoo/cf6a007d4feb3e12080e to your computer and use it in GitHub Desktop.
Save Jikoo/cf6a007d4feb3e12080e to your computer and use it in GitHub Desktop.
FastTNT-NoRedSand
package com.github.jikoo.fasttnt;
import java.util.HashMap;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Just for fun!
*
* @author Jikoo
*/
public class FastTNT extends JavaPlugin {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage("FastTNT does not offer console support at this time, sorry!");
return true;
}
Player player = (Player) sender;
// Total tnt that can be made with the materials in inventory first
int tntSand = sum(player.getInventory().all(Material.SAND), (short) 0) / 4;
int tntGunpowder = sum(player.getInventory().all(Material.SULPHUR)) / 5;
// Set total based on limiting factor
int tnt = tntSand <= tntGunpowder ? tntSand : tntGunpowder;
// Define total quantity that must be removed
tntSand = tnt * 4;
tntGunpowder = tnt * 5;
// Remove sand from Player
ItemStack is = new ItemStack(Material.SAND, 64);
while (tntSand > 0) {
if (tntSand <= 64) {
is.setAmount(tntSand);
tntSand = 0;
} else {
tntSand -= 64;
}
int failures = sum(player.getInventory().removeItem(is));
if (failures > 0) {
// If the player has no plain sand left, they must have red sand.
is = new ItemStack(Material.SAND, failures, (short) 1);
player.getInventory().removeItem(is);
}
}
// Remove sulpher (gunpowder) from Player
is.setType(Material.SULPHUR);
is.setAmount(64);
while (tntGunpowder > 0) {
if (tntGunpowder <= 64) {
is.setAmount(tntGunpowder);
tntGunpowder = 0;
} else {
tntGunpowder -= 64;
}
player.getInventory().removeItem(is);
}
player.sendMessage(ChatColor.GREEN + "Crafted " + tnt + " TNT!");
// Add TNT to Player
is.setType(Material.TNT);
is.setAmount(64);
while (tnt > 0) {
if (tnt <= 64) {
is.setAmount(tnt);
tnt = 0;
} else {
// In case of a massive addition failure, reset stack to 64 each round
is.setAmount(64);
tnt -= 64;
}
int failure = sum(player.getInventory().addItem(is));
// Hypothetical situation: Player has 7 sand, 6 gunpowder, inv is full of non-tnt items.
// Drop items at Player location rather than just delete resources.
is.setAmount(failure);
if (failure > 0) {
player.getWorld().dropItem(player.getLocation(), is);
}
}
return true;
}
private int sum(HashMap<Integer, ? extends ItemStack> hashMap) {
return sum(hashMap, Short.MAX_VALUE);
}
private int sum(HashMap<Integer, ? extends ItemStack> hashMap, short durability) {
int sum = 0;
for (ItemStack is : hashMap.values()) {
if (durability == Short.MAX_VALUE || durability == is.getDurability()) {
sum += is.getAmount();
}
}
return sum;
}
}
name: FastTNT
main: com.github.jikoo.fasttnt.FastTNT
version: 1.3-NoRedSand
author: Jikoo
commands:
fasttnt:
default: false
description: Craft TNT in a jiffy!
permission: fasttnt.use
aliases: [ftnt, tnt, ctnt]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment