1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 19:55:48 +00:00
Slimefun4/src/me/mrCookieSlime/Slimefun/AncientAltar/RitualAnimation.java

155 lines
4.1 KiB
Java
Raw Normal View History

2016-04-14 16:24:03 +00:00
package me.mrCookieSlime.Slimefun.AncientAltar;
import java.util.ArrayList;
2019-08-03 16:54:45 +00:00
import java.util.HashMap;
2016-04-14 16:24:03 +00:00
import java.util.List;
2019-08-03 16:54:45 +00:00
import java.util.Map;
2016-04-14 16:24:03 +00:00
import me.mrCookieSlime.Slimefun.SlimefunStartup;
import me.mrCookieSlime.Slimefun.listeners.AncientAltarListener;
import me.mrCookieSlime.Slimefun.Variables;
2016-04-14 16:24:03 +00:00
2019-08-03 16:54:45 +00:00
import org.bukkit.Effect;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Sound;
2016-04-14 16:24:03 +00:00
import org.bukkit.block.Block;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
public class RitualAnimation implements Runnable {
List<Block> altars;
2016-04-14 16:24:03 +00:00
Block altar;
Location l;
ItemStack output;
List<Block> pedestals;
List<ItemStack> items;
2016-04-14 16:24:03 +00:00
List<Location> particles;
2019-08-03 16:54:45 +00:00
Map<Item,Location> itemLock = new HashMap<>();
2016-04-14 16:24:03 +00:00
boolean running;
2018-03-02 19:40:45 +00:00
int stage;
2016-04-14 16:24:03 +00:00
public RitualAnimation(List<Block> altars, Block altar, Location drop, ItemStack output, List<Block> pedestals, List<ItemStack> items) {
this.l = drop;
this.altar = altar;
this.altars = altars;
this.output = output;
this.pedestals = pedestals;
this.items = items;
2019-08-25 20:21:06 +00:00
this.particles = new ArrayList<>();
2016-04-14 16:24:03 +00:00
this.running = true;
2018-03-02 19:40:45 +00:00
this.stage = 0;
2019-08-03 16:54:45 +00:00
for(Block ped:this.pedestals) {
Item itm = AncientAltarListener.findItem(ped);
this.itemLock.put(itm, itm.getLocation().clone());
}
2016-04-14 16:24:03 +00:00
}
@Override
public void run() {
idle();
2019-08-25 20:21:06 +00:00
2019-08-03 17:47:36 +00:00
if(!checkLockedItems()) {
2019-08-03 16:54:45 +00:00
abort();
2019-08-03 17:47:36 +00:00
return;
}
2019-08-25 20:21:06 +00:00
2018-03-02 19:40:45 +00:00
if(this.stage == 36) {
finish();
return;
}
2019-08-25 20:21:06 +00:00
2018-03-02 19:40:45 +00:00
if(this.stage > 0 && this.stage % 4 == 0) {
checkPedestal(pedestals.get(this.stage / 4 - 1));
}
2019-08-25 20:21:06 +00:00
2018-03-02 19:40:45 +00:00
this.stage += 1;
SlimefunStartup.instance.getServer().getScheduler().scheduleSyncDelayedTask(SlimefunStartup.instance, this, 8);
2016-04-14 16:24:03 +00:00
}
2019-08-03 16:54:45 +00:00
private boolean checkLockedItems() {
2019-08-25 20:21:06 +00:00
for (Item item : this.itemLock.keySet()) {
if (item.getLocation().distance(this.itemLock.get(item)) > 0.3) {
2019-08-03 16:54:45 +00:00
return false;
2019-08-25 20:21:06 +00:00
}
}
2019-08-03 16:54:45 +00:00
return true;
}
2016-04-14 16:24:03 +00:00
private void idle() {
try {
2018-09-04 15:28:41 +00:00
l.getWorld().spawnParticle(Particle.SPELL_WITCH, l,16, 1.2F, 0F, 1.2F);
l.getWorld().spawnParticle(Particle.FIREWORKS_SPARK,l,8, 0.2F, 0F, 0.2F);
2019-08-25 20:21:06 +00:00
2016-04-14 16:24:03 +00:00
for (Location l2: particles) {
2018-09-04 15:28:41 +00:00
l.getWorld().spawnParticle(Particle.ENCHANTMENT_TABLE, l2,16, 0.3F, 0.2F, 0.3F);
l.getWorld().spawnParticle(Particle.CRIT_MAGIC,l2,8, 0.3F, 0.2F, 0.3F);
2016-04-14 16:24:03 +00:00
}
} catch (Exception e) {
e.printStackTrace();
}
}
2016-04-14 16:24:03 +00:00
private void checkPedestal(Block pedestal) {
Item item = AncientAltarListener.findItem(pedestal);
2019-08-03 16:54:45 +00:00
if(item == null || itemLock.remove(item) == null) {
abort();
}
2016-04-14 16:24:03 +00:00
else {
particles.add(pedestal.getLocation().add(0.5, 1.5, 0.5));
items.add(AncientAltarListener.fixItemStack(item.getItemStack(), item.getCustomName()));
2018-09-04 09:33:57 +00:00
pedestal.getWorld().playSound(pedestal.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 5F, 2F);
2016-04-14 16:24:03 +00:00
try {
2018-09-04 15:28:41 +00:00
l.getWorld().spawnParticle(Particle.ENCHANTMENT_TABLE,pedestal.getLocation().add(0.5, 1.5, 0.5),16, 0.3F, 0.2F, 0.3F);
l.getWorld().spawnParticle(Particle.CRIT_MAGIC,pedestal.getLocation().add(0.5, 1.5, 0.5), 8,0.3F, 0.2F, 0.3F);
2016-04-14 16:24:03 +00:00
} catch (Exception e) {
e.printStackTrace();
}
2019-08-03 16:54:45 +00:00
itemLock.remove(item);
2016-04-14 16:24:03 +00:00
item.remove();
2019-08-03 16:54:45 +00:00
2016-04-14 16:24:03 +00:00
pedestal.removeMetadata("item_placed", SlimefunStartup.instance);
}
}
private void abort() {
running = false;
2018-08-06 19:13:46 +00:00
2019-08-25 20:21:06 +00:00
pedestals.forEach((pblock)-> {
Variables.altarinuse.remove(pblock.getLocation());
});
2018-08-06 19:13:46 +00:00
Variables.altarinuse.remove(altar.getLocation()); // should re-enable altar blocks on craft failure.
l.getWorld().playSound(l, Sound.ENTITY_ZOMBIE_ATTACK_IRON_DOOR, 5F, 1F);
2019-08-03 16:54:45 +00:00
itemLock.clear();
2016-04-14 16:24:03 +00:00
altars.remove(altar);
}
2018-08-06 19:13:46 +00:00
2016-04-14 16:24:03 +00:00
private void finish() {
if (running) {
l.getWorld().playSound(l, Sound.ENTITY_ZOMBIE_VILLAGER_CURE, 1F, 1F);
l.getWorld().playEffect(l, Effect.STEP_SOUND, Material.EMERALD_BLOCK);
l.getWorld().dropItemNaturally(l.add(0, 1, 0), output);
2018-08-06 19:13:46 +00:00
2018-07-09 14:41:22 +00:00
pedestals.forEach((pblock)->{
Variables.altarinuse.remove(pblock.getLocation());
});
Variables.altarinuse.remove(altar.getLocation()); // should re-enable altar blocks on craft completion.
altars.remove(altar);
}
else {
2018-09-04 09:33:57 +00:00
l.getWorld().playSound(l, Sound.ENTITY_ZOMBIE_BREAK_WOODEN_DOOR, 1F, 1F);
}
}
2019-08-03 16:54:45 +00:00
}