Skip to content

Commit

Permalink
simple item values loader, make Conflagrate scale in size by consumin…
Browse files Browse the repository at this point in the history
…g blaze dust/rods
  • Loading branch information
reoseah committed Nov 2, 2024
1 parent a44562e commit 7f4f612
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 30 deletions.
45 changes: 25 additions & 20 deletions src/main/java/io/github/reoseah/magisterium/Magisterium.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.reoseah.magisterium;

import com.google.common.collect.ImmutableSet;
import io.github.reoseah.magisterium.block.*;
import io.github.reoseah.magisterium.data.ItemValuesLoader;
import io.github.reoseah.magisterium.item.BookmarkItem;
import io.github.reoseah.magisterium.item.SpellBookItem;
import io.github.reoseah.magisterium.item.SpellPageItem;
Expand All @@ -12,6 +14,7 @@
import io.github.reoseah.magisterium.recipe.*;
import io.github.reoseah.magisterium.screen.ArcaneTableScreenHandler;
import io.github.reoseah.magisterium.screen.SpellBookScreenHandler;
import io.github.reoseah.magisterium.spellbook.SpellDataLoader;
import io.github.reoseah.magisterium.world.MagisteriumPlaygrounds;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
Expand All @@ -20,6 +23,8 @@
import net.fabricmc.fabric.api.loot.v3.LootTableSource;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.impl.resource.loader.ResourceManagerHelperImpl;
import net.minecraft.block.LecternBlock;
import net.minecraft.block.entity.LecternBlockEntity;
import net.minecraft.entity.player.PlayerEntity;
Expand All @@ -30,13 +35,15 @@
import net.minecraft.loot.entry.LootTableEntry;
import net.minecraft.loot.provider.number.UniformLootNumberProvider;
import net.minecraft.registry.*;
import net.minecraft.resource.ResourceType;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.sound.SoundCategory;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.world.World;
import net.minecraft.world.event.GameEvent;
Expand Down Expand Up @@ -127,6 +134,8 @@ public void onInitialize() {
Registry.register(Registries.SOUND_EVENT, "magisterium:chant", MagisteriumSounds.CHANT);
Registry.register(Registries.SOUND_EVENT, "magisterium:arcane_lift_loop", MagisteriumSounds.ARCANE_LIFT_LOOP);

ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new ItemValuesLoader());

MagisteriumGameRules.initialize();
MagisteriumCommands.initialize();

Expand Down Expand Up @@ -238,29 +247,25 @@ private static boolean tryPlaceGlyph(PlayerEntity player, World world, Hand hand
return true;
}

public static final RegistryKey<LootTable> COMMON_STRUCTURES_LOOT = RegistryKey.of(RegistryKeys.LOOT_TABLE,
public static final RegistryKey<LootTable> COMMON_STRUCTURES_LOOT = RegistryKey.of(RegistryKeys.LOOT_TABLE, //
Identifier.of("magisterium:chests/parts/common_loot"));
public static final RegistryKey<LootTable> RARE_STRUCTURES_LOOT = RegistryKey.of(RegistryKeys.LOOT_TABLE,
public static final RegistryKey<LootTable> RARE_STRUCTURES_LOOT = RegistryKey.of(RegistryKeys.LOOT_TABLE, //
Identifier.of("magisterium:chests/parts/rare_loot"));

public static final Set<Identifier> STRUCTURES_TO_SPAWN_COMMON_LOOT = new HashSet<>();

static {
STRUCTURES_TO_SPAWN_COMMON_LOOT.add(Identifier.ofVanilla("chests/simple_dungeon"));
STRUCTURES_TO_SPAWN_COMMON_LOOT.add(Identifier.ofVanilla("chests/desert_pyramid"));
STRUCTURES_TO_SPAWN_COMMON_LOOT.add(Identifier.ofVanilla("chests/jungle_temple"));
STRUCTURES_TO_SPAWN_COMMON_LOOT.add(Identifier.ofVanilla("chests/stronghold_corridor"));
STRUCTURES_TO_SPAWN_COMMON_LOOT.add(Identifier.ofVanilla("chests/stronghold_crossing"));
STRUCTURES_TO_SPAWN_COMMON_LOOT.add(Identifier.ofVanilla("chests/stronghold_library"));
}

public static final Set<Identifier> STRUCTURES_TO_SPAWN_RARE_LOOT = new HashSet<>();

static {
STRUCTURES_TO_SPAWN_RARE_LOOT.add(Identifier.ofVanilla("chests/desert_pyramid"));
STRUCTURES_TO_SPAWN_RARE_LOOT.add(Identifier.ofVanilla("chests/jungle_temple"));
STRUCTURES_TO_SPAWN_RARE_LOOT.add(Identifier.ofVanilla("chests/stronghold_library"));
}
public static final Set<Identifier> STRUCTURES_TO_SPAWN_COMMON_LOOT = ImmutableSet.<Identifier>builder() //
.add(Identifier.ofVanilla("chests/simple_dungeon")) //
.add(Identifier.ofVanilla("chests/desert_pyramid")) //
.add(Identifier.ofVanilla("chests/jungle_temple")) //
.add(Identifier.ofVanilla("chests/stronghold_corridor")) //
.add(Identifier.ofVanilla("chests/stronghold_crossing")) //
.add(Identifier.ofVanilla("chests/stronghold_library")) //
.build();

public static final Set<Identifier> STRUCTURES_TO_SPAWN_RARE_LOOT = ImmutableSet.<Identifier>builder() //
.add(Identifier.ofVanilla("chests/desert_pyramid")) //
.add(Identifier.ofVanilla("chests/jungle_temple")) //
.add(Identifier.ofVanilla("chests/stronghold_library")) //
.build();

private static void modifyLootTable(RegistryKey<LootTable> key, LootTable.Builder tableBuilder, LootTableSource source, RegistryWrapper.WrapperLookup registries) {
if (STRUCTURES_TO_SPAWN_COMMON_LOOT.contains(key.getValue())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;
import net.fabricmc.fabric.api.client.particle.v1.ParticleFactoryRegistry;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.impl.resource.loader.ResourceManagerHelperImpl;
import net.minecraft.client.gui.screen.ingame.HandledScreens;
import net.minecraft.client.item.ModelPredicateProviderRegistry;
Expand All @@ -31,7 +32,7 @@ public void onInitializeClient() {
// perhaps it could be loaded on the server and sent to client,
// like the recipes do?
// this will help with making items for each spell
ResourceManagerHelperImpl.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new SpellDataLoader());
ResourceManagerHelper.get(ResourceType.CLIENT_RESOURCES).registerReloadListener(new SpellDataLoader());

BlockRenderLayerMap.INSTANCE.putBlocks(RenderLayer.getCutout(), ArcaneTableBlock.INSTANCE, GlyphBlock.INSTANCE);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.github.reoseah.magisterium.data;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntMaps;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.resource.JsonDataLoader;
import net.minecraft.resource.ResourceManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.profiler.Profiler;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

public class ItemValuesLoader extends JsonDataLoader implements IdentifiableResourceReloadListener {
public static final Identifier ID = Identifier.of("magisterium", "item_values");

private static final Logger LOGGER = LogManager.getLogger();
private static final Gson GSON = new Gson();

// TODO: support item tags (for more flexibility)
public static Object2IntMap<Identifier> ITEM_VALUES = Object2IntMaps.emptyMap();

public static int getValue(ItemStack stack) {
return ITEM_VALUES.getInt(Registries.ITEM.getId(stack.getItem()));
}

public ItemValuesLoader() {
super(GSON, "magisterium/item_values");
}

@Override
public Identifier getFabricId() {
return ID;
}

@Override
protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager manager, Profiler profiler) {
boolean errors = false;

var data = new Object2IntOpenHashMap<Identifier>();

for (Map.Entry<Identifier, JsonElement> entry : prepared.entrySet()) {
try {
var json = JsonHelper.asObject(entry.getValue(), "item value entry");
var item = JsonHelper.getString(json, "item");
var value = JsonHelper.getInt(json, "value");
data.put(Identifier.of(item), value);
} catch (Exception e) {
LOGGER.error("Error loading item value from {}", entry.getKey(), e);
errors = true;
}
}
if (errors) {
throw new IllegalStateException("Failed to load item values");
}

ITEM_VALUES = Object2IntMaps.unmodifiable(data);
LOGGER.debug("Loaded {} item values", ITEM_VALUES.size());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.reoseah.magisterium.recipe;

import io.github.reoseah.magisterium.data.ItemValuesLoader;
import io.github.reoseah.magisterium.world.MagisteriumPlaygrounds;
import net.fabricmc.fabric.api.registry.FlammableBlockRegistry;
import net.minecraft.block.BlockState;
Expand Down Expand Up @@ -29,13 +30,24 @@ public boolean matches(SpellBookRecipeInput input, World world) {

@Override
public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup lookup) {
// TODO consume the items to increase the range

// TODO make the area circular instead of square
int totalValue = 0;
for (int i = 0; i < input.getSize(); i++) {
var stack = input.getStackInSlot(i);
if (!stack.isEmpty()) {
int value = ItemValuesLoader.getValue(stack);
totalValue += value;

stack.decrement(1);
input.inventory.setStack(i, stack);
}
}

// TODO spawn a bunch of fire particles in the area

final int buildUpStart = 1, buildUpFinish = 5, decayStart = 11, decayFinish = 15;
final int buildUpStart = 1, buildUpFinish = 5;

int decayStart = 5 + totalValue;
int decayFinish = 9 + totalValue;

boolean hasTargets = false;
boolean hasLit = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,12 @@ protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager mana
errors = true;
}
}

if (!errors) {
SPELLS = builder.build();
LOGGER.info("Loaded {} spell data", SPELLS.size());
} else {
if (errors) {
throw new IllegalStateException("Failed to load spell data");
}

SPELLS = builder.build();
LOGGER.debug("Loaded {} spell data", SPELLS.size());
}

private static BookElement readElement(JsonObject json) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"item": "minecraft:blaze_powder",
"value": 2
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"item": "minecraft:blaze_rod",
"value": 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"item": "minecraft:breeze_rod",
"value": 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"item": "minecraft:experience_bottle",
"value": 4
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"item": "minecraft:wind_charge",
"value": 1
}

0 comments on commit 7f4f612

Please sign in to comment.