Skip to content

Instantly share code, notes, and snippets.

@riftrsps
Last active August 24, 2019 18:58
Show Gist options
  • Save riftrsps/57634ff1f4c8ac58ff7db2d03d3ffcd5 to your computer and use it in GitHub Desktop.
Save riftrsps/57634ff1f4c8ac58ff7db2d03d3ffcd5 to your computer and use it in GitHub Desktop.
Buff Package for MineCraft Server API
public class Buff {
private final String name;
private final int baseRadius;
private int currentRadius;
private boolean global;
public Buff(String name, int radius, boolean global) {
this.name = name;
this.baseRadius = radius;
this.currentRadius = radius;
this.global = global;
}
private Buff(Buff original) {
this.name = original.name;
this.baseRadius = original.baseRadius;
this.currentRadius = original.currentRadius;
this.global = original.global;
}
public void globalize() {
global = true;
}
public void localize() {
global = false;
}
public Buff copy() {
return new Buff(this);
}
@Override
public final String toString() {
return name;
}
}
package api.buff;
import api.util.Group; // https://gist.github.com/riftrsps/69ba3473c77c2334fcdaac2e605f4e91#file-group-java
public class BuffGroup extends Group<Buff> {
private final int baseLifespan;
private int currentLifespan;
public BuffGroup(Set<Buff> buffs, int lifespan) {
super(buffs
.stream()
.collect(Collectors.toMap(Buff::toString, Function.identity())));
this.lifespan = lifespan;
}
public void decreaseLifespan() {
currentLifespan--;
}
public boolean lifespanExpired() {
return currentLifespan == 0;
}
public BuffGroup copy() {
stream().map(Buff::copy).collect(Collectors.toMap(Buff::toString, Function.identity()));
}
public Set<Buff> globalBuffs() {
return stream().filter(Buff::isGlobal).collect(Collectors.toSet());
}
public Set<Buff> localBuffs() {
return stream().filter(Buff::isLocal).collect(Collectors.toSet());
}
}
package api.buff;
import api.YourAPI;
class BuffManager {
private final Map<Villager.Profession, BuffGroup> buffBaseMap;
private Map<Player, Map<Villager, BuffGroup>> activeLocalBuffs = new HashMap<>();
private Map<Villager, BuffGroup> activeGlobalBuffs = new HashMap<>();
public BuffManager(YourAPI api, Map<Villager.Profession, BuffGroup> buffBaseMap) {
this.api = api;
this.buffBaseMap = buffBaseMap;
}
public void update() {
clearUnusedGlobal();
clearUnusedLocal();
}
public void activateBuffs(Villager villager, Entity entity) {
Villager.Profession profession = villager.getProfession();
if(buffBaseMap.contains(profession)) {
BuffGroup buffs = buffBaseMap.get(profession).copy()
buffManager.activateGlobal(villager, buffs.globalBuffs());
if(entity instanceof Player) {
buffManager.activateLocal((Player) entity, villager, buffs.localBuffs());
}
}
}
private void clearUnusedGlobal() {
Iterator<Map.Entry<Villager, BuffGroup>> globalIterator = activeGlobalBuffs.iterator();
while(globalIterator.hasNext()) {
Map.Entry<Villager, BuffGroup> entry = globalIterator.next();
BuffGroup buffs = entry.value();
buffs.decreaseLifespan();
if(buffs.lifespanExpired()) {
boolean stillInRadius = ...;
if(stillInRadius)
buffs.refreshLifespan()
else
buffIterator.remove();
}
}
}
private void clearUnusedLocal() {
Iterator<Map.Entry<Player, Map<Villager, BuffGroup>>> localIterator = activeLocalBuffs.iterator();
while(localIterator.hasNext()) {
Map.Entry<Player, Map<Villager, BuffGroup>> entry = localIterator.next();
Map<Villager, BuffGroup> buffMap = entry.value();
Iterator<Map.Entry<Villager, BuffGroup>> buffIterator = buffMap.iterator();
while(buffIterator.hasNext()) {
BuffGroup buffs = buffIterator.next().value();
buffs.decreaseLifespan();
if(buffs.lifespanExpired()) {
boolean stillInRadius = ...;
if(stillInRadius)
buffs.refreshLifespan()
else
buffIterator.remove();
}
}
if(buffMap.isEmpty())
localIterator.remove();
}
}
}
package api.buff;
import api.YourAPI;
public class BuffModule {
private final BuffManager buffManager;
private final YourAPI api;
private BuffModule(YourAPI api, BuffManager buffManager) {
this.api = api;
this.buffManager = buffManager;
}
public void updatePeriodically() {
buffManager.update();
}
public static BuffModule load(YourAPI api) {
// load from yml
Map<Villager.Profession, BuffGroup> buffBaseMap = ...;
return new BuffModule(api, new BuffManager(api, buffBaseMap));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment