Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the ability to change respawn behavior with the plugin
- Loading branch information
1 parent
de8098a
commit b5f50b4
Showing
8 changed files
with
226 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
API/src/main/java/com/bgsoftware/superiorskyblock/api/player/respawn/RespawnAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.bgsoftware.superiorskyblock.api.player.respawn; | ||
|
||
import com.bgsoftware.superiorskyblock.api.objects.Enumerable; | ||
import com.google.common.base.Preconditions; | ||
import org.bukkit.event.player.PlayerRespawnEvent; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public abstract class RespawnAction implements Enumerable { | ||
|
||
private static final Map<String, RespawnAction> respawnActions = new HashMap<>(); | ||
private static int ordinalCounter = 0; | ||
|
||
private final String name; | ||
private final int ordinal; | ||
|
||
protected RespawnAction(String name) { | ||
Preconditions.checkArgument(!respawnActions.containsKey(name), "name already exists."); | ||
this.name = name.toUpperCase(Locale.ENGLISH); | ||
this.ordinal = ordinalCounter++; | ||
} | ||
|
||
@Override | ||
public final int ordinal() { | ||
return this.ordinal; | ||
} | ||
|
||
/** | ||
* Get the name of the respawn action. | ||
*/ | ||
public final String getName() { | ||
return name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "RespawnAction{name=" + name + "}"; | ||
} | ||
|
||
public abstract boolean canPerform(PlayerRespawnEvent event); | ||
|
||
public abstract void perform(PlayerRespawnEvent event); | ||
|
||
/** | ||
* Get all the respawn actions. | ||
*/ | ||
public static Collection<RespawnAction> values() { | ||
return respawnActions.values(); | ||
} | ||
|
||
/** | ||
* Get a respawn action by its name. | ||
* | ||
* @param name The name to check. | ||
*/ | ||
public static RespawnAction getByName(String name) { | ||
Preconditions.checkNotNull(name, "name parameter cannot be null."); | ||
|
||
RespawnAction respawnAction = respawnActions.get(name.toUpperCase(Locale.ENGLISH)); | ||
|
||
Preconditions.checkNotNull(respawnAction, "Couldn't find an RespawnAction with the name " + name + "."); | ||
|
||
return respawnAction; | ||
} | ||
|
||
/** | ||
* Register a new respawn action. | ||
* | ||
* @param respawnAction The respawn action to register. | ||
*/ | ||
public static void register(RespawnAction respawnAction) { | ||
Preconditions.checkNotNull(respawnAction, "respawnAction parameter cannot be null."); | ||
|
||
String name = respawnAction.getName().toUpperCase(Locale.ENGLISH); | ||
|
||
Preconditions.checkState(!respawnActions.containsKey(name), "RespawnAction with the name " + name + " already exists."); | ||
|
||
respawnActions.put(name, respawnAction); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/com/bgsoftware/superiorskyblock/player/respawn/RespawnActions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.bgsoftware.superiorskyblock.player.respawn; | ||
|
||
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin; | ||
import com.bgsoftware.superiorskyblock.api.player.respawn.RespawnAction; | ||
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.player.PlayerRespawnEvent; | ||
|
||
public class RespawnActions { | ||
|
||
private static final SuperiorSkyblockPlugin plugin = SuperiorSkyblockPlugin.getPlugin(); | ||
|
||
public static final RespawnAction BED_TELEPORT = register(new RespawnAction("BED_TELEPORT") { | ||
|
||
@Override | ||
public boolean canPerform(PlayerRespawnEvent event) { | ||
Player player = event.getPlayer(); | ||
return player.getBedSpawnLocation() != null; | ||
} | ||
|
||
@Override | ||
public void perform(PlayerRespawnEvent event) { | ||
event.setRespawnLocation(event.getPlayer().getBedSpawnLocation()); | ||
} | ||
|
||
}); | ||
|
||
public static final RespawnAction ISLAND_TELEPORT = register(new RespawnAction("ISLAND_TELEPORT") { | ||
|
||
@Override | ||
public boolean canPerform(PlayerRespawnEvent event) { | ||
SuperiorPlayer superiorPlayer = plugin.getPlayers().getSuperiorPlayer(event.getPlayer()); | ||
return superiorPlayer.getIsland() != null; | ||
} | ||
|
||
@Override | ||
public void perform(PlayerRespawnEvent event) { | ||
SuperiorPlayer superiorPlayer = plugin.getPlayers().getSuperiorPlayer(event.getPlayer()); | ||
assert superiorPlayer.getIsland() != null; | ||
superiorPlayer.teleport(superiorPlayer.getIsland()); | ||
} | ||
|
||
}); | ||
|
||
public static final RespawnAction SPAWN_TELEPORT = register(new RespawnAction("SPAWN_TELEPORT") { | ||
|
||
@Override | ||
public boolean canPerform(PlayerRespawnEvent event) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void perform(PlayerRespawnEvent event) { | ||
SuperiorPlayer superiorPlayer = plugin.getPlayers().getSuperiorPlayer(event.getPlayer()); | ||
superiorPlayer.teleport(plugin.getGrid().getSpawnIsland()); | ||
} | ||
|
||
}); | ||
|
||
public static final RespawnAction VANILLA = register(new RespawnAction("VANILLA") { | ||
|
||
@Override | ||
public boolean canPerform(PlayerRespawnEvent event) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void perform(PlayerRespawnEvent event) { | ||
// Do nothing, let vanilla do its thing. | ||
} | ||
|
||
}); | ||
|
||
private RespawnActions() { | ||
|
||
} | ||
|
||
public static void registerActions() { | ||
// Do nothing, only trigger all the register calls | ||
} | ||
|
||
private static RespawnAction register(RespawnAction respawnAction) { | ||
RespawnAction.register(respawnAction); | ||
return respawnAction; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters