Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vents and Fences #167

Merged
merged 4 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,26 @@ public static void clearDirtFromMazePart(PrisonEscapeLocation upperCorner, Priso
}
}

// ########################################
// # Obstacle #
// ########################################

public static void fillWithBars(PrisonEscapeLocation upperCorner, PrisonEscapeLocation lowerCorner) {
fill(upperCorner, lowerCorner, Material.IRON_BARS);
}

public static void putVent(PrisonEscapeLocation location) {
WORLD.getBlockAt(location.getX(), location.getY(), location.getZ()).setType(Material.IRON_TRAPDOOR);
}

// ########################################
// # Util #
// ########################################

public static void clear(PrisonEscapeLocation upperCorner, PrisonEscapeLocation lowerCorner) {
fill(upperCorner, lowerCorner, Material.AIR);
}

private static void fill(PrisonEscapeLocation upperCorner, PrisonEscapeLocation lowerCorner, Material type) {
for (int x = lowerCorner.getX(); x <= upperCorner.getX(); x++) {
for (int y = lowerCorner.getY(); y <= upperCorner.getY(); y++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.tiagofar78.prisonescape.game.prisonbuilding.Obstacle;
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonBuilding;
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;
import net.tiagofar78.prisonescape.game.prisonbuilding.Regenerable;
import net.tiagofar78.prisonescape.game.prisonbuilding.Vault;
import net.tiagofar78.prisonescape.game.prisonbuilding.WallCrack;
import net.tiagofar78.prisonescape.items.FunctionalItem;
Expand Down Expand Up @@ -835,17 +836,20 @@ public int playerFixWallCrack(PrisonEscapePlayer player, WallCrack crack) {

public int obstacleTookDamage(PrisonEscapePlayer player, Obstacle obstacle, Item item) {
if (!_prisionersTeam.isOnTeam(player)) {
if (obstacle instanceof Regenerable) {
((Regenerable) obstacle).regenerate();
return 0;
}

return -1;
}

if (!item.isTool()) {
return 0; // TODO maybe send a message to use right item
return 0;
}

double returnCode = obstacle.takeDamage((ToolItem) item);
if (returnCode == -1) {
// TODO maybe send a message to use right item
} else if (returnCode == 0) {
if (returnCode == 0) {
obstacle.removeFromWorld();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package net.tiagofar78.prisonescape.game.prisonbuilding;

import net.tiagofar78.prisonescape.bukkit.BukkitWorldEditor;
import net.tiagofar78.prisonescape.items.ToolItem;
import net.tiagofar78.prisonescape.items.WireCutterItem;

public class Fence extends Obstacle implements Regenerable {

private PrisonEscapeLocation _upperCornerLocation;
private PrisonEscapeLocation _lowerCornerLocation;
private boolean _isDestroyed;

public Fence(PrisonEscapeLocation upperCornerLocation, PrisonEscapeLocation lowerCornerLocation) {
_upperCornerLocation = upperCornerLocation;
_lowerCornerLocation = lowerCornerLocation;
_isDestroyed = false;
generate();
}

@Override
public boolean isEffectiveTool(ToolItem tool) {
return tool instanceof WireCutterItem;
}

@Override
public boolean contains(PrisonEscapeLocation location) {
return isBetweenCorners(location.getX(), location.getY(), location.getZ());
}

private boolean isBetweenCorners(int x, int y, int z) {
return _lowerCornerLocation.getX() <= x && x <= _upperCornerLocation.getX() && _lowerCornerLocation
.getY() <= y && y <= _upperCornerLocation.getY() && _lowerCornerLocation
.getZ() <= z && z <= _upperCornerLocation.getZ();
}

@Override
public void removeFromWorld() {
BukkitWorldEditor.clear(_upperCornerLocation, _lowerCornerLocation);
BukkitWorldEditor.fillWithBars(_upperCornerLocation, _upperCornerLocation);
BukkitWorldEditor.fillWithBars(_lowerCornerLocation, _lowerCornerLocation);
_isDestroyed = true;
}

public void generate() {
BukkitWorldEditor.fillWithBars(_upperCornerLocation, _lowerCornerLocation);
}

@Override
public void regenerate() {
if (!_isDestroyed) {
return;
}

generate();
_isDestroyed = false;
}

@Override
public boolean isDestroyed() {
return _isDestroyed;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ public PrisonBuilding(PrisonEscapeLocation reference) {
_maze = new Maze();
List<Dirt> dirts = _maze.buildMaze(config.getMazeUpperCornerLocation().add(reference), config.getMazeFormat());
_obstacles.addAll(dirts);

for (List<PrisonEscapeLocation> pair : createLocationsPairList(reference, config.getFencesLocations())) {
Fence fence = new Fence(pair.get(0), pair.get(1));
fence.generate();
_obstacles.add(fence);
}

for (PrisonEscapeLocation location : createLocationsList(reference, config.getVentsLocations())) {
Vent vent = new Vent(location);
vent.generate();
_obstacles.add(vent);
}
}

private List<PrisonEscapeLocation> createLocationsList(
Expand All @@ -94,6 +106,19 @@ private List<PrisonEscapeLocation> createLocationsList(
return list;
}

private List<List<PrisonEscapeLocation>> createLocationsPairList(
PrisonEscapeLocation reference,
List<List<PrisonEscapeLocation>> pairs
) {
List<List<PrisonEscapeLocation>> list = new ArrayList<>();

for (List<PrisonEscapeLocation> pair : pairs) {
list.add(createLocationsList(reference, pair));
}

return list;
}

private Hashtable<String, PrisonEscapeLocation> createLocationsMap(
PrisonEscapeLocation reference,
Hashtable<PrisonEscapeLocation, PrisonEscapeLocation> locs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.tiagofar78.prisonescape.game.prisonbuilding;

public interface Regenerable {

public void regenerate();

public boolean isDestroyed();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package net.tiagofar78.prisonescape.game.prisonbuilding;

import net.tiagofar78.prisonescape.bukkit.BukkitWorldEditor;
import net.tiagofar78.prisonescape.items.ToolItem;
import net.tiagofar78.prisonescape.items.WrenchItem;

public class Vent extends Obstacle {

private PrisonEscapeLocation _location;

public Vent(PrisonEscapeLocation location) {
_location = location;
}

@Override
public boolean isEffectiveTool(ToolItem tool) {
return tool instanceof WrenchItem;
}

@Override
public boolean contains(PrisonEscapeLocation location) {
return location.equals(_location);
}

@Override
public void removeFromWorld() {
BukkitWorldEditor.clear(_location, _location);
}

public void generate() {
BukkitWorldEditor.putVent(_location);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public static Item createItem(String name) {

private static Item instantiateItem(Class<? extends Item> itemClass) {
try {
System.out.println("Instanciou um novo " + itemClass.getSimpleName());
return itemClass.getDeclaredConstructor().newInstance();
} catch (Exception e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public static ConfigManager getInstance() {
private List<List<String>> _wallCrackFormats;
private List<String> _mazeFormat;
private PrisonEscapeLocation _mazeUpperCornerLocation;
private List<List<PrisonEscapeLocation>> _fencesLocations;
private List<PrisonEscapeLocation> _ventsLocations;

private Hashtable<String, List<ItemProbability>> _regionsChestContents;

Expand Down Expand Up @@ -131,6 +133,8 @@ public ConfigManager() {
_wallCrackFormats = createStringListList(config, "WallCrackFormats");
_mazeFormat = config.getStringList("Maze.Format");
_mazeUpperCornerLocation = createLocation(config, "Maze.UpperCornerLocation");
_fencesLocations = createLocationPairList(config, "Fences");
_ventsLocations = createLocationList(config, "Vents");

_regionsChestContents = createRegionsChestContentsMap(config);

Expand Down Expand Up @@ -163,6 +167,24 @@ private List<PrisonEscapeLocation> createLocationList(YamlConfiguration config,
return list;
}

private List<List<PrisonEscapeLocation>> createLocationPairList(YamlConfiguration config, String path) {
List<List<PrisonEscapeLocation>> list = new ArrayList<>();

List<String> filteredKeys = config.getKeys(true)
.stream()
.filter(key -> key.startsWith(path) && key.lastIndexOf(".") == path.length())
.toList();

for (String key : filteredKeys) {
List<PrisonEscapeLocation> pair = new ArrayList<>();
pair.add(createLocation(config, key + ".UpperCornerLocation"));
pair.add(createLocation(config, key + ".LowerCornerLocation"));
list.add(pair);
}

return list;
}

private List<List<String>> createStringListList(YamlConfiguration config, String path) {
List<List<String>> list = new ArrayList<>();

Expand Down Expand Up @@ -448,6 +470,14 @@ public PrisonEscapeLocation getMazeUpperCornerLocation() {
return createLocationCopy(_mazeUpperCornerLocation);
}

public List<List<PrisonEscapeLocation>> getFencesLocations() {
return createLocationsPairListCopy(_fencesLocations);
}

public List<PrisonEscapeLocation> getVentsLocations() {
return createLocationsListCopy(_ventsLocations);
}

public List<ItemProbability> getChestContents(String regionName) {
if (!_regionsChestContents.containsKey(regionName)) {
return null;
Expand Down Expand Up @@ -486,6 +516,18 @@ private List<PrisonEscapeLocation> createLocationsListCopy(List<PrisonEscapeLoca
return list;
}

private List<List<PrisonEscapeLocation>> createLocationsPairListCopy(
List<List<PrisonEscapeLocation>> locationPairs
) {
List<List<PrisonEscapeLocation>> list = new ArrayList<>();

for (List<PrisonEscapeLocation> locationPair : locationPairs) {
list.add(createLocationsListCopy(locationPair));
}

return list;
}

private Hashtable<PrisonEscapeLocation, PrisonEscapeLocation> createLocationsMapCopy(
Hashtable<PrisonEscapeLocation, PrisonEscapeLocation> locations
) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ Maze:
X: 0
Y: 0
Z: 0
Fences:
"1":
UpperCornerLocation:
X: 0
Y: 0
Z: 0
LowerCornerLocation:
X: 0
Y: 0
Z: 0
Vents:
"1":
X: 0
Y: 0
Z: 0
CommonItemsProbability: 0.8
RareItemsProbability: 0.25
ChestSize: 5
Expand Down
Loading