Skip to content

Instantly share code, notes, and snippets.

@Twister915
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Twister915/cccc32bcc27ab28400d3 to your computer and use it in GitHub Desktop.
Save Twister915/cccc32bcc27ab28400d3 to your computer and use it in GitHub Desktop.
Converts ores and squashes ingots into blocks, based strictly on bukkit stuff from line 22 down
package com.nebulas.dev;
import net.cogzmc.core.modular.command.CommandException;
import net.cogzmc.core.modular.command.ModuleCommand;
import net.cogzmc.core.player.CPlayer;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import java.util.HashMap;
import java.util.Map;
public class LolCommand extends ModuleCommand {
public LolCommand() {
super("lol");
}
@SuppressWarnings("deprecation")
@Override
protected void handleCommand(CPlayer player, String[] args) throws CommandException {
Player bukkitPlayer = player.getBukkitPlayer();
PlayerInventory inventory = bukkitPlayer.getInventory();
Map<Material, Integer> converted = new HashMap<>();
Map<Material, Integer> squashed = new HashMap<>();
//Do our conversions
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack item = inventory.getItem(i);
if (item == null) continue;
for (OreConversions oreConversion : OreConversions.values()) {
if (oreConversion.from == item.getType()) {
ItemStack itemStack = new ItemStack(oreConversion.to, item.getAmount());
inventory.setItem(i, itemStack);
//Might as well keep a tally for a message at the end
Integer integer = converted.get(oreConversion.from);
if (integer == null) integer = 0;
integer += itemStack.getAmount();
converted.put(oreConversion.from, integer);
break;
}
}
}
//Do our squashing
for (BlockConversions bConversion : BlockConversions.values()) {
int amount = 0;
for (ItemStack itemStack : inventory) {
if (itemStack == null) continue;
if (itemStack.getType() != bConversion.from) continue;
amount += itemStack.getAmount();
}
if (amount < bConversion.quantity) continue;
int toRemove = amount - (amount % bConversion.quantity);
int blocks = toRemove/bConversion.quantity;
Integer slot = -1;
for (int i = 0; i < inventory.getSize(); i++) {
ItemStack item = inventory.getItem(i);
if (item == null) continue;
if (item.getType() != bConversion.from) continue;
if (toRemove < item.getAmount()) {
item.setAmount(item.getAmount()-toRemove);
toRemove = 0;
break;
}
if (slot == -1) slot = i;
inventory.setItem(i, null);
if (toRemove == item.getAmount()) {
toRemove = 0;
break;
} else if (toRemove > item.getAmount()) {
toRemove -= item.getAmount();
}
}
if (toRemove != 0) throw new IllegalStateException("I was unable to safely remove all items from the inventory!");
ItemStack itemStack = new ItemStack(bConversion.to, blocks);
if (slot == -1) {
inventory.addItem(itemStack);
} else {
inventory.setItem(slot, itemStack);
}
Integer integer = squashed.get(bConversion.from);
if (integer == null) integer = 0;
integer += blocks*bConversion.quantity;
squashed.put(bConversion.from, integer);
}
bukkitPlayer.updateInventory();
}
private static enum BlockConversions {
IRON(Material.IRON_INGOT, 9, Material.IRON_BLOCK),
GOLD(Material.GOLD_INGOT, 9, Material.GOLD_BLOCK),
GOLD_DUST(Material.GOLD_NUGGET, 9, Material.GOLD_INGOT),
DIAMOND(Material.DIAMOND, 9, Material.DIAMOND_BLOCK),
EMERALD(Material.EMERALD, 9, Material.EMERALD_BLOCK);
private final Material from;
private final Integer quantity;
private final Material to;
BlockConversions(Material from, Integer quantity, Material to) {
this.from = from;
this.quantity = quantity;
this.to = to;
}
}
private static enum OreConversions {
IRON(Material.IRON_ORE, Material.IRON_INGOT),
DIAMOND(Material.DIAMOND_ORE, Material.DIAMOND),
COAL(Material.COAL_ORE, Material.COAL),
GOLD(Material.GOLD_ORE, Material.GOLD_INGOT);
private final Material from;
private final Material to;
OreConversions(Material from, Material to) {
this.from = from;
this.to = to;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment