Skip to content

Commit

Permalink
close spell book when spell fails so players can see the message, uns…
Browse files Browse the repository at this point in the history
…table charge spell (not sure if I want to keep it)
  • Loading branch information
reoseah committed Oct 29, 2024
1 parent ebaa933 commit d3abe96
Show file tree
Hide file tree
Showing 17 changed files with 240 additions and 18 deletions.
5 changes: 4 additions & 1 deletion src/main/java/io/github/reoseah/magisterium/Magisterium.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void onInitialize() {

Registry.register(Registries.DATA_COMPONENT_TYPE, "magisterium:current_page", SpellBookItem.CURRENT_PAGE);
Registry.register(Registries.DATA_COMPONENT_TYPE, "magisterium:page_data", SpellBookItem.PAGES);
Registry.register(Registries.DATA_COMPONENT_TYPE, "magisterium:unstable_charge", SpellBookItem.UNSTABLE_CHARGE);
Registry.register(Registries.DATA_COMPONENT_TYPE, "magisterium:spell", SpellPageItem.SPELL);

var group = FabricItemGroup.builder() //
Expand All @@ -76,6 +77,7 @@ public void onInitialize() {
entries.add(SpellPageItem.createSpellPage(Identifier.of("magisterium:glyphic_ignition")));
entries.add(SpellPageItem.createSpellPage(Identifier.of("magisterium:conflagrate")));
entries.add(SpellPageItem.createSpellPage(Identifier.of("magisterium:illusory_wall")));
entries.add(SpellPageItem.createSpellPage(Identifier.of("magisterium:unstable_charge")));
entries.add(BookmarkItem.INSTANCE);
}) //
.build();
Expand All @@ -92,6 +94,7 @@ public void onInitialize() {
Registry.register(Registries.RECIPE_SERIALIZER, "magisterium:glyphic_ignition", GlyphicIgnitionRecipe.SERIALIZER);
Registry.register(Registries.RECIPE_SERIALIZER, "magisterium:conflagrate", ConflagrateRecipe.SERIALIZER);
Registry.register(Registries.RECIPE_SERIALIZER, "magisterium:illusory_wall", IllusoryWallRecipe.SERIALIZER);
Registry.register(Registries.RECIPE_SERIALIZER, "magisterium:unstable_charge", UnstableChargeRecipe.SERIALIZER);

Registry.register(Registries.SCREEN_HANDLER, "magisterium:spell_book", SpellBookScreenHandler.TYPE);
Registry.register(Registries.SCREEN_HANDLER, "magisterium:arcane_table", ArcaneTableScreenHandler.TYPE);
Expand Down Expand Up @@ -143,7 +146,7 @@ private static ActionResult interact(PlayerEntity player, World world, Hand hand
var book = lectern.getBook();
if (book.isEmpty() && stack.isOf(SpellBookItem.INSTANCE)) {
if (MagisteriumPlaygrounds.canModifyWorld(world, pos, player)) {
return LecternBlock.putBookIfAbsent(player, world, pos, state, stack) ? ActionResult.SUCCESS : ActionResult.FAIL;
return LecternBlock.putBookIfAbsent(player, world, pos, state, stack) ? ActionResult.SUCCESS : ActionResult.PASS;
}
return ActionResult.PASS;
} else if (book.isOf(SpellBookItem.INSTANCE)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ protected MapCodec<? extends BlockWithEntity> getCodec() {
public void onBroken(WorldAccess world, BlockPos pos, BlockState state) {
super.onBroken(world, pos, state);

final int maxIterations = 100;
final int maxIterations = 200;

var queue = new ArrayDeque<BlockPos>();
for (var dir : Direction.values()) {
queue.add(pos.offset(dir));
for (var nextPos : BlockPos.iterate(pos.add(-1, -1, -1), pos.add(1, 1, 1))) {
queue.add(nextPos.toImmutable());
}
var visited = new HashSet<BlockPos>();

Expand All @@ -67,13 +67,16 @@ public void onBroken(WorldAccess world, BlockPos pos, BlockState state) {
}
visited.add(currentPos);

var currentState = world.getBlockState(currentPos);
if (currentState.getBlock() == INSTANCE) {
if (world.getBlockState(currentPos).getBlock() == INSTANCE) {
// TODO perhaps check that illusion block is the same as the one being broken
// in case different illusions are touching each other
world.setBlockState(currentPos, Blocks.AIR.getDefaultState(), 3);
world.syncWorldEvent(null, 2001, currentPos, Block.getRawIdFromState(INSTANCE.getDefaultState()));

for (var dir : Direction.values()) {
queue.add(currentPos.offset(dir));
for (var nextPos : BlockPos.iterate(currentPos.add(-1, -1, -1), currentPos.add(1, 1, 1))) {
if (!visited.contains(nextPos)) {
queue.add(nextPos.toImmutable());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import io.github.reoseah.magisterium.screen.SpellBookScreenHandler;
import net.minecraft.component.ComponentType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ProfileComponent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.tooltip.TooltipType;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
Expand All @@ -26,6 +34,9 @@ public class SpellBookItem extends Item {
.codec(ItemStack.OPTIONAL_CODEC.listOf()) //
.packetCodec(ItemStack.OPTIONAL_PACKET_CODEC.collect(PacketCodecs.toList())) //
.build();
public static final ComponentType<Unit> UNSTABLE_CHARGE = ComponentType.<Unit>builder() //
.codec(Unit.CODEC) //
.build();

public static final Item INSTANCE = new SpellBookItem(new Item.Settings().maxCount(1).rarity(Rarity.RARE).component(CURRENT_PAGE, 0));

Expand All @@ -43,6 +54,7 @@ public static ItemStack createTestBook() {
list.set(3, SpellPageItem.createSpellPage(Identifier.of("magisterium:glyphic_ignition")));
list.set(4, SpellPageItem.createSpellPage(Identifier.of("magisterium:conflagrate")));
list.set(5, SpellPageItem.createSpellPage(Identifier.of("magisterium:illusory_wall")));
list.set(6, SpellPageItem.createSpellPage(Identifier.of("magisterium:unstable_charge")));
}));

return book;
Expand Down Expand Up @@ -71,4 +83,47 @@ public Text getDisplayName() {
}
return TypedActionResult.success(book, false);
}

@Override
public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
super.appendTooltip(stack, context, tooltip, type);
if (!stack.contains(PAGES) || stack.get(PAGES).isEmpty()) {
tooltip.add(Text.translatable("item.magisterium.spell_book.empty").formatted(Formatting.GRAY));
}
if (stack.contains(UNSTABLE_CHARGE)) {
tooltip.add(Text.translatable("item.magisterium.spell_book.unstable_charge").formatted(Formatting.GRAY));
}
}

@Override
public float getBonusAttackDamage(Entity target, float baseAttackDamage, DamageSource damageSource) {
var stack = damageSource.getWeaponStack();
if (stack != null && stack.contains(UNSTABLE_CHARGE)) {
return baseAttackDamage + 9;
}
return super.getBonusAttackDamage(target, baseAttackDamage, damageSource);
}

@Override
public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
if (stack.contains(UNSTABLE_CHARGE)) {
stack.remove(UNSTABLE_CHARGE);
if (target.isDead() && target.getWorld().getRandom().nextFloat() < .5) {
if (target.getType() == EntityType.ZOMBIE) {
target.dropItem(Items.ZOMBIE_HEAD);
} else if (target.getType() == EntityType.SKELETON) {
target.dropItem(Items.SKELETON_SKULL);
} else if (target.getType() == EntityType.CREEPER) {
target.dropItem(Items.CREEPER_HEAD);
} else if (target.getType() == EntityType.PLAYER) {
var player = (PlayerEntity) target;
var head = new ItemStack(Items.PLAYER_HEAD);
head.set(DataComponentTypes.PROFILE, new ProfileComponent(player.getGameProfile()));
target.dropStack(head);
}
}
// TODO sent a packet to spawn particles and play a sound
}
return super.postHit(stack, target, attacker);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -49,10 +50,12 @@ public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup

if (!hasTargets) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_targets"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
} else if (hasFailed && hasLit) {
input.player.sendMessage(Text.translatable("magisterium.gui.partial_success"), true);
} else if (hasFailed) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_success"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
}

return ItemStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -81,10 +82,12 @@ public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup

if (!hasTargets) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_targets"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
} else if (hasFailed && hasLit) {
input.player.sendMessage(Text.translatable("magisterium.gui.partial_success"), true);
} else if (hasFailed) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_success"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
}

return ItemStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -48,10 +49,12 @@ public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup

if (!hasTargets) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_glyphs_found"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
} else if (hasFailed && hasLit) {
input.player.sendMessage(Text.translatable("magisterium.gui.partial_success"), true);
} else if (hasFailed) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_success"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
}

return ItemStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import io.github.reoseah.magisterium.block.GlyphBlock;
import io.github.reoseah.magisterium.block.IllusoryWallBlock;
import io.github.reoseah.magisterium.block.IllusoryWallBlockEntity;
import net.minecraft.block.BlockRenderType;
import net.minecraft.block.Blocks;
import net.minecraft.item.BlockItem;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
Expand Down Expand Up @@ -50,8 +52,9 @@ public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup
illusionState = blockItem.getBlock().getDefaultState();
}

if (illusionState.isAir()) {
if (illusionState.isAir() || illusionState.getRenderType() != BlockRenderType.MODEL) {
input.player.sendMessage(Text.translatable("magisterium.gui.invalid_illusion_block"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
return ItemStack.EMPTY;
}

Expand All @@ -62,6 +65,7 @@ public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup

if (startPos == null) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_glyphs_found"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
return ItemStack.EMPTY;
}

Expand Down Expand Up @@ -111,6 +115,7 @@ public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup
input.player.sendMessage(Text.translatable("magisterium.gui.partial_success"), true);
} else if (hasFailure) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_success"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
}

return ItemStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.property.Properties;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
Expand Down Expand Up @@ -50,10 +51,12 @@ public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup

if (!hasTargets) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_targets"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
} else if (hasFailed && hasQuenched) {
input.player.sendMessage(Text.translatable("magisterium.gui.partial_success"), true);
} else if (hasFailed) {
input.player.sendMessage(Text.translatable("magisterium.gui.no_success"), true);
((ServerPlayerEntity) input.player).closeHandledScreen();
}

return ItemStack.EMPTY;
Expand Down
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.screen.SpellBookScreenHandler;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
Expand All @@ -8,10 +9,12 @@
public class SpellBookRecipeInput implements RecipeInput {
protected final Inventory inventory;
protected final PlayerEntity player;
protected final SpellBookScreenHandler.Context context;

public SpellBookRecipeInput(Inventory inventory, PlayerEntity player) {
public SpellBookRecipeInput(Inventory inventory, PlayerEntity player, SpellBookScreenHandler.Context context) {
this.inventory = inventory;
this.player = player;
this.context = context;
}

@Override
Expand All @@ -31,4 +34,8 @@ public ItemStack removeStack(int slot, int count) {
public PlayerEntity getPlayer() {
return this.player;
}

public SpellBookScreenHandler.Context getContext() {
return this.context;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.reoseah.magisterium.recipe;

import io.github.reoseah.magisterium.item.SpellBookItem;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.minecraft.util.Unit;
import net.minecraft.world.World;

public class UnstableChargeRecipe extends SpellBookRecipe {
public static final RecipeSerializer<UnstableChargeRecipe> SERIALIZER = new SpellBookRecipe.SimpleSerializer<>(UnstableChargeRecipe::new);

protected UnstableChargeRecipe(Identifier utterance, int duration) {
super(utterance, duration);
}

@Override
public boolean matches(SpellBookRecipeInput input, World world) {
// TODO: require ingredients, like a Nether Wart and a Bottle o' Enchanting
return true;
}

@Override
public ItemStack craft(SpellBookRecipeInput input, RegistryWrapper.WrapperLookup lookup) {
input.getContext().setStackComponent(SpellBookItem.UNSTABLE_CHARGE, Unit.INSTANCE);

return ItemStack.EMPTY;
}

@Override
public ItemStack getResult(RegistryWrapper.WrapperLookup registriesLookup) {
return ItemStack.EMPTY;
}

@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
}
Loading

0 comments on commit d3abe96

Please sign in to comment.