Skip to content

Commit

Permalink
Pushing local changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jobarion committed Oct 11, 2015
1 parent e32c802 commit 5f0b97b
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/spleefleague/core/SpleefLeague.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.spleefleague.core.chat.ChatManager;
import com.spleefleague.core.command.BasicCommand;
import com.spleefleague.core.command.CommandLoader;
import com.spleefleague.core.halloween.Candy;
import com.spleefleague.core.halloween.HalloweenListener;
import com.spleefleague.core.io.Config;
import com.spleefleague.core.io.Settings;
import com.spleefleague.core.listeners.ChatListener;
Expand Down Expand Up @@ -76,7 +78,8 @@ public void start() {
EastereggListener.init();
InventoryMenuTemplateRepository.initTemplates();
Warp.initialize();

Candy.init();
HalloweenListener.init();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.spleefleague.core.player.SLPlayer;
import com.spleefleague.core.plugin.CorePlugin;
import com.spleefleague.core.utils.Warp;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;

public class warp extends BasicCommand {

Expand Down Expand Up @@ -64,9 +65,7 @@ private void teleportToWarp(Player p, String warpName) {
Warp warp = Warp.byName(warpName);

if (warp != null) {

//TODO: check if ingame?
p.teleport(warp.getLocation());
p.teleport(warp.getLocation(), TeleportCause.COMMAND);
success(p, "You have been teleported to '" + warp.getName() + "'");
}
else {
Expand Down
70 changes: 70 additions & 0 deletions src/main/java/com/spleefleague/core/halloween/Candy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.spleefleague.core.halloween;

import com.mongodb.client.MongoCursor;
import com.spleefleague.core.SpleefLeague;
import com.spleefleague.core.io.DBEntity;
import com.spleefleague.core.io.DBLoad;
import com.spleefleague.core.io.DBLoadable;
import com.spleefleague.core.io.EntityBuilder;
import com.spleefleague.core.io.TypeConverter;
import java.util.ArrayList;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.bukkit.Location;

/**
*
* @author Jonas
*/
public class Candy extends DBEntity implements DBLoadable{

@DBLoad(fieldName = "location", typeConverter = TypeConverter.LocationConverter.class)
private Location location;

public Location getLocation() {
return location;
}

private static Candy[] candies;

public static void init() {
ArrayList<Candy> candies = new ArrayList<>();
MongoCursor<Document> dbc = SpleefLeague.getInstance().getMongo().getDatabase("Halloween").getCollection("Candy").find().iterator();
while(dbc.hasNext()) {
candies.add(EntityBuilder.load(dbc.next(), Candy.class));
}
Candy.candies = candies.toArray(new Candy[0]);
}

public static Candy getCandy(ObjectId _id) {
for(Candy candy : getCandies()) {
if(candy.getObjectId().equals(_id)) {
return candy;
}
}
return null;
}

public static Candy[] getCandies() {
return candies;
}

public static class CandyObjectIdConverter extends TypeConverter<ObjectId, Candy> {

@Override
public Candy convertLoad(ObjectId t) {
return Candy.getCandy(t);
}

@Override
public ObjectId convertSave(Candy v) {
return v.getObjectId();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.spleefleague.core.halloween;

import com.spleefleague.core.SpleefLeague;
import com.spleefleague.core.chat.Theme;
import com.spleefleague.core.io.Settings;
import com.spleefleague.core.player.SLPlayer;
import com.spleefleague.core.utils.Area;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

/**
*
* @author Jonas
*/
public class HalloweenListener implements Listener{

private static Listener instance;
private final Area portalArea;
private final Location portalTarget;
private final PotionEffect positive, negative;

public static void init() {
if(instance == null) {
instance = new HalloweenListener();
Bukkit.getPluginManager().registerEvents(instance, SpleefLeague.getInstance());
}
}

private HalloweenListener() {
portalArea = Settings.get("halloween_portal_area", Area.class);
portalTarget = Settings.getLocation("halloween_portal_target");
positive = new PotionEffect(PotionEffectType.SPEED, 20 * 15, 1);
negative = new PotionEffect(PotionEffectType.SLOW, 20 * 15, 1);
}

@EventHandler
public void onInteract(PlayerInteractEvent event) {
if(event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getType() == Material.SKULL) {
for(Candy candy : Candy.getCandies()) {
if(candy.getLocation().equals(event.getClickedBlock().getLocation())) {
SLPlayer slp = SpleefLeague.getInstance().getPlayerManager().get(event.getPlayer());
if(!slp.getCandy().contains(candy)) {
slp.getCandy().add(candy);
slp.sendMessage(Theme.SUCCESS + "" + slp.getCandy().size() + "/" + Candy.getCandies().length);
if(Math.random() < 0.5) {
event.getPlayer().addPotionEffect(positive);
}
else {
event.getPlayer().addPotionEffect(negative);
}
}
break;
}
}
}
}

@EventHandler
public void onMove(PlayerMoveEvent event) {
SLPlayer slp = SpleefLeague.getInstance().getPlayerManager().get(event.getPlayer());
if(slp != null) {
if(slp.getCandy().size() >= Candy.getCandies().length) {
event.getPlayer().teleport(portalTarget);
}
}
}
}
20 changes: 10 additions & 10 deletions src/main/java/com/spleefleague/core/io/EntityBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,11 @@ public void set(Object instance, Object value) {
private void setField(Object instance, Object value) {
try {
Field f = super.field;
if (f.getType().isEnum() && value instanceof String) {
if (!f.getAnnotation(DBLoad.class).typeConverter().equals(TypeConverter.class)) {
TypeConverter tc = f.getAnnotation(DBLoad.class).typeConverter().newInstance();
f.set(instance, tc.convertLoad(value));
}
else if (f.getType().isEnum() && value instanceof String) {
f.set(instance, Enum.valueOf((Class<Enum>) f.getType(), (String) value));
}
else if (f.getType().isArray() && value instanceof List) {
Expand All @@ -391,10 +395,6 @@ else if (f.getType().isArray() && value instanceof List) {
}
f.set(instance, createGenericArray(array, f.getType().getComponentType()));
}
else if (!f.getAnnotation(DBLoad.class).typeConverter().equals(TypeConverter.class)) {
TypeConverter tc = f.getAnnotation(DBLoad.class).typeConverter().newInstance();
f.set(instance, tc.convertLoad(value));
}
else if (value instanceof Document && DBLoadable.class.isAssignableFrom(f.getType())) {
f.set(instance, deserialize((Document) value, f.getType()));
}
Expand All @@ -409,7 +409,11 @@ else if (value instanceof Document && DBLoadable.class.isAssignableFrom(f.getTyp
private void setMethod(Object instance, Object value) {
try {
Method m = super.method;
if (m.getParameterTypes()[0].isEnum() && value instanceof String) {
if (!m.getAnnotation(DBLoad.class).typeConverter().equals(TypeConverter.class)) {
TypeConverter tc = m.getAnnotation(DBLoad.class).typeConverter().newInstance();
m.invoke(instance, tc.convertLoad(value));
}
else if (m.getParameterTypes()[0].isEnum() && value instanceof String) {
m.invoke(instance, Enum.valueOf((Class<Enum>) m.getParameterTypes()[0], (String) value));
}
else if (m.getParameterTypes()[0].isArray() && value instanceof List) {
Expand All @@ -430,10 +434,6 @@ else if (m.getParameterTypes()[0].isArray() && value instanceof List) {
}
m.invoke(instance, createGenericArray(array, m.getParameterTypes()[0].getComponentType()));
}
else if (!m.getAnnotation(DBLoad.class).typeConverter().equals(TypeConverter.class)) {
TypeConverter tc = m.getAnnotation(DBLoad.class).typeConverter().newInstance();
m.invoke(instance, tc.convertLoad(value));
}
else if (value instanceof Document && DBLoadable.class.isAssignableFrom(m.getParameterTypes()[0])) {
m.invoke(instance, deserialize((Document) value, m.getParameterTypes()[0]));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,15 @@ public void onJoin(PlayerJoinEvent event) {
if(!player.hasPlayedBefore()) {
ChatManager.sendMessage(SpleefLeague.getInstance().getChatPrefix(), ChatColor.BLUE + "Welcome " + ChatColor.YELLOW + event.getPlayer().getName() + ChatColor.BLUE + " to SpleefLeague!", "DEFAULT");
}
for(SLPlayer slp : SpleefLeague.getInstance().getPlayerManager().getAll()) {
slp.getPlayer().setPlayerListName(slp.getRank().getColor() + slp.getName());
slp.getPlayer().setDisplayName(slp.getRank().getColor() + slp.getName());
}
Bukkit.getScheduler().runTaskLater(SpleefLeague.getInstance(), new Runnable() {
@Override
public void run() {
for(SLPlayer slp : SpleefLeague.getInstance().getPlayerManager().getAll()) {
slp.getPlayer().setPlayerListName(slp.getRank().getColor() + slp.getName());
slp.getPlayer().setDisplayName(slp.getRank().getColor() + slp.getName());
}
}
},20);
logIPAddress(event.getPlayer());
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/spleefleague/core/player/SLPlayer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.spleefleague.core.player;

import com.spleefleague.core.halloween.Candy;
import java.util.HashSet;
import java.util.UUID;
import com.spleefleague.core.io.DBLoad;
Expand All @@ -22,13 +23,17 @@ public class SLPlayer extends GeneralPlayer {
private HashSet<String> chatChannels;
@DBLoad(fieldName = "easteregg", typeConverter = HashSetIntegerConverter.class)
private HashSet<Integer> eastereggs;
@DBLoad(fieldName = "candies", typeConverter = Candy.CandyObjectIdConverter.class)
@DBSave(fieldName = "candies", typeConverter = Candy.CandyObjectIdConverter.class)
private HashSet<Candy> candies;
private String sendingChannel;
private PlayerState state = PlayerState.IDLE;

public SLPlayer() {
super();
chatChannels = new HashSet<>();
eastereggs = new HashSet<>();
candies = new HashSet<>();
}

@DBSave(fieldName = "rank", typeConverter = RankStringConverter.class)
Expand Down Expand Up @@ -62,6 +67,10 @@ public int getCoins() {
return coins;
}

public HashSet<Candy> getCandy() {
return candies;
}

@DBSave(fieldName = "easteregg", typeConverter = HashSetIntegerConverter.class)
public HashSet<Integer> getEastereggs() {
return eastereggs;
Expand Down

0 comments on commit 5f0b97b

Please sign in to comment.