Skip to content

Commit

Permalink
Fix bugs, add new event for other mods to use, allow customisation of…
Browse files Browse the repository at this point in the history
… xp storage
  • Loading branch information
Patbox committed Aug 11, 2021
1 parent 24a63d0 commit 8b9d828
Show file tree
Hide file tree
Showing 15 changed files with 211 additions and 83 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ If you have any questions, you can ask them on my [Discord](https://discord.com/

![Example image](https://i.imgur.com/hfyd10Q.png)

Fun fact, I died 147 times while making this mod + 17 on 2nd account (so far)
Fun fact, I died 237 times while making this mod + 35 on 2nd account (so far)

## Commands (and permissions):
- `/graves` - Main command, shows list of users graves (`universal_graves.list`, available by default)
Expand All @@ -18,7 +18,7 @@ Additionally, by having `universal_graves.teleport` permission, you can teleport

## Configuration:
You can find config file in `./config/unicversal-graves.json`.
[Formatting uses PlaceholderAPI's Text Parser for which docs you can find here](https://github.com/Patbox/FabricPlaceholderAPI/blob/1.17/TEXT_FORMATTING.md).
[Formatting uses Simplified Text for which docs you can find here]().
Additionally, every message type has few own local variables.

```json5
Expand All @@ -32,6 +32,9 @@ Additionally, every message type has few own local variables.
"protectionTime": 300, // Time for which graves should be protected (is seconds)
"shouldBreak": true, // Changes if grave should break after some time
"breakAfter": 900, // Time after which grave will break
"storeExperience": true, // If true, experience points will be stored in grave
"xpStorageType": "vanilla", // Allows to change how much of xp is stored, `none` for nothing, `vanilla` for vanilla amount `percent_levels` for percent of levels and `percent_points` for percent of points
"xpPercentTypeValue": 100.0, // Changes how much percent of xp will be stored, works only with xpStorageType of `percent_...`
"createGravesFromPvP": true, // If false, after dying from another players attack grave won't be created
"dropItemsAfterExpiring": true, // If items should drop breaking from expiration
"hologram": true, // Enables hologram
Expand Down
9 changes: 4 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ repositories {
maven { url 'https://maven.nucleoid.xyz' }
maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
maven { url 'https://api.modrinth.com/maven' }
//mavenLocal()

/*maven {
name = "TerraformersMC"
Expand Down Expand Up @@ -48,10 +47,10 @@ dependencies {
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"


modImplementation include("eu.pb4:sgui:1.0.0-rc3-1.17.1")
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-pre9+1.17.1")
modImplementation include("eu.pb4:sgui:1.0.0-rc4+1.17.1")
modImplementation include("eu.pb4:hologram-api:0.2.1+1.17.1")
modImplementation include("eu.pb4:placeholder-api:1.1.0+1.17.1")
modImplementation include("eu.pb4:polymer:0.1.0-rc.4+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
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.3

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

Expand Down
49 changes: 23 additions & 26 deletions src/main/java/eu/pb4/graves/GravesMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,45 @@

import eu.pb4.graves.compat.GomlCompat;
import eu.pb4.graves.compat.TrinketsCompat;
import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.grave.GraveBlock;
import eu.pb4.graves.grave.GraveBlockEntity;
import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.grave.GraveManager;
import eu.pb4.graves.other.Commands;
import eu.pb4.polymer.PolymerMod;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.object.builder.v1.block.entity.FabricBlockEntityTypeBuilder;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class GravesMod implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger("Universal Graves");
public static String VERSION = FabricLoader.getInstance().getModContainer("universal-graves").get().getMetadata().getVersion().getFriendlyString();

@Override
public void onInitialize() {
Registry.register(Registry.BLOCK, new Identifier("universal_graves", "grave"), GraveBlock.INSTANCE);
GraveBlockEntity.BLOCK_ENTITY_TYPE = Registry.register(Registry.BLOCK_ENTITY_TYPE, "universal_graves:grave", FabricBlockEntityTypeBuilder.create(GraveBlockEntity::new, GraveBlock.INSTANCE).build(null));
Commands.register();
PolymerMod.registerVirtualBlockEntity(new Identifier("universal_graves", "grave"));
FabricLoader loader = FabricLoader.getInstance();

if (loader.isModLoaded("trinkets")) {
TrinketsCompat.register();
}
if (loader.isModLoaded("goml")) {
GomlCompat.register();
}
public static final Logger LOGGER = LogManager.getLogger("Universal Graves");
public static String VERSION = FabricLoader.getInstance().getModContainer("universal-graves").get().getMetadata().getVersion().getFriendlyString();

ServerLifecycleEvents.SERVER_STARTING.register((server) -> ConfigManager.loadConfig());
ServerLifecycleEvents.SERVER_STARTED.register((server ->
GraveManager.INSTANCE = (GraveManager) server.getOverworld().getPersistentStateManager().getOrCreate((nbtCompound) -> GraveManager.fromNbt(nbtCompound),
() -> new GraveManager(),
"universal-graves"))
);
}
@Override
public void onInitialize() {
Registry.register(Registry.BLOCK, new Identifier("universal_graves", "grave"), GraveBlock.INSTANCE);
GraveBlockEntity.BLOCK_ENTITY_TYPE = Registry.register(Registry.BLOCK_ENTITY_TYPE, "universal_graves:grave", FabricBlockEntityTypeBuilder.create(GraveBlockEntity::new, GraveBlock.INSTANCE).build(null));
Commands.register();
PolymerMod.registerVirtualBlockEntity(new Identifier("universal_graves", "grave"));
FabricLoader loader = FabricLoader.getInstance();

if (loader.isModLoaded("trinkets")) {
TrinketsCompat.register();
}
if (loader.isModLoaded("goml")) {
GomlCompat.register();
}

ServerLifecycleEvents.SERVER_STARTING.register((server) -> ConfigManager.loadConfig());
ServerLifecycleEvents.SERVER_STARTED.register((server ->
GraveManager.INSTANCE = (GraveManager) server.getOverworld().getPersistentStateManager().getOrCreate(GraveManager::fromNbt,
GraveManager::new,
"universal-graves"))
);
}
}
59 changes: 29 additions & 30 deletions src/main/java/eu/pb4/graves/compat/TrinketsCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,46 @@
import dev.emi.trinkets.api.TrinketInventory;
import dev.emi.trinkets.api.TrinketsApi;
import dev.emi.trinkets.api.event.TrinketDropCallback;
import eu.pb4.graves.event.PlayerGraveItemAddedEvent;
import eu.pb4.graves.event.PlayerGraveItemsEvent;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;

public class TrinketsCompat {
public static void register() {
PlayerGraveItemsEvent.EVENT.register((player, items) -> {
TrinketsApi.getTrinketComponent(player).ifPresent(trinkets -> trinkets.forEach((ref, stack) -> {
if (stack.isEmpty()) {
return;
}
PlayerGraveItemsEvent.EVENT.register((player, items) -> TrinketsApi.getTrinketComponent(player).ifPresent(trinkets -> trinkets.forEach((ref, stack) -> {
if (stack.isEmpty() || PlayerGraveItemAddedEvent.EVENT.invoker().canAddItem(player, stack) == ActionResult.FAIL) {
return;
}

TrinketEnums.DropRule dropRule = TrinketsApi.getTrinket(stack.getItem()).getDropRule(stack, ref, player);
TrinketEnums.DropRule dropRule = TrinketsApi.getTrinket(stack.getItem()).getDropRule(stack, ref, player);

dropRule = TrinketDropCallback.EVENT.invoker().drop(dropRule, stack, ref, player);
dropRule = TrinketDropCallback.EVENT.invoker().drop(dropRule, stack, ref, player);

TrinketInventory inventory = ref.inventory();
TrinketInventory inventory = ref.inventory();

if (dropRule == TrinketEnums.DropRule.DEFAULT) {
dropRule = inventory.getSlotType().getDropRule();
}
if (dropRule == TrinketEnums.DropRule.DEFAULT) {
dropRule = inventory.getSlotType().getDropRule();
}

if (dropRule == TrinketEnums.DropRule.DEFAULT) {
if (EnchantmentHelper.hasVanishingCurse(stack)) {
dropRule = TrinketEnums.DropRule.DESTROY;
} else {
dropRule = TrinketEnums.DropRule.DROP;
}
if (dropRule == TrinketEnums.DropRule.DEFAULT) {
if (EnchantmentHelper.hasVanishingCurse(stack)) {
dropRule = TrinketEnums.DropRule.DESTROY;
} else {
dropRule = TrinketEnums.DropRule.DROP;
}

switch (dropRule) {
case DROP:
items.add(stack.copy());
case DESTROY:
inventory.setStack(ref.index(), ItemStack.EMPTY);
break;
default:
break;
}
}));

});
}

switch (dropRule) {
case DROP:
items.add(stack.copy());
case DESTROY:
inventory.setStack(ref.index(), ItemStack.EMPTY);
break;
default:
break;
}
})));
}
}
3 changes: 3 additions & 0 deletions src/main/java/eu/pb4/graves/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import eu.pb4.graves.grave.GravesLookType;
import eu.pb4.graves.config.data.ConfigData;
import eu.pb4.graves.grave.GravesXPCalculation;
import eu.pb4.placeholders.TextParser;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -37,11 +38,13 @@ public final class Config {
public final Text creationFailedPvPGraveMessage;
@Nullable
public final Text creationFailedClaimGraveMessage;
public final GravesXPCalculation xpCalc;


public Config(ConfigData data) {
this.configData = data;
this.style = GravesLookType.byName(configData.graveType);
this.xpCalc = GravesXPCalculation.byName(configData.xpStorageType);
this.hologramProtectedText = parse(data.hologramProtectedText);
this.hologramText = parse(data.hologramText);

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/eu/pb4/graves/config/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import eu.pb4.graves.GravesMod;
import eu.pb4.graves.config.data.ConfigData;
import net.fabricmc.loader.api.FabricLoader;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class ConfigManager {
public static final int VERSION = 1;
Expand All @@ -34,14 +37,14 @@ public static boolean loadConfig() {


if (configFile.exists()) {
String json = IOUtils.toString(new InputStreamReader(new FileInputStream(configFile), "UTF-8"));
String json = IOUtils.toString(new InputStreamReader(new FileInputStream(configFile), StandardCharsets.UTF_8));

config = GSON.fromJson(json, ConfigData.class);
} else {
config = new ConfigData();
}

BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), "UTF-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configFile), StandardCharsets.UTF_8));
writer.write(GSON.toJson(config));
writer.close();

Expand Down
8 changes: 7 additions & 1 deletion src/main/java/eu/pb4/graves/config/data/ConfigData.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package eu.pb4.graves.config.data;

import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.grave.GravesLookType;
import eu.pb4.graves.grave.GravesXPCalculation;

import java.util.ArrayList;
import java.util.List;

public class ConfigData {
public int CONFIG_VERSION_DONT_TOUCH_THIS = ConfigManager.VERSION;
public String _comment = "Before changing anything, see https://github.com/Patbox/UniversalGraves#configuration";
public String graveType = "player_head";
public String graveType = GravesLookType.PLAYER_HEAD.name;
public String lockedTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYjdjYWI1NmM4MmNiODFiZGI5OTc5YTQ2NGJjOWQzYmEzZTY3MjJiYTEyMmNmNmM1Mjg3MzAxMGEyYjU5YWVmZSJ9fX0=";
public String unlockedTexture = "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYjdjYWI1NmM4MmNiODFiZGI5OTc5YTQ2NGJjOWQzYmEzZTY3MjJiYTEyMmNmNmM1Mjg3MzAxMGEyYjU5YWVmZSJ9fX0=";

Expand All @@ -18,6 +20,10 @@ public class ConfigData {
public boolean shouldBreak = true;
public int breakAfter = 900;

public boolean storeExperience = true;
public String xpStorageType = GravesXPCalculation.VANILLA.name;
public double xpPercentTypeValue = 100;

public boolean createGravesFromPvP = true;
public boolean createGravesInClaims = true;
public boolean dropItemsAfterExpiring = true;
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/eu/pb4/graves/event/PlayerGraveItemAddedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package eu.pb4.graves.event;

import net.fabricmc.fabric.api.event.Event;
import net.fabricmc.fabric.api.event.EventFactory;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;

import java.util.List;

public interface PlayerGraveItemAddedEvent {
Event<PlayerGraveItemAddedEvent> EVENT = EventFactory.createArrayBacked(PlayerGraveItemAddedEvent.class,
(listeners) -> (player, item) -> {
for (PlayerGraveItemAddedEvent listener : listeners) {
ActionResult result = listener.canAddItem(player, item);

if (result != ActionResult.PASS) {
return result;
}
}
return ActionResult.PASS;
});

ActionResult canAddItem(ServerPlayerEntity player, ItemStack item);
}
6 changes: 5 additions & 1 deletion src/main/java/eu/pb4/graves/grave/GraveBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
BlockEntity blockEntity = world.getBlockEntity(pos);

if (blockEntity instanceof GraveBlockEntity grave && grave.info.canTakeFrom(player)) {
new GraveGui((ServerPlayerEntity) player, grave).open();
if (grave.info.itemCount > 0) {
new GraveGui((ServerPlayerEntity) player, grave).open();
} else {
world.setBlockState(pos, grave.replacedBlockState, Block.NOTIFY_ALL);
}
return ActionResult.SUCCESS;
}
return super.onUse(state, world, pos, player, hand, hit);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/eu/pb4/graves/grave/GraveGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public void onTick() {
@Override
public void onClose() {
if (this.grave.isEmpty() && !this.grave.isRemoved()) {
this.grave.getWorld().setBlockState(this.grave.getPos(), Blocks.AIR.getDefaultState(), Block.NOTIFY_ALL);
assert this.grave.getWorld() != null;
this.grave.getWorld().setBlockState(this.grave.getPos(), this.grave.replacedBlockState, Block.NOTIFY_ALL);
} else {
this.grave.updateItemCount();
}
Expand Down
Loading

0 comments on commit 8b9d828

Please sign in to comment.