Skip to content

Commit

Permalink
fixed disenchanting enchanted books in midnight solution resulting in…
Browse files Browse the repository at this point in the history
… enchanted books without enchants
DaFuqs committed Nov 13, 2023
1 parent 381f098 commit efabb05
Showing 3 changed files with 49 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -29,29 +29,29 @@
import java.util.*;

public class MidnightSolutionFluidBlock extends SpectrumFluidBlock {

public static final BlockState SPREAD_BLOCKSTATE = SpectrumBlocks.BLACK_MATERIA.getDefaultState().with(BlackMateriaBlock.AGE, 0);
private static final int EXPERIENCE_DISENCHANT_RETURN_DIV = 3;

public MidnightSolutionFluidBlock(FlowableFluid fluid, Settings settings) {
super(fluid, settings);
}

@Override
public DefaultParticleType getSplashParticle() {
return SpectrumParticleTypes.MIDNIGHT_SOLUTION_SPLASH;
}

@Override
public Pair<DefaultParticleType, DefaultParticleType> getFishingParticles() {
return new Pair<>(SpectrumParticleTypes.GRAY_SPARKLE_RISING, SpectrumParticleTypes.MIDNIGHT_SOLUTION_FISHING);
}

@Override
public RecipeType<? extends FluidConvertingRecipe> getDippingRecipeType() {
return SpectrumRecipeTypes.MIDNIGHT_SOLUTION_CONVERTING;
}

public static boolean tryConvertNeighbor(@NotNull World world, BlockPos pos, BlockPos fromPos) {
FluidState fluidState = world.getFluidState(fromPos);
if (!fluidState.isEmpty() && fluidState.isIn(SpectrumFluidTags.MIDNIGHT_SOLUTION_CONVERTED)) {
@@ -61,29 +61,29 @@ public static boolean tryConvertNeighbor(@NotNull World world, BlockPos pos, Blo
}
return false;
}

public static void playExtinguishSound(@NotNull WorldAccess world, BlockPos pos) {
world.syncWorldEvent(1501, pos, 0);
}

@Override
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
if (this.receiveNeighborFluids(world, pos, state)) {
world.createAndScheduleFluidTick(pos, state.getFluidState().getFluid(), this.fluid.getTickRate(world));
}
}

@Override
public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) {
if (this.receiveNeighborFluids(world, pos, state)) {
world.createAndScheduleFluidTick(pos, state.getFluidState().getFluid(), this.fluid.getTickRate(world));
}
}

@Override
public void onEntityCollision(BlockState state, World world, BlockPos pos, Entity entity) {
super.onEntityCollision(state, world, pos, entity);

if (!world.isClient) {
if (entity instanceof LivingEntity livingEntity) {
if (!livingEntity.isDead() && world.getTime() % 20 == 0) {
@@ -104,45 +104,43 @@ public void onEntityCollision(BlockState state, World world, BlockPos pos, Entit
}
}
}

private static void disenchantItemAndSpawnXP(World world, ItemEntity itemEntity) {
ItemStack itemStack = itemEntity.getStack();
// if the item is enchanted: remove enchantments and spawn XP
// basically disenchanting the item
if (itemStack.hasEnchantments() || itemStack.isOf(Items.ENCHANTED_BOOK)) {
Map<Enchantment, Integer> enchantments = EnchantmentHelper.get(itemStack);
if (enchantments.size() > 0) {
int randomEnchantmentIndex = world.random.nextInt(enchantments.size());
Enchantment enchantmentToRemove = (Enchantment) enchantments.keySet().toArray()[randomEnchantmentIndex];

int experience = EnchanterBlockEntity.getEnchantingPrice(itemStack, enchantmentToRemove, enchantments.get(enchantmentToRemove));
experience /= EXPERIENCE_DISENCHANT_RETURN_DIV;

if (experience > 0) {
ExperienceOrbEntity experienceOrbEntity = new ExperienceOrbEntity(world, itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(), experience);
world.spawnEntity(experienceOrbEntity);
}
world.playSound(null, itemEntity.getBlockPos(), SoundEvents.BLOCK_GRINDSTONE_USE, SoundCategory.NEUTRAL, 1.0F, 0.9F + world.getRandom().nextFloat() * 0.2F);
SpectrumS2CPacketSender.playParticleWithRandomOffsetAndVelocity((ServerWorld) world, itemEntity.getPos(), SpectrumParticleTypes.GRAY_SPARKLE_RISING, 10, Vec3d.ZERO, new Vec3d(0.2, 0.4, 0.2));
SpectrumEnchantmentHelper.removeEnchantment(itemStack, enchantmentToRemove);
itemEntity.setToDefaultPickupDelay();
Map<Enchantment, Integer> enchantments = EnchantmentHelper.get(itemStack);
if (!enchantments.isEmpty()) {
int randomEnchantmentIndex = world.random.nextInt(enchantments.size());
Enchantment enchantmentToRemove = (Enchantment) enchantments.keySet().toArray()[randomEnchantmentIndex];

int experience = EnchanterBlockEntity.getEnchantingPrice(itemStack, enchantmentToRemove, enchantments.get(enchantmentToRemove));
experience /= EXPERIENCE_DISENCHANT_RETURN_DIV;

if (experience > 0) {
ExperienceOrbEntity experienceOrbEntity = new ExperienceOrbEntity(world, itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(), experience);
world.spawnEntity(experienceOrbEntity);
}
world.playSound(null, itemEntity.getBlockPos(), SoundEvents.BLOCK_GRINDSTONE_USE, SoundCategory.NEUTRAL, 1.0F, 0.9F + world.getRandom().nextFloat() * 0.2F);
SpectrumS2CPacketSender.playParticleWithRandomOffsetAndVelocity((ServerWorld) world, itemEntity.getPos(), SpectrumParticleTypes.GRAY_SPARKLE_RISING, 10, Vec3d.ZERO, new Vec3d(0.2, 0.4, 0.2));
itemEntity.setStack(SpectrumEnchantmentHelper.removeEnchantment(itemStack, enchantmentToRemove).getLeft());
itemEntity.setToDefaultPickupDelay();
}
}

@Override
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {
super.randomDisplayTick(state, world, pos, random);
if (!world.getBlockState(pos.up()).isSolidBlock(world, pos.up()) && random.nextFloat() < 0.03F) {
world.addParticle(SpectrumParticleTypes.VOID_FOG, pos.getX() + random.nextDouble(), pos.getY() + 1, pos.getZ() + random.nextDouble(), 0, random.nextDouble() * 0.1, 0);
}
}

@Override
public boolean canPathfindThrough(BlockState state, BlockView world, BlockPos pos, NavigationType type) {
return false;
}

/**
* @param world The world
* @param pos The position in the world
@@ -175,5 +173,5 @@ private boolean receiveNeighborFluids(World world, BlockPos pos, BlockState stat
}
return true;
}

}
Original file line number Diff line number Diff line change
@@ -182,18 +182,23 @@ public static boolean canCombineAny(Map<Enchantment, Integer> existingEnchantmen
* @param enchantments the enchantments to remove
* @return if >0 enchantments could be removed
*/
public static boolean removeEnchantments(@NotNull ItemStack itemStack, Enchantment... enchantments) {
public static Pair<ItemStack, Boolean> removeEnchantments(@NotNull ItemStack itemStack, Enchantment... enchantments) {
boolean anySuccess = false;
for (Enchantment enchantment : enchantments) {
anySuccess |= removeEnchantment(itemStack, enchantment);
Pair<ItemStack, Boolean> result = removeEnchantment(itemStack, enchantment);
anySuccess = result.getRight();
itemStack = result.getLeft();
}
return anySuccess;
return new Pair(itemStack, anySuccess);
}

public static boolean removeEnchantment(@NotNull ItemStack itemStack, Enchantment enchantment) {

/**
* @return The resulting stack & a boolean to specify if removing was successful
*/
public static Pair<ItemStack, Boolean> removeEnchantment(@NotNull ItemStack itemStack, Enchantment enchantment) {
NbtCompound compound = itemStack.getNbt();
if (compound == null) {
return false;
return new Pair<>(itemStack, false);
}

NbtList enchantmentList;
@@ -215,14 +220,19 @@ public static boolean removeEnchantment(@NotNull ItemStack itemStack, Enchantmen
}

if (itemStack.isOf(Items.ENCHANTED_BOOK)) {
if(enchantmentList.isEmpty()) {
ItemStack newStack = new ItemStack(Items.BOOK);
newStack.setCount(itemStack.getCount());
return new Pair<>(newStack, true);
}
compound.put(EnchantedBookItem.STORED_ENCHANTMENTS_KEY, enchantmentList);
} else {
compound.put(ItemStack.ENCHANTMENTS_KEY, enchantmentList);

}
itemStack.setNbt(compound);

return success;
return new Pair<>(itemStack, true);
}

public static <T extends Item & ExtendedEnchantable> ItemStack getMaxEnchantedStack(@NotNull T item) {
Original file line number Diff line number Diff line change
@@ -193,15 +193,14 @@ private static void enchantAndRemoveOthers(PlayerEntity player, ItemStack stack,
}
}

if (SpectrumEnchantmentHelper.removeEnchantments(stack, Enchantments.SILK_TOUCH, SpectrumEnchantments.RESONANCE, Enchantments.FORTUNE)) {
if (SpectrumEnchantmentHelper.removeEnchantments(stack, Enchantments.SILK_TOUCH, SpectrumEnchantments.RESONANCE, Enchantments.FORTUNE).getRight()) {
SpectrumEnchantmentHelper.addOrExchangeEnchantment(stack, enchantment, level, true, true);
player.sendMessage(message, true);
} else if (player instanceof ServerPlayerEntity serverPlayerEntity) {
triggerUnenchantedWorkstaffAdvancement(serverPlayerEntity);
}
}


private static void triggerUnenchantedWorkstaffAdvancement(ServerPlayerEntity player) {
player.playSound(SpectrumSoundEvents.USE_FAIL, SoundCategory.PLAYERS, 0.75F, 1.0F);
Support.grantAdvancementCriterion(player, "lategame/trigger_unenchanted_workstaff", "code_triggered");

0 comments on commit efabb05

Please sign in to comment.