Skip to content

Instantly share code, notes, and snippets.

@Exerosis
Created February 20, 2018 23:26
Show Gist options
  • Save Exerosis/5b5b21ac0554936b4ef66bc3fb7785ca to your computer and use it in GitHub Desktop.
Save Exerosis/5b5b21ac0554936b4ef66bc3fb7785ca to your computer and use it in GitHub Desktop.
package me.exerosis.example.alpha;
import org.bukkit.entity.LivingEntity;
public class BaseBoss implements Boss {
private final LivingEntity base;
private Stage stage;
public BaseBoss(LivingEntity base) {
this.base = base;
}
@Override
public LivingEntity base() {
return base;
}
@Override
public <Type extends Stage> Type stage(Type stage) {
this.stage.disable();
if (!base().isDead()) {
this.stage = stage;
stage.enable();
}
return stage;
}
@SuppressWarnings("unchecked")
@Override
public <Type extends Stage> Type stage() {
return (Type) stage;
}
}
package me.exerosis.example.alpha;
import org.bukkit.entity.LivingEntity;
public interface Boss {
LivingEntity base();
//--Stages--
<Type extends Stage> Type stage(Type stage);
<Type extends Stage> Type stage();
interface Stage {
void enable();
void disable();
}
}
package me.exerosis.example.alpha.stages;
import me.exerosis.example.alpha.Boss;
public class AbilityStage implements Boss.Stage {
private final Boss.Stage[] stages;
public AbilityStage(Boss.Stage... stages) {
this.stages = stages;
}
@Override
public void enable() {
for (Boss.Stage stage : stages) {
stage.enable();
}
}
@Override
public void disable() {
for (Boss.Stage stage : stages) {
stage.disable();
}
}
}
package me.exerosis.example.alpha.stages;
public class AttributesAbility {
}
package me.exerosis.example.alpha.stages;
import com.google.common.collect.Range;
import com.google.common.collect.RangeMap;
import me.exerosis.example.alpha.Boss;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.plugin.Plugin;
import static com.google.common.collect.TreeRangeMap.create;
import static org.bukkit.Bukkit.getPluginManager;
import static org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH;
import static org.bukkit.event.EventPriority.MONITOR;
import static org.bukkit.event.HandlerList.unregisterAll;
public class HealthStage implements Listener, Boss.Stage {
private final RangeMap<Double, Boss.Stage> stages = create();
private final Plugin plugin;
private final Boss boss;
private Boss.Stage stage;
public HealthStage(Plugin plugin, Boss boss) {
this.plugin = plugin;
this.boss = boss;
}
public HealthStage add(Range<Double> range, Boss.Stage stage) {
stages.put(range, stage);
return this;
}
public void remove(Range<Double> range) {
stages.remove(range);
}
@Override
public void enable() {
getPluginManager().registerEvents(this, plugin);
setStage(stages.get(asPercent(boss.base().getHealth())));
}
@Override
public void disable() {
unregisterAll(this);
stage.disable();
stage = null;
}
@EventHandler(priority = MONITOR, ignoreCancelled = true)
void onDamage(EntityDamageEvent event) {
if (!event.getEntity().equals(boss))
return;
setStage(stages.get(asPercent(boss.base().getHealth() - event.getFinalDamage())));
}
private double asPercent(double health) {
return health / boss.base().getAttribute(GENERIC_MAX_HEALTH).getValue();
}
private void setStage(Boss.Stage stage) {
if (this.stage != null)
this.stage.disable();
this.stage = stage;
stage.enable();
}
}
package me.exerosis.example.alpha.test;
import me.exerosis.example.alpha.BaseBoss;
import me.exerosis.example.alpha.stages.AbilityStage;
import me.exerosis.example.alpha.stages.HealthStage;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.plugin.Plugin;
import static com.google.common.collect.Range.greaterThan;
import static com.google.common.collect.Range.singleton;
import static org.bukkit.entity.EntityType.ZOMBIE;
public class ZombieBoss extends BaseBoss {
public static final double DYING = 0.0;
public static final double RUNNING = 0.50;
public ZombieBoss(Plugin plugin, Location location) {
super((LivingEntity) location.getWorld().spawnEntity(location, ZOMBIE));
Stage shooting = new AbilityStage();
Stage running = new AbilityStage();
Stage dying = new AbilityStage();
stage(new HealthStage(plugin, this))
.add(singleton(DYING), dying)
.add(greaterThan(DYING), running)
.add(greaterThan(RUNNING), shooting);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment