use of org.bukkit.inventory.InventoryHolder in project Minigames by AddstarMC.
the class RollbackScheduler method run.
@Override
public void run() {
long time = System.nanoTime();
while (iterator.hasNext()) {
BlockData bdata = iterator.next();
bdata.getBlockState().update(true);
if (System.nanoTime() - time > Minigames.plugin.getConfig().getDouble("regeneration.maxDelay") * 1000000)
return;
}
while (physIterator.hasNext()) {
BlockData bdata = physIterator.next();
bdata.getBlockState().update(true);
if ((bdata.getBlockState().getType() == Material.SIGN_POST || bdata.getBlockState().getType() == Material.WALL_SIGN) && bdata.getBlockState() instanceof Sign) {
Sign sign = (Sign) bdata.getLocation().getBlock().getState();
Sign signOld = (Sign) bdata.getBlockState();
sign.setLine(0, signOld.getLine(0));
sign.setLine(1, signOld.getLine(1));
sign.setLine(2, signOld.getLine(2));
sign.setLine(3, signOld.getLine(3));
sign.update();
} else if (bdata.getLocation().getBlock().getState() instanceof InventoryHolder) {
InventoryHolder block = (InventoryHolder) bdata.getLocation().getBlock().getState();
if (bdata.getItems() != null)
block.getInventory().setContents(bdata.getItems().clone());
} else if (bdata.getBlockState() instanceof FlowerPot) {
FlowerPot pot = (FlowerPot) bdata.getLocation().getBlock().getState();
if (bdata.getSpecialData("contents") != null)
pot.setContents((MaterialData) bdata.getSpecialData("contents"));
} else if (bdata.getBlockState().getType() == Material.JUKEBOX) {
Jukebox jbox = (Jukebox) bdata.getLocation().getBlock().getState();
Jukebox orig = (Jukebox) bdata.getBlockState();
jbox.setPlaying(orig.getPlaying());
jbox.update();
} else if (bdata.getBlockState().getType() == Material.SKULL) {
Skull skull = (Skull) bdata.getBlockState().getBlock().getState();
Skull orig = (Skull) bdata.getBlockState();
if (orig.getOwningPlayer() != null)
skull.setOwningPlayer(orig.getOwningPlayer());
skull.setRotation(orig.getRotation());
skull.setSkullType(orig.getSkullType());
skull.update();
}
if (System.nanoTime() - time > Minigames.plugin.getConfig().getDouble("regeneration.maxDelay") * 1000000)
return;
}
// When rolling back a single player's changes dont change the overall games state
if (modifier == null) {
HandlerList.unregisterAll(minigame.getBlockRecorder());
HandlerList.bakeAll();
minigame.setState(MinigameState.IDLE);
}
task.cancel();
}
use of org.bukkit.inventory.InventoryHolder in project Minigames by AddstarMC.
the class BasicRecorder method takeItem.
@EventHandler(ignoreCancelled = true)
private void takeItem(PlayerInteractEvent event) {
MinigamePlayer ply = pdata.getMinigamePlayer(event.getPlayer());
if (ply == null)
return;
if (ply.isInMinigame() && event.getAction() == Action.RIGHT_CLICK_BLOCK && !ply.getMinigame().isSpectator(ply)) {
Minigame mgm = ply.getMinigame();
RecorderData d = mgm.getBlockRecorder();
if (event.getClickedBlock().getState() instanceof InventoryHolder) {
d.addBlock(event.getClickedBlock().getLocation().getBlock(), ply);
}
}
}
use of org.bukkit.inventory.InventoryHolder in project Glowstone by GlowstoneMC.
the class RandomItemsContent method fillContainer.
public boolean fillContainer(Random random, DirectionalContainer container, BlockState state, int maxStacks) {
if (state.getBlock().getState() instanceof InventoryHolder) {
Inventory inventory = ((InventoryHolder) state.getBlock().getState()).getInventory();
int size = inventory.getSize();
for (int i = 0; i < maxStacks; i++) {
RandomAmountItem item = getRandomItem(random);
if (item != null) {
for (ItemStack stack : item.getItemStacks(random)) {
// slot can be overriden hence maxStacks can be less than what's expected
inventory.setItem(random.nextInt(size), stack);
}
}
}
} else {
throw new IllegalArgumentException("Bad container type");
}
return true;
}
use of org.bukkit.inventory.InventoryHolder in project BKCommonLib by bergerhealer.
the class CommonEntity method replaceEntity.
@SuppressWarnings("unchecked")
private void replaceEntity(final EntityHandle newInstance) {
final EntityHandle oldInstance = this.handle;
if (oldInstance.getRaw() == newInstance.getRaw()) {
throw new RuntimeException("Can not replace an entity with itself!");
}
// Reset entity state
oldInstance.setDead(true);
newInstance.setDead(false);
oldInstance.setValid(false);
newInstance.setValid(true);
// *** Bukkit Entity ***
CraftEntityHandle craftEntity = CraftEntityHandle.createHandle(this.entity);
craftEntity.setHandle(newInstance);
if (entity instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) entity).getInventory();
if (CraftInventoryHandle.T.isAssignableFrom(inv)) {
CraftInventoryHandle cInv = CraftInventoryHandle.createHandle(inv);
if (IInventoryHandle.T.isAssignableFrom(newInstance.getRaw())) {
IInventoryHandle iinvHandle = IInventoryHandle.createHandle(newInstance.getRaw());
cInv.setHandleField(iinvHandle);
}
}
}
// *** Give the old entity a new Bukkit Entity ***
oldInstance.setBukkitEntityField(CraftEntityHandle.createCraftEntity(Bukkit.getServer(), oldInstance));
this.handle = newInstance;
// *** Replace entity in passenger and vehicle fields ***
EntityHandle vehicle = newInstance.getVehicle();
if (vehicle != null) {
replaceInList(vehicle.getPassengers(), newInstance);
}
if (newInstance.getPassengers() != null) {
for (EntityHandle passenger : newInstance.getPassengers()) {
if (oldInstance.getRaw() == passenger.getVehicle().getRaw()) {
passenger.setVehicle(newInstance);
}
}
}
// *** DataWatcher field of the old Entity ***
Object dataWatcher = EntityHandle.T.datawatcherField.raw.get(newInstance.getRaw());
if (dataWatcher != null) {
DataWatcherHandle.T.owner.set(dataWatcher, newInstance);
}
// *** Perform further replacement all over the place in the server ***
replaceEntityInServer(oldInstance, newInstance);
// *** Repeat the replacement in the server the next tick to make sure nothing lingers ***
CommonUtil.nextTick(new Runnable() {
@Override
public void run() {
replaceEntityInServer(oldInstance, newInstance);
}
});
// *** Make sure a controller is set when hooked ***
if (this.isHooked()) {
DefaultEntityController controller = new DefaultEntityController();
controller.bind(this);
}
}
use of org.bukkit.inventory.InventoryHolder in project Essentials by drtshock.
the class EssentialsPlayerListener method onInventoryClickEvent.
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onInventoryClickEvent(final InventoryClickEvent event) {
Player refreshPlayer = null;
final Inventory top = event.getView().getTopInventory();
final InventoryType type = top.getType();
if (type == InventoryType.PLAYER) {
final User user = ess.getUser((Player) event.getWhoClicked());
final InventoryHolder invHolder = top.getHolder();
if (invHolder != null && invHolder instanceof HumanEntity) {
final User invOwner = ess.getUser((Player) invHolder);
if (user.isInvSee() && (!user.isAuthorized("essentials.invsee.modify") || invOwner.isAuthorized("essentials.invsee.preventmodify") || !invOwner.getBase().isOnline())) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
}
} else if (type == InventoryType.ENDER_CHEST) {
final User user = ess.getUser((Player) event.getWhoClicked());
if (user.isEnderSee() && (!user.isAuthorized("essentials.enderchest.modify"))) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
} else if (type == InventoryType.WORKBENCH) {
User user = ess.getUser((Player) event.getWhoClicked());
if (user.isRecipeSee()) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
} else if (type == InventoryType.CHEST && top.getSize() == 9) {
final User user = ess.getUser((Player) event.getWhoClicked());
final InventoryHolder invHolder = top.getHolder();
if (invHolder != null && invHolder instanceof HumanEntity && user.isInvSee()) {
event.setCancelled(true);
refreshPlayer = user.getBase();
}
}
if (refreshPlayer != null) {
final Player player = refreshPlayer;
ess.scheduleSyncDelayedTask(new Runnable() {
@Override
public void run() {
player.updateInventory();
}
}, 1);
}
}
Aggregations