Skip to content

Commit

Permalink
Improvements and new events
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 13, 2021
1 parent 691dc53 commit a9d193b
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 74 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repositories {
maven { url 'https://maven.nucleoid.xyz' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://api.modrinth.com/maven' }
mavenLocal()
//mavenLocal()

/*maven {
name = "TerraformersMC"
Expand Down Expand Up @@ -49,9 +49,9 @@ dependencies {


modImplementation include("eu.pb4:sgui:1.0.0-rc3-1.17.1")
modImplementation include("eu.pb4:hologram-api:0.1.1+1.17")
modImplementation include("eu.pb4:hologram-api:0.1.2+1.17.1")
modImplementation include("eu.pb4:placeholder-api:1.0.1+1.17")
modImplementation include("eu.pb4:polymer:0.1.0-pre5-1.17")
modImplementation include("eu.pb4:polymer:0.1.0-pre9+1.17.1")
modImplementation include("fr.catcore:server-translations-api:1.4.5+1.17")
modImplementation include("me.lucko:fabric-permissions-api:0.1-SNAPSHOT")

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.3

# Mod Properties
mod_version = 1.0.6+1.17.1
mod_version = 1.0.7+1.17.1
maven_group = eu.pb4
archives_base_name = graves

# Dependencies
fabric_version=0.34.9+1.17
fabric_version=0.36.1+1.17
17 changes: 17 additions & 0 deletions src/main/java/eu/pb4/graves/compat/GomlCompat.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package eu.pb4.graves.compat;


import draylar.goml.api.ClaimUtils;
import draylar.goml.api.event.ClaimEvents;
import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.event.GraveValidPosCheckEvent;
import eu.pb4.graves.grave.GraveBlockEntity;
import eu.pb4.graves.other.GraveUtils;
import net.minecraft.util.ActionResult;

import java.util.stream.Collectors;

public class GomlCompat {
public static void register() {
ClaimEvents.PERMISSION_DENIED.register((player, world, hand, pos, reason) -> {
Expand All @@ -14,5 +20,16 @@ public static void register() {

return ActionResult.PASS;
});

GraveValidPosCheckEvent.EVENT.register(((player, world, pos) -> {
if (!ConfigManager.getConfig().configData.createGravesInClaims) {
for (var entry : ClaimUtils.getClaimsAt(world, pos).collect(Collectors.toList())) {
if (!entry.getValue().hasPermission(player)) {
return GraveUtils.BlockResult.BLOCK_CLAIM;
}
}
}
return GraveUtils.BlockResult.ALLOW;
}));
}
}
1 change: 1 addition & 0 deletions src/main/java/eu/pb4/graves/compat/TrinketsCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import dev.emi.trinkets.api.TrinketInventory;
import dev.emi.trinkets.api.TrinketsApi;
import dev.emi.trinkets.api.event.TrinketDropCallback;
import eu.pb4.graves.event.PlayerGraveItemsEvent;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.ItemStack;

Expand Down
22 changes: 16 additions & 6 deletions src/main/java/eu/pb4/graves/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import eu.pb4.graves.config.data.ConfigData;
import eu.pb4.placeholders.TextParser;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -22,12 +23,20 @@ public final class Config {
public final List<Text> guiProtectedText;
public final List<Text> guiText;

@Nullable
public final Text noLongerProtectedMessage;
@Nullable
public final Text graveExpiredMessage;
@Nullable
public final Text graveBrokenMessage;
@Nullable
public final Text createdGraveMessage;
@Nullable
public final Text creationFailedGraveMessage;
@Nullable
public final Text creationFailedPvPGraveMessage;
@Nullable
public final Text creationFailedClaimGraveMessage;


public Config(ConfigData data) {
Expand All @@ -42,12 +51,13 @@ public Config(ConfigData data) {
this.guiProtectedText = parse(data.guiProtectedText);
this.guiText = parse(data.guiText);

this.noLongerProtectedMessage = TextParser.parse(data.noLongerProtectedMessage);
this.graveExpiredMessage = TextParser.parse(data.graveExpiredMessage);
this.graveBrokenMessage = TextParser.parse(data.graveBrokenMessage);
this.createdGraveMessage = TextParser.parse(data.createdGraveMessage);
this.creationFailedGraveMessage = TextParser.parse(data.creationFailedGraveMessage);
this.creationFailedPvPGraveMessage = TextParser.parse(data.creationFailedPvPGraveMessage);
this.noLongerProtectedMessage = data.displayNoLongerProtectedMessage ? TextParser.parse(data.noLongerProtectedMessage) : null;
this.graveExpiredMessage = data.displayGraveExpiredMessage ? TextParser.parse(data.graveExpiredMessage) : null;
this.graveBrokenMessage = data.displayGraveBrokenMessage ? TextParser.parse(data.graveBrokenMessage) : null;
this.createdGraveMessage = data.displayCreatedGraveMessage ? TextParser.parse(data.createdGraveMessage) : null;
this.creationFailedGraveMessage = data.displayCreationFailedGraveMessage ? TextParser.parse(data.creationFailedGraveMessage) : null;
this.creationFailedPvPGraveMessage = data.displayCreationFailedPvPGraveMessage ? TextParser.parse(data.creationFailedPvPGraveMessage) : null;
this.creationFailedClaimGraveMessage = data.displayCreationFailedClaimGraveMessage ? TextParser.parse(data.creationFailedClaimMessage) : null;
}


Expand Down
6 changes: 5 additions & 1 deletion src/main/java/eu/pb4/graves/config/data/ConfigData.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ConfigData {
public int breakAfter = 900;

public boolean createGravesFromPvP = true;
public boolean createGravesInClaims = true;
public boolean dropItemsAfterExpiring = true;

public boolean hologram = true;
Expand All @@ -45,7 +46,10 @@ public class ConfigData {
public String creationFailedGraveMessage = "<red><lang:'text.graves.creation_failed':'<gold>${position}':'<yellow>${world}'>";

public boolean displayCreationFailedPvPGraveMessage = true;
public String creationFailedPvPGraveMessage = "<red><lang:'text.graves.created_failed_pvp':'<gold>${position}':'<yellow>${world}'>";
public String creationFailedPvPGraveMessage = "<red><lang:'text.graves.creation_failed_pvp':'<gold>${position}':'<yellow>${world}'>";

public boolean displayCreationFailedClaimGraveMessage = true;
public String creationFailedClaimMessage = "<red><lang:'text.graves.creation_failed_claim':'<gold>${position}':'<yellow>${world}'>";

public String neverExpires = "Never";

Expand Down
24 changes: 24 additions & 0 deletions src/main/java/eu/pb4/graves/event/GraveValidPosCheckEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package eu.pb4.graves.event;

import eu.pb4.graves.other.GraveUtils;
import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

public interface GraveValidPosCheckEvent {
Event<GraveValidPosCheckEvent> EVENT = EventFactory.createArrayBacked(GraveValidPosCheckEvent.class,
(listeners) -> (player, world, pos) -> {
GraveUtils.BlockResult result = GraveUtils.BlockResult.ALLOW;
for (GraveValidPosCheckEvent listener : listeners) {
result = listener.isValid(player, world, pos);
if (!result.canCreate()) {
return result;
}
}
return result;
});

GraveUtils.BlockResult isValid(ServerPlayerEntity player, ServerWorld world, BlockPos pos);
}
41 changes: 41 additions & 0 deletions src/main/java/eu/pb4/graves/event/PlayerGraveCreationEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package eu.pb4.graves.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;

public interface PlayerGraveCreationEvent {
Event<PlayerGraveCreationEvent> EVENT = EventFactory.createArrayBacked(PlayerGraveCreationEvent.class,
(listeners) -> (player) -> {
CreationResult result = CreationResult.ALLOW;
for (PlayerGraveCreationEvent listener : listeners) {
result = listener.shouldCreate(player);
if (!result.allow) {
return result;
}
}
return result;
});

CreationResult shouldCreate(ServerPlayerEntity player);

enum CreationResult {
ALLOW(true),
BLOCK(false),
BLOCK_PVP(false),
BLOCK_CLAIM(false),
BLOCK_SILENT(false);

private final boolean allow;

CreationResult(boolean allow) {
this.allow = allow;
}

public boolean canCreate() {
return this.allow;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package eu.pb4.graves.compat;
package eu.pb4.graves.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/eu/pb4/graves/grave/GraveGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public GraveGui(ServerPlayerEntity player, GraveBlockEntity grave) {
this.setTitle(PlaceholderAPI.parsePredefinedText(ConfigManager.getConfig().graveTitle, PlaceholderAPI.PREDEFINED_PLACEHOLDER_PATTERN, grave.info.getPlaceholders()));
int x = 0;
int skipped = 0;
for (; x < Math.min(this.grave.size(), this.getSize()); x++) {
for (; x < this.grave.size(); x++) {
if (this.getFirstEmptySlot() == -1) {
return;
}

if (!grave.getStack(x).isEmpty()) {
this.addSlotRedirect(new OutputSlot(grave, x, 0, 0));
} else {
Expand All @@ -40,7 +44,7 @@ public GraveGui(ServerPlayerEntity player, GraveBlockEntity grave) {
}

public static ScreenHandlerType<?> getScreenHandlerType(int size) {
return switch (size / 9) {
return switch ((size - 1) / 9) {
case 0 -> ScreenHandlerType.GENERIC_9X1;
case 1 -> ScreenHandlerType.GENERIC_9X2;
case 2 -> ScreenHandlerType.GENERIC_9X3;
Expand Down
13 changes: 2 additions & 11 deletions src/main/java/eu/pb4/graves/grave/GraveInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mojang.authlib.GameProfile;
import eu.pb4.graves.config.Config;
import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.other.GraveUtils;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
Expand Down Expand Up @@ -86,17 +87,7 @@ public Map<String, Text> getPlaceholders() {
values.put("xp", new LiteralText("" + this.xp));
values.put("item_count", new LiteralText("" + this.itemCount));
values.put("position", new LiteralText("" + this.position.toShortString()));

List<String> parts = new ArrayList<>();
{
String[] words = this.world.getPath().split("_");
for (String word : words) {
String[] s = word.split("", 2);
s[0] = s[0].toUpperCase(Locale.ROOT);
parts.add(String.join("", s));
}
}
values.put("world", new LiteralText(String.join(" ", parts)));
values.put("world", new LiteralText(GraveUtils.toWorldName(this.world)));
values.put("death_cause", this.deathCause);
return values;
}
Expand Down
83 changes: 48 additions & 35 deletions src/main/java/eu/pb4/graves/mixin/LivingEntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.pb4.graves.mixin;

import com.google.common.collect.ImmutableList;
import eu.pb4.graves.compat.PlayerGraveItemsEvent;
import eu.pb4.graves.event.PlayerGraveCreationEvent;
import eu.pb4.graves.event.PlayerGraveItemsEvent;
import eu.pb4.graves.config.Config;
import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.grave.GraveBlock;
Expand All @@ -22,7 +22,6 @@
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
import net.minecraft.util.Util;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.GameRules;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -46,56 +45,70 @@ private void replaceWithGrave(DamageSource source, CallbackInfo ci) {
}

try {

Config config = ConfigManager.getConfig();
Text text = null;
Map<String, Text> placeholders = Collections.emptyMap();
Map<String, Text> placeholders = Map.of(
"position", new LiteralText("" + player.getBlockPos().toShortString()),
"world", new LiteralText(GraveUtils.toWorldName(player.getServerWorld().getRegistryKey().getValue()))
);


if (!config.configData.createGravesFromPvP && source.getAttacker() instanceof PlayerEntity) {
if (config.configData.displayCreationFailedPvPGraveMessage) {
text = config.creationFailedPvPGraveMessage;
}
text = config.creationFailedPvPGraveMessage;
} else {
BlockPos gravePos = GraveUtils.findGravePosition(player.getServerWorld(), player.getBlockPos(), TagRegistry.block(GraveUtils.REPLACEABLE_TAG));
var eventResult = PlayerGraveCreationEvent.EVENT.invoker().shouldCreate(player);

if (gravePos != null) {
List<ItemStack> items = new ArrayList<>();
if (eventResult.canCreate()) {
var result = GraveUtils.findGravePosition(player, player.getServerWorld(), player.getBlockPos(), TagRegistry.block(GraveUtils.REPLACEABLE_TAG));

for (int i = 0; i < player.getInventory().size(); ++i) {
ItemStack itemStack = player.getInventory().getStack(i);
if (!itemStack.isEmpty()) {
if (EnchantmentHelper.hasVanishingCurse(itemStack)) {
player.getInventory().removeStack(i);
} else {
items.add(player.getInventory().removeStack(i));
if (result.result().canCreate()) {
BlockPos gravePos = result.pos();
List<ItemStack> items = new ArrayList<>();

for (int i = 0; i < player.getInventory().size(); ++i) {
ItemStack itemStack = player.getInventory().getStack(i);
if (!itemStack.isEmpty()) {
if (EnchantmentHelper.hasVanishingCurse(itemStack)) {
player.getInventory().removeStack(i);
} else {
items.add(player.getInventory().removeStack(i));
}
}
}
}

PlayerGraveItemsEvent.EVENT.invoker().modifyItems(player, items);
PlayerGraveItemsEvent.EVENT.invoker().modifyItems(player, items);

if (items.size() == 0) {
return;
}
if (items.size() == 0) {
return;
}

BlockState blockState = player.getServerWorld().getBlockState(gravePos);
player.getServerWorld().setBlockState(gravePos, GraveBlock.INSTANCE.getDefaultState().with(Properties.ROTATION, player.getRandom().nextInt(15)));
BlockEntity entity = player.getServerWorld().getBlockEntity(gravePos);
BlockState blockState = player.getServerWorld().getBlockState(gravePos);
player.getServerWorld().setBlockState(gravePos, GraveBlock.INSTANCE.getDefaultState().with(Properties.ROTATION, player.getRandom().nextInt(15)));
BlockEntity entity = player.getServerWorld().getBlockEntity(gravePos);

if (entity instanceof GraveBlockEntity grave) {
int i = player.experienceLevel * 7;
grave.setGrave(player.getGameProfile(), items, i > 100 ? 100 : i, source.getDeathMessage(player), blockState);
player.experienceLevel = 0;
if (config.configData.displayCreatedGraveMessage) {
if (entity instanceof GraveBlockEntity grave) {
int i = player.experienceLevel * 7;
grave.setGrave(player.getGameProfile(), items, i > 100 ? 100 : i, source.getDeathMessage(player), blockState);
player.experienceLevel = 0;
text = config.createdGraveMessage;
placeholders = grave.info.getPlaceholders();
}
} else {
if (config.configData.displayCreationFailedGraveMessage) {
} else {
text = config.creationFailedGraveMessage;
placeholders = Map.of("position", new LiteralText("" + player.getBlockPos().toShortString()));
}
} else {
text = switch (result.result()) {
case BLOCK -> config.creationFailedGraveMessage;
case BLOCK_CLAIM -> config.creationFailedClaimGraveMessage;
case ALLOW -> null;
};
}
} else {
text = switch (eventResult) {
case BLOCK -> config.creationFailedGraveMessage;
case BLOCK_CLAIM -> config.creationFailedClaimGraveMessage;
case BLOCK_PVP -> config.creationFailedPvPGraveMessage;
default -> null;
};
}
}

Expand Down
Loading

0 comments on commit a9d193b

Please sign in to comment.