-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: applenick <[email protected]>
- Loading branch information
Showing
5 changed files
with
195 additions
and
0 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
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,23 @@ | ||
package tc.oc.pgm.enderchest; | ||
|
||
import tc.oc.pgm.api.filter.Filter; | ||
import tc.oc.pgm.api.region.Region; | ||
|
||
public class Dropoff { | ||
|
||
private final Region region; | ||
private final Filter filter; | ||
|
||
public Dropoff(Region region, Filter filter) { | ||
this.region = region; | ||
this.filter = filter; | ||
} | ||
|
||
public Region getRegion() { | ||
return region; | ||
} | ||
|
||
public Filter getFilter() { | ||
return filter; | ||
} | ||
} |
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,8 @@ | ||
package tc.oc.pgm.enderchest; | ||
|
||
/** what behavior enderchests will fallback on in the absence of dropoff locations * */ | ||
public enum DropoffFallback { | ||
KEEP, | ||
DELETE, | ||
AUTO; | ||
} |
90 changes: 90 additions & 0 deletions
90
core/src/main/java/tc/oc/pgm/enderchest/EnderChestMatchModule.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,90 @@ | ||
package tc.oc.pgm.enderchest; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import java.util.List; | ||
import org.bukkit.Location; | ||
import org.bukkit.Material; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.inventory.Inventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import tc.oc.pgm.api.match.Match; | ||
import tc.oc.pgm.api.match.MatchModule; | ||
import tc.oc.pgm.api.match.MatchScope; | ||
import tc.oc.pgm.api.party.Competitor; | ||
import tc.oc.pgm.api.party.Party; | ||
import tc.oc.pgm.events.ListenerScope; | ||
import tc.oc.pgm.events.PlayerJoinMatchEvent; | ||
import tc.oc.pgm.events.PlayerPartyChangeEvent; | ||
|
||
@ListenerScope(MatchScope.LOADED) | ||
public class EnderChestMatchModule implements MatchModule, Listener { | ||
|
||
private final Match match; | ||
private final boolean enabled; | ||
private final DropoffFallback fallback; | ||
|
||
private final ImmutableList<Dropoff> dropoffs; | ||
|
||
public EnderChestMatchModule( | ||
Match match, boolean enabled, List<Dropoff> dropoffs, DropoffFallback fallback) { | ||
this.match = match; | ||
this.enabled = enabled; | ||
this.dropoffs = ImmutableList.copyOf(dropoffs); | ||
this.fallback = fallback; | ||
} | ||
|
||
public boolean isEnabled() { | ||
return enabled; | ||
} | ||
|
||
@EventHandler | ||
public void onPlayerJoin(PlayerJoinMatchEvent event) { | ||
event.getPlayer().getBukkit().getEnderChest().clear(); | ||
} | ||
|
||
@EventHandler | ||
public void onParticipantLeave(PlayerPartyChangeEvent event) { | ||
if (!isEnabled()) return; | ||
if (dropoffs.isEmpty()) return; | ||
Party oldParty = event.getOldParty(); | ||
if (!(oldParty instanceof Competitor)) return; | ||
|
||
Inventory enderchest = event.getPlayer().getBukkit().getEnderChest(); | ||
|
||
boolean dropped = false; | ||
for (Dropoff dropoff : dropoffs) { | ||
if (dropoff.getFilter().query(oldParty).isAllowed()) { | ||
drop( | ||
enderchest, | ||
dropoff.getRegion().getRandom(match.getRandom()).toLocation(match.getWorld())); | ||
dropped = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!dropped) { | ||
switch (fallback) { | ||
case AUTO: | ||
if (dropoffs.isEmpty()) { | ||
enderchest.clear(); | ||
} | ||
break; | ||
case DELETE: | ||
enderchest.clear(); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
private void drop(Inventory inventory, Location location) { | ||
for (ItemStack item : inventory.getContents()) { | ||
if (item != null && item.getType() != Material.AIR) { | ||
location.getWorld().dropItem(location, item); | ||
} | ||
} | ||
inventory.clear(); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
core/src/main/java/tc/oc/pgm/enderchest/EnderChestModule.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,71 @@ | ||
package tc.oc.pgm.enderchest; | ||
|
||
import com.google.common.collect.Lists; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
import org.jdom2.Document; | ||
import org.jdom2.Element; | ||
import tc.oc.pgm.api.filter.Filter; | ||
import tc.oc.pgm.api.map.MapModule; | ||
import tc.oc.pgm.api.map.factory.MapFactory; | ||
import tc.oc.pgm.api.map.factory.MapModuleFactory; | ||
import tc.oc.pgm.api.match.Match; | ||
import tc.oc.pgm.api.match.MatchModule; | ||
import tc.oc.pgm.api.region.Region; | ||
import tc.oc.pgm.filters.parse.FilterParser; | ||
import tc.oc.pgm.regions.RandomPointsValidation; | ||
import tc.oc.pgm.regions.RegionParser; | ||
import tc.oc.pgm.util.xml.InvalidXMLException; | ||
import tc.oc.pgm.util.xml.Node; | ||
import tc.oc.pgm.util.xml.XMLUtils; | ||
|
||
public class EnderChestModule implements MapModule { | ||
|
||
private final boolean enabled; | ||
private final List<Dropoff> dropoffs; | ||
private final DropoffFallback fallback; | ||
|
||
public EnderChestModule(boolean enabled, List<Dropoff> dropoffs, DropoffFallback fallback) { | ||
this.enabled = enabled; | ||
this.dropoffs = dropoffs; | ||
this.fallback = fallback; | ||
} | ||
|
||
@Override | ||
public MatchModule createMatchModule(Match match) { | ||
return new EnderChestMatchModule(match, enabled, dropoffs, fallback); | ||
} | ||
|
||
public static class Factory implements MapModuleFactory<EnderChestModule> { | ||
@Override | ||
public EnderChestModule parse(MapFactory factory, Logger logger, Document doc) | ||
throws InvalidXMLException { | ||
FilterParser filters = factory.getFilters(); | ||
RegionParser regions = factory.getRegions(); | ||
|
||
boolean enabled = false; | ||
DropoffFallback fallback = DropoffFallback.AUTO; | ||
List<Dropoff> dropoffs = Lists.newArrayList(); | ||
|
||
for (Element enderchestEl : doc.getRootElement().getChildren("enderchest")) { | ||
fallback = | ||
XMLUtils.parseEnum( | ||
Node.fromAttr(enderchestEl, "fallback"), | ||
DropoffFallback.class, | ||
"fallback", | ||
DropoffFallback.AUTO); | ||
enabled = true; | ||
} | ||
|
||
for (Element dropoffEl : | ||
XMLUtils.flattenElements(doc.getRootElement(), "enderchest", "dropoff")) { | ||
Region region = | ||
regions.parseRequiredProperty(dropoffEl, "region", RandomPointsValidation.INSTANCE); | ||
Filter filter = filters.parseRequiredProperty(dropoffEl, "filter"); | ||
dropoffs.add(new Dropoff(region, filter)); | ||
} | ||
|
||
return new EnderChestModule(enabled, dropoffs, fallback); | ||
} | ||
} | ||
} |