use of org.bukkit.block.Chest in project Minigames by AddstarMC.
the class MgBlockData method randomizeContents.
public void randomizeContents(int minContents, int maxContents) {
if (hasRandomized || items == null)
return;
List<ItemStack> itemRand = new ArrayList<>();
for (ItemStack item1 : items) {
if (item1 != null) {
itemRand.add(item1.clone());
}
}
Collections.shuffle(itemRand);
List<ItemStack> itemChest = new ArrayList<>();
if (maxContents > itemRand.size()) {
maxContents = itemRand.size();
}
if (minContents > itemRand.size()) {
minContents = itemRand.size();
}
int rand = minContents + (int) (Math.random() * ((maxContents - minContents) + 1));
for (int i = 0; i < items.length; i++) {
if (i < rand) {
itemChest.add(i, itemRand.get(i));
} else {
itemChest.add(null);
}
}
Collections.shuffle(itemChest);
ItemStack[] newItems = new ItemStack[itemChest.size()];
int inc = 0;
for (ItemStack item : itemChest) {
newItems[inc] = item;
inc++;
}
if (state instanceof Chest) {
Chest chest = (Chest) state;
chest.getInventory().setContents(newItems);
}
hasRandomized = true;
}
use of org.bukkit.block.Chest in project Minigames by AddstarMC.
the class RecorderData method addBlock.
public MgBlockData addBlock(BlockState block, MinigamePlayer modifier) {
MgBlockData bdata = new MgBlockData(block, modifier);
String sloc = bdata.getLocation().getBlockX() + ":" + bdata.getLocation().getBlockY() + ":" + bdata.getLocation().getBlockZ();
if (!blockdata.containsKey(sloc)) {
if (block instanceof InventoryHolder) {
InventoryHolder inv = (InventoryHolder) block;
if (inv instanceof DoubleChest) {
Location left = ((DoubleChest) inv).getLeftSide().getInventory().getLocation().clone();
Location right = ((DoubleChest) inv).getRightSide().getInventory().getLocation().clone();
if (bdata.getLocation() == left) {
addInventory(bdata, ((DoubleChest) inv).getLeftSide());
if (minigame.isRandomizeChests())
bdata.randomizeContents(minigame.getMinChestRandom(), minigame.getMaxChestRandom());
}
MgBlockData secondChest = addBlock(right.getBlock(), modifier);
if (secondChest.getItems() == null) {
addInventory(secondChest, ((DoubleChest) inv).getRightSide());
if (minigame.isRandomizeChests())
secondChest.randomizeContents(minigame.getMinChestRandom(), minigame.getMaxChestRandom());
} else if (inv instanceof Chest) {
addInventory(bdata, inv);
if (minigame.isRandomizeChests())
bdata.randomizeContents(minigame.getMinChestRandom(), minigame.getMaxChestRandom());
}
} else {
addInventory(bdata, inv);
}
} else if (block.getType() == Material.FLOWER_POT) {
bdata.setSpecialData("contents", block.getData());
}
blockdata.put(sloc, bdata);
return bdata;
} else {
if (block.getType() != Material.CHEST || !blockdata.get(sloc).hasRandomized())
blockdata.get(sloc).setModifier(modifier);
return blockdata.get(sloc);
}
}
use of org.bukkit.block.Chest in project Minigames by AddstarMC.
the class TreasureHuntMechanic method spawnTreasure.
public static void spawnTreasure(final Minigame mgm) {
final TreasureHuntModule thm = TreasureHuntModule.getMinigameModule(mgm);
if (thm.hasTreasureLocation())
removeTreasure(mgm);
if (!thm.getCurrentHints().isEmpty())
thm.clearHints();
thm.setTreasureFound(false);
Location tcpos = mgm.getStartLocations().get(0).clone();
final Location rpos = tcpos;
double rx;
double ry;
double rz;
final int maxradius;
if (thm.getMaxRadius() == 0) {
maxradius = 1000;
} else {
maxradius = thm.getMaxRadius();
}
final int maxheight = thm.getMaxHeight();
Random rand = new Random();
int rrad = rand.nextInt(maxradius);
double randCir = 2 * Math.PI * rand.nextInt(360) / 360;
rx = tcpos.getX() - 0.5 + Math.round(rrad * Math.cos(randCir));
rz = tcpos.getZ() - 0.5 + Math.round(rrad * Math.sin(randCir));
ry = tcpos.getY() + rand.nextInt(maxheight);
rpos.setX(rx);
rpos.setY(ry);
rpos.setZ(rz);
// TODO: Improve so no invalid spawns (Not over void, Strict containment)
switch(rpos.getBlock().getType()) {
case AIR:
case CAVE_AIR:
case VOID_AIR:
while (rpos.getBlock().getType() == Material.AIR && rpos.getY() > 1) {
rpos.setY(rpos.getY() - 1);
}
rpos.setY(rpos.getY() + 1);
Bukkit.getScheduler().runTaskLater(plugin, () -> rpos.getBlock().setType(Material.CHEST), 1L);
break;
default:
while (rpos.getBlock().getType() != Material.AIR && rpos.getY() < 255) {
rpos.setY(rpos.getY() + 1);
}
Bukkit.getScheduler().runTaskLater(plugin, () -> rpos.getBlock().setType(Material.CHEST), 1L);
break;
}
// Fill new chest
Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (rpos.getBlock().getState() instanceof Chest) {
final Chest chest = (Chest) rpos.getBlock().getState();
// TODO: Treasure hunt needs own rewards specification
RewardsModule rewards = RewardsModule.getModule(mgm);
if (rewards.getScheme() instanceof StandardRewardScheme) {
if (!((StandardRewardScheme) rewards.getScheme()).getPrimaryReward().getRewards().isEmpty()) {
int numitems = (int) Math.round(Math.random() * (thm.getMaxTreasure() - thm.getMinTreasure())) + thm.getMinTreasure();
final ItemStack[] items = new ItemStack[27];
for (int i = 0; i < numitems; i++) {
RewardType rew = ((StandardRewardScheme) rewards.getScheme()).getPrimaryReward().getReward().get(0);
if (rew instanceof ItemReward) {
ItemReward irew = (ItemReward) rew;
items[i] = irew.getRewardItem();
}
}
Collections.shuffle(Arrays.asList(items));
chest.getInventory().setContents(items);
}
}
}
}, 0L);
thm.setTreasureLocation(rpos);
plugin.getLogger().info(MinigameUtils.formStr("minigame.treasurehunt.consSpawn", mgm.getName(false), rpos.getBlockX() + ", " + rpos.getBlockY() + ", " + rpos.getBlockZ()));
MinigameUtils.broadcast(MinigameUtils.formStr("minigame.treasurehunt.plySpawn", maxradius, thm.getLocation()), mgm, "minigame.treasure.announce");
mgm.setMinigameTimer(new MinigameTimer(mgm, mgm.getTimer()));
}
use of org.bukkit.block.Chest in project HawkEye by oliverwoodings.
the class InventoryUtil method getContainerContents.
/**
* Method for getting complete inventory from a ContainerBlock
* Works around a bug in Minecraft that sometimes returns only half the chest
* Thanks to N3X15 and the BigBrother team for letting me use this
* @param container block to check
* @return ItemStack[] of both inventories merged
* @author N3X15
*/
public static ItemStack[] getContainerContents(InventoryHolder container) {
// If it isn't a chest, there is no issue!
if (!(container instanceof Chest))
return container.getInventory().getContents();
Chest chest = (Chest) container;
Chest second = null;
// Iterate through nearby blocks to find any other chests
if (chest.getBlock().getRelative(BlockFace.NORTH).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.NORTH).getState();
else if (chest.getBlock().getRelative(BlockFace.SOUTH).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.SOUTH).getState();
else if (chest.getBlock().getRelative(BlockFace.EAST).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.EAST).getState();
else if (chest.getBlock().getRelative(BlockFace.WEST).getType() == Material.CHEST)
second = (Chest) chest.getBlock().getRelative(BlockFace.WEST).getState();
// If we can't find a second chest, just return this one
if (second == null) {
return chest.getInventory().getContents();
} else {
// I think it would be good, to consistently return same chest
// contents, regardless of what
// block was clicked on. That means, we must determine, which part
// of chest comes first, and which second.
// I choose the one, which has lower X coordinate. If they are same,
// than it's the one with lower Z coordinate.
// I believe it can be easily checked with this trick:
ItemStack[] result = new ItemStack[54];
ItemStack[] firstHalf;
ItemStack[] secondHalf;
if ((chest.getX() + chest.getZ()) < (second.getX() + second.getZ())) {
firstHalf = chest.getInventory().getContents();
secondHalf = second.getInventory().getContents();
} else {
firstHalf = second.getInventory().getContents();
secondHalf = chest.getInventory().getContents();
}
// Merge them
for (int i = 0; i < 27; i++) {
result[i] = firstHalf[i];
result[i + 27] = secondHalf[i];
}
return result;
}
}
Aggregations