<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.registry.item.tool;

import com.skelril.nitro.registry.Craftable;
import com.skelril.nitro.registry.item.CustomItem;
import com.skelril.nitro.selector.EventAwareContent;
import com.skelril.skree.content.registry.item.CustomItemTypes;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.List;
import java.util.Optional;

import static com.skelril.nitro.transformer.ForgeTransformer.tf;

public class FocusTeleporter extends CustomItem implements Craftable, EventAwareContent {

    @Override
    public String __getID() {
        return "focus_teleporter";
    }

    @Override
    public int __getMaxStackSize() {
        return 1;
    }

    @Override
    public CreativeTabs __getCreativeTab() {
        return CreativeTabs.tabTools;
    }

    @Override
    public void registerRecipes() {
        GameRegistry.addRecipe(
                new ItemStack(this),
                "AAA",
                "BCB",
                "BBB",
                'A', new ItemStack(CustomItemTypes.RED_SHARD),
                'B', new ItemStack(CustomItemTypes.SEA_CRYSTAL),
                'C', new ItemStack(CustomItemTypes.ENDER_FOCUS)
        );
    }

    @Listener
    public void onLeftClick(InteractBlockEvent.Primary event) {
        Optional<Player> optPlayer = event.getCause().first(Player.class);

        if (!optPlayer.isPresent()) return;

        Player player = optPlayer.get();

        Optional<org.spongepowered.api.item.inventory.ItemStack> optHeldItem = player.getItemInHand();

        if (optHeldItem.isPresent()) {
            org.spongepowered.api.item.inventory.ItemStack held = optHeldItem.get();
            if (held.getItem() == this) {
                setDestination(held, player.getLocation());
                player.setItemInHand(held);
            }
        }
    }

    @Listener
    public void onRightClick(InteractBlockEvent.Secondary event) {
        Optional<Player> optPlayer = event.getCause().first(Player.class);

        if (!optPlayer.isPresent()) return;

        Player player = optPlayer.get();

        Optional<org.spongepowered.api.item.inventory.ItemStack> optHeldItem = player.getItemInHand();

        if (optHeldItem.isPresent()) {
            org.spongepowered.api.item.inventory.ItemStack held = optHeldItem.get();
            if (held.getItem() == this) {
                Optional<Location<World>> optDestination = getDestination(held);
                InventoryPlayer playerInv = tf(player).inventory;
                if (optDestination.isPresent() && playerInv.hasItem(CustomItemTypes.ENDER_FOCUS)) {
                    playerInv.consumeInventoryItem(CustomItemTypes.ENDER_FOCUS);
                    tf(player).inventoryContainer.detectAndSendChanges();
                    player.setLocation(optDestination.get());
                }
            }
        }
    }

    private void setDestination(org.spongepowered.api.item.inventory.ItemStack stack, Location<World> target) {
        setDestination(tf(stack), target);
    }

    private void setDestination(ItemStack stack, Location<World> target) {
        if (stack.getTagCompound() == null) {
            stack.setTagCompound(new NBTTagCompound());
        }

        if (!stack.getTagCompound().hasKey("skree_dest_data")) {
            stack.getTagCompound().setTag("skree_dest_data", new NBTTagCompound());
        }

        NBTTagCompound tag = stack.getTagCompound().getCompoundTag("skree_dest_data");
        tag.setString("world", target.getExtent().getName());
        tag.setDouble("x", target.getX());
        tag.setDouble("y", target.getY());
        tag.setDouble("z", target.getZ());
    }

    private Optional<Location<World>> getDestination(org.spongepowered.api.item.inventory.ItemStack stack) {
        return getDestination(tf(stack));
    }

    private Optional<Location<World>> getDestination(ItemStack stack) {
        if (stack.getTagCompound() == null || !stack.getTagCompound().hasKey("skree_dest_data")) {
            return Optional.empty();
        }

        NBTTagCompound tag = stack.getTagCompound().getCompoundTag("skree_dest_data");
        String worldName = tag.getString("world");
        double x = tag.getDouble("x");
        double y = tag.getDouble("y");
        double z = tag.getDouble("z");
        Optional<World> optWorld = Sponge.getServer().getWorld(worldName);
        if (optWorld.isPresent()) {
            return Optional.of(new Location<>(optWorld.get(), x, y, z));
        }
        return Optional.empty();
    }

    private Optional<String> getClientDestination(org.spongepowered.api.item.inventory.ItemStack stack) {
        return getClientDestination(tf(stack));
    }

    private Optional<String> getClientDestination(ItemStack stack) {
        if (stack.getTagCompound() == null || !stack.getTagCompound().hasKey("skree_dest_data")) {
            return Optional.empty();
        }

        NBTTagCompound tag = stack.getTagCompound().getCompoundTag("skree_dest_data");
        String worldName = tag.getString("world");
        double x = tag.getDouble("x");
        double y = tag.getDouble("y");
        double z = tag.getDouble("z");

        return Optional.of(worldName + " at " + (int) x + ", " + (int) y + ", " + (int) z);
    }


    // Modified Native Item methods

    @SuppressWarnings("unchecked")
    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, EntityPlayer playerIn, List tooltip, boolean advanced) {
        Optional<String> optDestStr = getClientDestination(stack);
        tooltip.add("Destination: " + (optDestStr.isPresent() ? optDestStr.get() : "Not set"));
    }
}
<commit_msg>Prevent the Focus Teleporter from breaking things<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.registry.item.tool;

import com.skelril.nitro.registry.Craftable;
import com.skelril.nitro.registry.item.CustomItem;
import com.skelril.nitro.selector.EventAwareContent;
import com.skelril.skree.content.registry.item.CustomItemTypes;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.registry.GameRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.block.InteractBlockEvent;
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.World;

import java.util.List;
import java.util.Optional;

import static com.skelril.nitro.transformer.ForgeTransformer.tf;

public class FocusTeleporter extends CustomItem implements Craftable, EventAwareContent {

    @Override
    public String __getID() {
        return "focus_teleporter";
    }

    @Override
    public int __getMaxStackSize() {
        return 1;
    }

    @Override
    public CreativeTabs __getCreativeTab() {
        return CreativeTabs.tabTools;
    }

    @Override
    public void registerRecipes() {
        GameRegistry.addRecipe(
                new ItemStack(this),
                "AAA",
                "BCB",
                "BBB",
                'A', new ItemStack(CustomItemTypes.RED_SHARD),
                'B', new ItemStack(CustomItemTypes.SEA_CRYSTAL),
                'C', new ItemStack(CustomItemTypes.ENDER_FOCUS)
        );
    }

    @Listener
    public void onLeftClick(InteractBlockEvent.Primary event) {
        Optional<Player> optPlayer = event.getCause().first(Player.class);

        if (!optPlayer.isPresent()) return;

        Player player = optPlayer.get();

        Optional<org.spongepowered.api.item.inventory.ItemStack> optHeldItem = player.getItemInHand();

        if (optHeldItem.isPresent()) {
            org.spongepowered.api.item.inventory.ItemStack held = optHeldItem.get();
            if (held.getItem() == this) {
                setDestination(held, player.getLocation());
                player.setItemInHand(held);
                event.setCancelled(true);
            }
        }
    }

    @Listener
    public void onRightClick(InteractBlockEvent.Secondary event) {
        Optional<Player> optPlayer = event.getCause().first(Player.class);

        if (!optPlayer.isPresent()) return;

        Player player = optPlayer.get();

        Optional<org.spongepowered.api.item.inventory.ItemStack> optHeldItem = player.getItemInHand();

        if (optHeldItem.isPresent()) {
            org.spongepowered.api.item.inventory.ItemStack held = optHeldItem.get();
            if (held.getItem() == this) {
                Optional<Location<World>> optDestination = getDestination(held);
                InventoryPlayer playerInv = tf(player).inventory;
                if (optDestination.isPresent() && playerInv.hasItem(CustomItemTypes.ENDER_FOCUS)) {
                    playerInv.consumeInventoryItem(CustomItemTypes.ENDER_FOCUS);
                    tf(player).inventoryContainer.detectAndSendChanges();
                    player.setLocation(optDestination.get());
                    event.setCancelled(true);
                }
            }
        }
    }

    private void setDestination(org.spongepowered.api.item.inventory.ItemStack stack, Location<World> target) {
        setDestination(tf(stack), target);
    }

    private void setDestination(ItemStack stack, Location<World> target) {
        if (stack.getTagCompound() == null) {
            stack.setTagCompound(new NBTTagCompound());
        }

        if (!stack.getTagCompound().hasKey("skree_dest_data")) {
            stack.getTagCompound().setTag("skree_dest_data", new NBTTagCompound());
        }

        NBTTagCompound tag = stack.getTagCompound().getCompoundTag("skree_dest_data");
        tag.setString("world", target.getExtent().getName());
        tag.setDouble("x", target.getX());
        tag.setDouble("y", target.getY());
        tag.setDouble("z", target.getZ());
    }

    private Optional<Location<World>> getDestination(org.spongepowered.api.item.inventory.ItemStack stack) {
        return getDestination(tf(stack));
    }

    private Optional<Location<World>> getDestination(ItemStack stack) {
        if (stack.getTagCompound() == null || !stack.getTagCompound().hasKey("skree_dest_data")) {
            return Optional.empty();
        }

        NBTTagCompound tag = stack.getTagCompound().getCompoundTag("skree_dest_data");
        String worldName = tag.getString("world");
        double x = tag.getDouble("x");
        double y = tag.getDouble("y");
        double z = tag.getDouble("z");
        Optional<World> optWorld = Sponge.getServer().getWorld(worldName);
        if (optWorld.isPresent()) {
            return Optional.of(new Location<>(optWorld.get(), x, y, z));
        }
        return Optional.empty();
    }

    private Optional<String> getClientDestination(org.spongepowered.api.item.inventory.ItemStack stack) {
        return getClientDestination(tf(stack));
    }

    private Optional<String> getClientDestination(ItemStack stack) {
        if (stack.getTagCompound() == null || !stack.getTagCompound().hasKey("skree_dest_data")) {
            return Optional.empty();
        }

        NBTTagCompound tag = stack.getTagCompound().getCompoundTag("skree_dest_data");
        String worldName = tag.getString("world");
        double x = tag.getDouble("x");
        double y = tag.getDouble("y");
        double z = tag.getDouble("z");

        return Optional.of(worldName + " at " + (int) x + ", " + (int) y + ", " + (int) z);
    }


    // Modified Native Item methods

    @SuppressWarnings("unchecked")
    @SideOnly(Side.CLIENT)
    public void addInformation(ItemStack stack, EntityPlayer playerIn, List tooltip, boolean advanced) {
        Optional<String> optDestStr = getClientDestination(stack);
        tooltip.add("Destination: " + (optDestStr.isPresent() ? optDestStr.get() : "Not set"));
    }
}

{'fraction_non_alphanumeric': 0.12023191943851083, 'fraction_numerical': 0.0007628928898382667, 'mean_word_length': 2.9496836396504973, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3112953', 'n_tokens_mistral': 3917, 'n_tokens_neox': 3828, 'n_words': 674}