Search in sources :

Example 1 with IWorldPosCallableBridge

use of io.izzel.arclight.common.bridge.util.IWorldPosCallableBridge in project Arclight by IzzelAliz.

the class EnchantmentContainerMixin method onCraftMatrixChanged.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void onCraftMatrixChanged(IInventory inventoryIn) {
    if (inventoryIn == this.tableInventory) {
        ItemStack itemstack = inventoryIn.getStackInSlot(0);
        if (!itemstack.isEmpty()) {
            this.worldPosCallable.consume((p_217002_2_, p_217002_3_) -> {
                float power = 0;
                for (int k = -1; k <= 1; ++k) {
                    for (int l = -1; l <= 1; ++l) {
                        if ((k != 0 || l != 0) && p_217002_2_.isAirBlock(p_217002_3_.add(l, 0, k)) && p_217002_2_.isAirBlock(p_217002_3_.add(l, 1, k))) {
                            power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 0, k * 2));
                            power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 1, k * 2));
                            if (l != 0 && k != 0) {
                                power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 0, k));
                                power += getPower(p_217002_2_, p_217002_3_.add(l * 2, 1, k));
                                power += getPower(p_217002_2_, p_217002_3_.add(l, 0, k * 2));
                                power += getPower(p_217002_2_, p_217002_3_.add(l, 1, k * 2));
                            }
                        }
                    }
                }
                this.rand.setSeed(this.xpSeed.get());
                for (int i1 = 0; i1 < 3; ++i1) {
                    this.enchantLevels[i1] = EnchantmentHelper.calcItemStackEnchantability(this.rand, i1, (int) power, itemstack);
                    this.enchantClue[i1] = -1;
                    this.worldClue[i1] = -1;
                    if (this.enchantLevels[i1] < i1 + 1) {
                        this.enchantLevels[i1] = 0;
                    }
                    this.enchantLevels[i1] = ForgeEventFactory.onEnchantmentLevelSet(p_217002_2_, p_217002_3_, i1, (int) power, itemstack, enchantLevels[i1]);
                }
                for (int j1 = 0; j1 < 3; ++j1) {
                    if (this.enchantLevels[j1] > 0) {
                        List<EnchantmentData> list = this.getEnchantmentList(itemstack, j1, this.enchantLevels[j1]);
                        if (list != null && !list.isEmpty()) {
                            EnchantmentData enchantmentdata = list.get(this.rand.nextInt(list.size()));
                            this.enchantClue[j1] = Registry.ENCHANTMENT.getId(enchantmentdata.enchantment);
                            this.worldClue[j1] = enchantmentdata.enchantmentLevel;
                        }
                    }
                }
                CraftItemStack item = CraftItemStack.asCraftMirror(itemstack);
                org.bukkit.enchantments.EnchantmentOffer[] offers = new EnchantmentOffer[3];
                for (int j = 0; j < 3; ++j) {
                    org.bukkit.enchantments.Enchantment enchantment = (this.enchantClue[j] >= 0) ? org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(ForgeRegistries.ENCHANTMENTS.getKey(Registry.ENCHANTMENT.getByValue(this.enchantClue[j])))) : null;
                    offers[j] = (enchantment != null) ? new EnchantmentOffer(enchantment, this.worldClue[j], this.enchantLevels[j]) : null;
                }
                PrepareItemEnchantEvent event = new PrepareItemEnchantEvent(((ServerPlayerEntityBridge) this.playerInventory.player).bridge$getBukkitEntity(), this.getBukkitView(), ((IWorldPosCallableBridge) this.worldPosCallable).bridge$getLocation().getBlock(), item, offers, (int) power);
                event.setCancelled(!itemstack.isEnchantable());
                Bukkit.getPluginManager().callEvent(event);
                if (event.isCancelled()) {
                    for (int j = 0; j < 3; ++j) {
                        this.enchantLevels[j] = 0;
                        this.enchantClue[j] = -1;
                        this.worldClue[j] = -1;
                    }
                    return;
                }
                for (int j = 0; j < 3; j++) {
                    EnchantmentOffer offer = event.getOffers()[j];
                    if (offer != null) {
                        this.enchantLevels[j] = offer.getCost();
                        this.enchantClue[j] = Registry.ENCHANTMENT.getId(ForgeRegistries.ENCHANTMENTS.getValue(CraftNamespacedKey.toMinecraft(offer.getEnchantment().getKey())));
                        this.worldClue[j] = offer.getEnchantmentLevel();
                    } else {
                        this.enchantLevels[j] = 0;
                        this.enchantClue[j] = -1;
                        this.worldClue[j] = -1;
                    }
                }
                this.detectAndSendChanges();
            });
        } else {
            for (int i = 0; i < 3; ++i) {
                this.enchantLevels[i] = 0;
                this.enchantClue[i] = -1;
                this.worldClue[i] = -1;
            }
        }
    }
}
Also used : IWorldPosCallableBridge(io.izzel.arclight.common.bridge.util.IWorldPosCallableBridge) Enchantment(org.bukkit.enchantments.Enchantment) PrepareItemEnchantEvent(org.bukkit.event.enchantment.PrepareItemEnchantEvent) EnchantmentData(net.minecraft.enchantment.EnchantmentData) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) EnchantmentOffer(org.bukkit.enchantments.EnchantmentOffer) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with IWorldPosCallableBridge

use of io.izzel.arclight.common.bridge.util.IWorldPosCallableBridge in project Arclight by IzzelAliz.

the class EnchantmentContainerMixin method enchantItem.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public boolean enchantItem(PlayerEntity playerIn, int id) {
    ItemStack itemstack = this.tableInventory.getStackInSlot(0);
    ItemStack itemstack1 = this.tableInventory.getStackInSlot(1);
    int i = id + 1;
    if ((itemstack1.isEmpty() || itemstack1.getCount() < i) && !playerIn.abilities.isCreativeMode) {
        return false;
    } else if (this.enchantLevels[id] <= 0 || itemstack.isEmpty() || (playerIn.experienceLevel < i || playerIn.experienceLevel < this.enchantLevels[id]) && !playerIn.abilities.isCreativeMode) {
        return false;
    } else {
        this.worldPosCallable.consume((p_217003_6_, p_217003_7_) -> {
            ItemStack itemstack2 = itemstack;
            List<EnchantmentData> list = this.getEnchantmentList(itemstack, id, this.enchantLevels[id]);
            if (true || !list.isEmpty()) {
                // playerIn.onEnchant(itemstack, i);
                boolean flag = itemstack.getItem() == Items.BOOK;
                Map<Enchantment, Integer> enchants = new java.util.HashMap<>();
                for (EnchantmentData obj : list) {
                    enchants.put(org.bukkit.enchantments.Enchantment.getByKey(CraftNamespacedKey.fromMinecraft(ForgeRegistries.ENCHANTMENTS.getKey(obj.enchantment))), obj.enchantmentLevel);
                }
                CraftItemStack item = CraftItemStack.asCraftMirror(itemstack2);
                EnchantItemEvent event = new EnchantItemEvent(((Player) ((PlayerEntityBridge) playerIn).bridge$getBukkitEntity()), this.getBukkitView(), ((IWorldPosCallableBridge) this.worldPosCallable).bridge$getLocation().getBlock(), item, this.enchantLevels[id], enchants, id);
                Bukkit.getPluginManager().callEvent(event);
                int level = event.getExpLevelCost();
                if (event.isCancelled() || (level > playerIn.experienceLevel && !playerIn.abilities.isCreativeMode) || event.getEnchantsToAdd().isEmpty()) {
                    return;
                }
                if (flag) {
                    itemstack2 = new ItemStack(Items.ENCHANTED_BOOK);
                    this.tableInventory.setInventorySlotContents(0, itemstack2);
                }
                for (Map.Entry<org.bukkit.enchantments.Enchantment, Integer> entry : event.getEnchantsToAdd().entrySet()) {
                    try {
                        if (flag) {
                            NamespacedKey enchantId = entry.getKey().getKey();
                            net.minecraft.enchantment.Enchantment nms = ForgeRegistries.ENCHANTMENTS.getValue(CraftNamespacedKey.toMinecraft(enchantId));
                            if (nms == null) {
                                continue;
                            }
                            EnchantmentData weightedrandomenchant = new EnchantmentData(nms, entry.getValue());
                            EnchantedBookItem.addEnchantment(itemstack2, weightedrandomenchant);
                        } else {
                            item.addUnsafeEnchantment(entry.getKey(), entry.getValue());
                        }
                    } catch (IllegalArgumentException e) {
                    /* Just swallow invalid enchantments */
                    }
                }
                playerIn.onEnchant(itemstack, i);
                if (!playerIn.abilities.isCreativeMode) {
                    itemstack1.shrink(i);
                    if (itemstack1.isEmpty()) {
                        this.tableInventory.setInventorySlotContents(1, ItemStack.EMPTY);
                    }
                }
                playerIn.addStat(Stats.ENCHANT_ITEM);
                if (playerIn instanceof ServerPlayerEntity) {
                    CriteriaTriggers.ENCHANTED_ITEM.trigger((ServerPlayerEntity) playerIn, itemstack2, i);
                }
                this.tableInventory.markDirty();
                this.xpSeed.set(playerIn.getXPSeed());
                this.onCraftMatrixChanged(this.tableInventory);
                p_217003_6_.playSound((PlayerEntity) null, p_217003_7_, SoundEvents.BLOCK_ENCHANTMENT_TABLE_USE, SoundCategory.BLOCKS, 1.0F, p_217003_6_.rand.nextFloat() * 0.1F + 0.9F);
            }
        });
        return true;
    }
}
Also used : PrepareItemEnchantEvent(org.bukkit.event.enchantment.PrepareItemEnchantEvent) EnchantedBookItem(net.minecraft.item.EnchantedBookItem) ForgeEventFactory(net.minecraftforge.event.ForgeEventFactory) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) Enchantment(org.bukkit.enchantments.Enchantment) Inject(org.spongepowered.asm.mixin.injection.Inject) Container(net.minecraft.inventory.container.Container) EnchantmentContainer(net.minecraft.inventory.container.EnchantmentContainer) Random(java.util.Random) Player(org.bukkit.entity.Player) Overwrite(org.spongepowered.asm.mixin.Overwrite) IntReferenceHolder(net.minecraft.util.IntReferenceHolder) IWorldPosCallableBridge(io.izzel.arclight.common.bridge.util.IWorldPosCallableBridge) CallbackInfoReturnable(org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable) ItemStack(net.minecraft.item.ItemStack) CallbackInfo(org.spongepowered.asm.mixin.injection.callback.CallbackInfo) CriteriaTriggers(net.minecraft.advancements.CriteriaTriggers) Mixin(org.spongepowered.asm.mixin.Mixin) EnchantmentData(net.minecraft.enchantment.EnchantmentData) Map(java.util.Map) Stats(net.minecraft.stats.Stats) CraftInventoryView(org.bukkit.craftbukkit.v.inventory.CraftInventoryView) SoundCategory(net.minecraft.util.SoundCategory) IWorldPosCallable(net.minecraft.util.IWorldPosCallable) Bukkit(org.bukkit.Bukkit) At(org.spongepowered.asm.mixin.injection.At) PlayerInventory(net.minecraft.entity.player.PlayerInventory) EnchantItemEvent(org.bukkit.event.enchantment.EnchantItemEvent) PlayerEntity(net.minecraft.entity.player.PlayerEntity) NamespacedKey(org.bukkit.NamespacedKey) World(net.minecraft.world.World) CraftNamespacedKey(org.bukkit.craftbukkit.v.util.CraftNamespacedKey) BlockPos(net.minecraft.util.math.BlockPos) Items(net.minecraft.item.Items) Final(org.spongepowered.asm.mixin.Final) CraftInventoryEnchanting(org.bukkit.craftbukkit.v.inventory.CraftInventoryEnchanting) ServerPlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge) EnchantmentHelper(net.minecraft.enchantment.EnchantmentHelper) SoundEvents(net.minecraft.util.SoundEvents) Registry(net.minecraft.util.registry.Registry) EnchantmentContainerBridge(io.izzel.arclight.common.bridge.inventory.container.EnchantmentContainerBridge) List(java.util.List) IInventory(net.minecraft.inventory.IInventory) EnchantmentOffer(org.bukkit.enchantments.EnchantmentOffer) Shadow(org.spongepowered.asm.mixin.Shadow) PlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.PlayerEntityBridge) ForgeRegistries(net.minecraftforge.registries.ForgeRegistries) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) ServerPlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge) PlayerEntityBridge(io.izzel.arclight.common.bridge.entity.player.PlayerEntityBridge) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) EnchantmentData(net.minecraft.enchantment.EnchantmentData) PlayerEntity(net.minecraft.entity.player.PlayerEntity) ServerPlayerEntity(net.minecraft.entity.player.ServerPlayerEntity) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) EnchantItemEvent(org.bukkit.event.enchantment.EnchantItemEvent) NamespacedKey(org.bukkit.NamespacedKey) CraftNamespacedKey(org.bukkit.craftbukkit.v.util.CraftNamespacedKey) List(java.util.List) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) Map(java.util.Map) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

IWorldPosCallableBridge (io.izzel.arclight.common.bridge.util.IWorldPosCallableBridge)2 EnchantmentData (net.minecraft.enchantment.EnchantmentData)2 ItemStack (net.minecraft.item.ItemStack)2 CraftItemStack (org.bukkit.craftbukkit.v.inventory.CraftItemStack)2 Enchantment (org.bukkit.enchantments.Enchantment)2 EnchantmentOffer (org.bukkit.enchantments.EnchantmentOffer)2 PrepareItemEnchantEvent (org.bukkit.event.enchantment.PrepareItemEnchantEvent)2 PlayerEntityBridge (io.izzel.arclight.common.bridge.entity.player.PlayerEntityBridge)1 ServerPlayerEntityBridge (io.izzel.arclight.common.bridge.entity.player.ServerPlayerEntityBridge)1 EnchantmentContainerBridge (io.izzel.arclight.common.bridge.inventory.container.EnchantmentContainerBridge)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1 CriteriaTriggers (net.minecraft.advancements.CriteriaTriggers)1 EnchantmentHelper (net.minecraft.enchantment.EnchantmentHelper)1 PlayerEntity (net.minecraft.entity.player.PlayerEntity)1 PlayerInventory (net.minecraft.entity.player.PlayerInventory)1 ServerPlayerEntity (net.minecraft.entity.player.ServerPlayerEntity)1 IInventory (net.minecraft.inventory.IInventory)1 Container (net.minecraft.inventory.container.Container)1