95 lines
2.4 KiB
Java
95 lines
2.4 KiB
Java
package nl.iobyte.themepark.api.ridecount;
|
|
|
|
import nl.iobyte.themepark.api.attraction.Attraction;
|
|
import nl.iobyte.themepark.api.events.ridecount.ChangeCountEvent;
|
|
import nl.iobyte.themepark.api.events.ridecount.PreProcessCountEvent;
|
|
import nl.iobyte.themepark.api.events.ridecount.SetCountEvent;
|
|
import org.bukkit.Bukkit;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.UUID;
|
|
|
|
public class AttractionCount {
|
|
|
|
private Attraction attraction;
|
|
private HashMap<UUID, RideCount> counts = new HashMap<>();
|
|
|
|
public AttractionCount(Attraction attraction) {
|
|
this.attraction = attraction;
|
|
}
|
|
|
|
public Attraction getAttraction() {
|
|
return attraction;
|
|
}
|
|
|
|
public void addCount(RideCount rideCount) {
|
|
if(rideCount == null)
|
|
return;
|
|
|
|
if(!attraction.getId().equals(rideCount.getAttraction().getId()))
|
|
return;
|
|
|
|
counts.put(rideCount.getUUID(), rideCount);
|
|
}
|
|
|
|
public boolean hasCount(UUID uuid) {
|
|
if(uuid == null)
|
|
return false;
|
|
|
|
return counts.containsKey(uuid);
|
|
}
|
|
|
|
public RideCount getCount(UUID uuid) {
|
|
if(!hasCount(uuid))
|
|
return null;
|
|
|
|
return counts.get(uuid);
|
|
}
|
|
|
|
public void removeCount(UUID uuid) {
|
|
if(!hasCount(uuid))
|
|
return;
|
|
|
|
counts.remove(uuid);
|
|
}
|
|
|
|
public void addCount(UUID uuid, int amount) {
|
|
if(amount < 1)
|
|
return;
|
|
|
|
int before = 0;
|
|
int after = amount;
|
|
RideCount count = getCount(uuid);
|
|
if(count == null) {
|
|
count = new RideCount(uuid, attraction, amount);
|
|
counts.put(uuid, count);
|
|
} else {
|
|
before = count.getCount();
|
|
after += before;
|
|
count.addCount(amount);
|
|
}
|
|
|
|
ChangeCountEvent e = new ChangeCountEvent(attraction, this, uuid, before, after);
|
|
Bukkit.getPluginManager().callEvent(e);
|
|
}
|
|
|
|
public void setCount(UUID uuid, int amount) {
|
|
RideCount count = getCount(uuid);
|
|
|
|
int before = 0;
|
|
if(count == null) {
|
|
count = new RideCount(uuid, attraction, amount);
|
|
counts.put(uuid, count);
|
|
} else {
|
|
if(count.getCount() == amount)
|
|
return;
|
|
|
|
before = count.getCount();
|
|
count.setCount(amount);
|
|
}
|
|
|
|
SetCountEvent e = new SetCountEvent(attraction, this, uuid, before, amount);
|
|
Bukkit.getPluginManager().callEvent(e);
|
|
}
|
|
|
|
}
|