Created
December 10, 2020 18:14
-
-
Save shwang/4c36fb80f10e959c4d455d86a7290122 to your computer and use it in GitHub Desktop.
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
package io.github.herobraine.experiments; | |
import java.util.Random; | |
import org.bukkit.Difficulty; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.Statistic; | |
import org.bukkit.block.Block; | |
import org.bukkit.block.Biome; | |
import org.bukkit.entity.Player; | |
import org.bukkit.event.EventHandler; | |
import org.bukkit.inventory.ItemStack; | |
import org.bukkit.event.block.BlockPlaceEvent; | |
import org.json.simple.JSONObject; | |
import org.json.simple.JSONArray; | |
import io.github.herobraine.bundles.PlayerBundle; | |
import io.github.herobraine.bundles.WorldBundle; | |
import io.github.herobraine.apis.TeleportApis; | |
public class FindCavesExperiment extends Experiment{ | |
private int[] cmsTraveled; | |
private int[] caveLocation; | |
public FindCavesExperiment() { | |
} | |
public FindCavesExperiment(WorldBundle b) { | |
super(b); | |
} | |
@Override | |
protected void initData() { | |
cmsTraveled = new int[getPlayers().size()]; | |
for (int i = 0; i < getPlayers().size(); i++) | |
cmsTraveled[i] = getPlayers().get(i).getPlayer().getStatistic(Statistic.WALK_ONE_CM) | |
+ getPlayers().get(i).getPlayer().getStatistic(Statistic.SPRINT_ONE_CM) | |
+ getPlayers().get(i).getPlayer().getStatistic(Statistic.CROUCH_ONE_CM) | |
+ getPlayers().get(i).getPlayer().getStatistic(Statistic.FALL_ONE_CM) | |
+ getPlayers().get(i).getPlayer().getStatistic(Statistic.CLIMB_ONE_CM); | |
caveLocation = new int[3]; | |
} | |
@Override | |
protected void finalizeData() { | |
for (int i = 0; i < getPlayers().size(); i++) { | |
JSONObject pData = new JSONObject(); | |
Player p = getPlayers().get(i).getPlayer(); | |
pData.put("distance_travelled", | |
p.getStatistic(Statistic.WALK_ONE_CM) + p.getStatistic(Statistic.SPRINT_ONE_CM) | |
+ p.getStatistic(Statistic.CROUCH_ONE_CM) + p.getStatistic(Statistic.FALL_ONE_CM) | |
+ p.getStatistic(Statistic.CLIMB_ONE_CM) - cmsTraveled[i]); | |
for (int inv = 0; inv < p.getInventory().getSize(); inv++) { | |
if (p.getInventory().getContents()[inv] != null) | |
pData.put("inv_" + inv, p.getInventory().getContents()[inv].getType().toString()); | |
} | |
addData(p.getUniqueId().toString(), pData); | |
} | |
JSONArray cave_loc = new JSONArray(); | |
for (int x : caveLocation) { | |
cave_loc.add(x); | |
} | |
addData("cave_location", cave_loc); | |
} | |
@Override | |
public String getExperimentName() { | |
return "findcaves"; | |
} | |
@Override | |
protected void prepPlayerForSolo(PlayerBundle b) { | |
super.prepPlayerForSolo(b); | |
b.getPlayer().getInventory().setItem(0, new ItemStack(Material.DIAMOND_BLOCK, 1)); | |
} | |
@Override | |
protected void prepWorldForSolo() { | |
// TODO: Maybe keep track of player block place/break events | |
// or maybe even the player killing mobs, and undo them. | |
// 50/50 on whether this is necessary. | |
getExperimentWorld().setTime((long) 0); | |
getExperimentWorld().setDifficulty(Difficulty.PEACEFUL); | |
getExperimentWorld().setStorm(false); | |
getExperimentWorld().setThundering(false); | |
} | |
@Override | |
protected void resetWorldForSolo() { | |
cmsTraveled = null; | |
caveLocation = new int[3]; | |
// FIXME: If this is set to null, it causes a NullPointerException | |
// in `onBlockPlace()` where caveLocation is null, even though it | |
// is always set to `new int[3]` in `initData()`, which is always | |
// run before the experiment starts and the listener can trigger. | |
} | |
@Override | |
protected void teleportPlayerForMultiplayer(PlayerBundle b) { | |
teleportPlayerForSolo(b); | |
} | |
@Override | |
protected void teleportPlayerForSolo(PlayerBundle b) { | |
Random random = new Random(); | |
int x = random.nextInt(4001) - 2000; | |
int z = random.nextInt(4001) - 2000; | |
// Check that the spawn location is in a "clear enough" area - i.e. | |
// flat plains in a small radius around the player spawn. | |
while (getExperimentWorld().getBiome(x, z) != Biome.PLAINS && | |
getExperimentWorld().getBiome(x + 100, z) != Biome.PLAINS && | |
getExperimentWorld().getBiome(x - 100, z) != Biome.PLAINS && | |
getExperimentWorld().getBiome(x, z + 100) != Biome.PLAINS && | |
getExperimentWorld().getBiome(x, z - 100) != Biome.PLAINS) { | |
x = random.nextInt(4001) - 2000; | |
z = random.nextInt(4001) - 2000; | |
} | |
Location l = new Location(getExperimentWorld(), x, 0, z); | |
l.setY(getExperimentWorld().getHighestBlockYAt(l)); | |
TeleportApis.teleport(b, l); | |
} | |
@EventHandler | |
public void onBlockPlace(BlockPlaceEvent e) { | |
Block target = e.getBlockPlaced(); | |
if (target.getType() == Material.DIAMOND_BLOCK) { | |
Location target_loc = target.getLocation(); | |
caveLocation[0] = target_loc.getBlockX(); | |
caveLocation[1] = target_loc.getBlockY(); | |
caveLocation[2] = target_loc.getBlockZ(); | |
e.setCancelled(true); | |
endExperiment(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment