Jump to content

CommandCore

Members
  • Posts

    37
  • Joined

  • Last visited

Posts posted by CommandCore

  1. 13 minutes ago, diesieben07 said:
    • Do not use @OnlyIn.
    • Custom entities must override Entity#createSpawnPacket and call NetworkHooks.getEntitySpawningPacket.
    • Use @Override when overriding methods.

    Solved. Removed @OnlyIn from HeartballEntity and added this to the end of my Entity class:

    @Override
        public IPacket<?> createSpawnPacket() {
            return NetworkHooks.getEntitySpawningPacket(this);
        }

    Thanks for the help,

     

    Cheers.

  2. 20 hours ago, diesieben07 said:

    Show more of your code.

    Sure:

     

    HeartballRenderer.java

    package com.agilapathy.twodotx.entities.heartball;
    
    import com.agilapathy.twodotx.TwoDotXMod;
    import com.mojang.blaze3d.matrix.MatrixStack;
    import com.mojang.blaze3d.vertex.IVertexBuilder;
    import net.minecraft.client.renderer.IRenderTypeBuffer;
    import net.minecraft.client.renderer.RenderType;
    import net.minecraft.client.renderer.entity.EntityRenderer;
    import net.minecraft.client.renderer.entity.EntityRendererManager;
    import net.minecraft.client.renderer.texture.OverlayTexture;
    import net.minecraft.util.ResourceLocation;
    import net.minecraft.util.math.BlockPos;
    import net.minecraft.util.math.vector.Matrix3f;
    import net.minecraft.util.math.vector.Matrix4f;
    import net.minecraft.util.math.vector.Vector3f;
    import net.minecraftforge.api.distmarker.Dist;
    import net.minecraftforge.api.distmarker.OnlyIn;
    import net.minecraftforge.fml.client.registry.IRenderFactory;
    
    import javax.annotation.Nullable;
    
    @OnlyIn(Dist.CLIENT)
    public class HeartballRenderer extends EntityRenderer<HeartballEntity> {
    
    
        private static final ResourceLocation HEARTBALL_TEXTURE = new ResourceLocation(TwoDotXMod.MOD_ID + ":textures/entity/heartball.png");
        private static final RenderType field_229044_e_ = RenderType.getEntityCutoutNoCull(HEARTBALL_TEXTURE);
    
        public HeartballRenderer(EntityRendererManager renderManagerIn) {
            super(renderManagerIn);
        }
    
        protected int getBlockLight(HeartballEntity entityIn, BlockPos partialTicks) {
            return 15;
        }
    
        public void render(HeartballEntity entityIn, float entityYaw, float partialTicks, MatrixStack matrixStackIn, IRenderTypeBuffer bufferIn, int packedLightIn) {
            matrixStackIn.push();
            matrixStackIn.scale(2.0F, 2.0F, 2.0F);
            matrixStackIn.rotate(this.renderManager.getCameraOrientation());
            matrixStackIn.rotate(Vector3f.YP.rotationDegrees(180.0F));
            MatrixStack.Entry matrixstack$entry = matrixStackIn.getLast();
            Matrix4f matrix4f = matrixstack$entry.getMatrix();
            Matrix3f matrix3f = matrixstack$entry.getNormal();
            IVertexBuilder ivertexbuilder = bufferIn.getBuffer(field_229044_e_);
            func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 0.0F, 0, 0, 1);
            func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 1.0F, 0, 1, 1);
            func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 1.0F, 1, 1, 0);
            func_229045_a_(ivertexbuilder, matrix4f, matrix3f, packedLightIn, 0.0F, 1, 0, 0);
            matrixStackIn.pop();
            super.render(entityIn, entityYaw, partialTicks, matrixStackIn, bufferIn, packedLightIn);
        }
    
        private static void func_229045_a_(IVertexBuilder p_229045_0_, Matrix4f p_229045_1_, Matrix3f p_229045_2_, int p_229045_3_, float p_229045_4_, int p_229045_5_, int p_229045_6_, int p_229045_7_) {
            p_229045_0_.pos(p_229045_1_, p_229045_4_ - 0.5F, (float)p_229045_5_ - 0.25F, 0.0F).color(255, 255, 255, 255).tex((float)p_229045_6_, (float)p_229045_7_).overlay(OverlayTexture.NO_OVERLAY).lightmap(p_229045_3_).normal(p_229045_2_, 0.0F, 1.0F, 0.0F).endVertex();
        }
    
        /**
         * Returns the location of an entity's texture.
         */
        @Override
        public ResourceLocation getEntityTexture(HeartballEntity entity) {
            return HEARTBALL_TEXTURE;
        }
    }

     

    HeartballEntity.java

    package com.agilapathy.twodotx.entities.heartball;
    
    import java.util.List;
    
    import com.agilapathy.twodotx.util.RegistryHandler;
    import net.minecraft.entity.*;
    import net.minecraft.entity.projectile.DamagingProjectileEntity;
    import net.minecraft.item.ItemStack;
    import net.minecraft.item.Items;
    import net.minecraft.particles.IParticleData;
    import net.minecraft.particles.ParticleTypes;
    import net.minecraft.potion.EffectInstance;
    import net.minecraft.potion.Effects;
    import net.minecraft.util.DamageSource;
    import net.minecraft.util.math.EntityRayTraceResult;
    import net.minecraft.util.math.RayTraceResult;
    import net.minecraft.world.World;
    import net.minecraftforge.api.distmarker.Dist;
    import net.minecraftforge.api.distmarker.OnlyIn;
    
    public class HeartballEntity extends DamagingProjectileEntity{
        public HeartballEntity(EntityType<? extends HeartballEntity> p_i50171_1_, World p_i50171_2_) {
            super(p_i50171_1_, p_i50171_2_);
        }
    
        @OnlyIn(Dist.CLIENT)
        public HeartballEntity(World worldIn, double x, double y, double z, double accelX, double accelY, double accelZ) {
            super(RegistryHandler.HEARTBALL_PROJECTILE.get(), x, y, z, accelX, accelY, accelZ, worldIn);
        }
    
        public HeartballEntity(World worldIn, LivingEntity shooter, double accelX, double accelY, double accelZ) {
            super(RegistryHandler.HEARTBALL_PROJECTILE.get(), shooter, accelX, accelY, accelZ, worldIn);
        }
    
        /**
         * Called when this EntityFireball hits a block or entity.
         */
        protected void onImpact(RayTraceResult result) {
            super.onImpact(result);
            Entity entity = this.func_234616_v_();
            if (result.getType() != RayTraceResult.Type.ENTITY || !((EntityRayTraceResult)result).getEntity().isEntityEqual(entity)) {
                if (!this.world.isRemote) {
                    List<LivingEntity> list = this.world.getEntitiesWithinAABB(LivingEntity.class, this.getBoundingBox().grow(4.0D, 2.0D, 4.0D));
                    AreaEffectCloudEntity areaeffectcloudentity = new AreaEffectCloudEntity(this.world, this.getPosX(), this.getPosY(), this.getPosZ());
                    if (entity instanceof LivingEntity) {
                        areaeffectcloudentity.setOwner((LivingEntity)entity);
                    }
    
                    areaeffectcloudentity.setParticleData(ParticleTypes.DRAGON_BREATH);
                    areaeffectcloudentity.setRadius(3.0F);
                    areaeffectcloudentity.setDuration(600);
                    areaeffectcloudentity.setRadiusPerTick((7.0F - areaeffectcloudentity.getRadius()) / (float)areaeffectcloudentity.getDuration());
                    areaeffectcloudentity.addEffect(new EffectInstance(Effects.INSTANT_DAMAGE, 1, 1));
                    if (!list.isEmpty()) {
                        for(LivingEntity livingentity : list) {
                            double d0 = this.getDistanceSq(livingentity);
                            if (d0 < 16.0D) {
                                areaeffectcloudentity.setPosition(livingentity.getPosX(), livingentity.getPosY(), livingentity.getPosZ());
                                break;
                            }
                        }
                    }
    
                    this.world.playEvent(2006, this.getPosition(), this.isSilent() ? -1 : 1);
                    this.world.addEntity(areaeffectcloudentity);
                    this.remove();
                }
    
            }
        }
    
        /**
         * Returns true if other Entities should be prevented from moving through this Entity.
         */
        public boolean canBeCollidedWith() {
            return false;
        }
    
        /**
         * Called when the entity is attacked.
         */
        public boolean attackEntityFrom(DamageSource source, float amount) {
            return false;
        }
    
        protected IParticleData getParticle() {
            return ParticleTypes.DRAGON_BREATH;
        }
    
        protected boolean isFireballFiery() {
            return false;
        }
    }

     

    TwoDotXMod.java (Main class)

    package com.agilapathy.twodotx;
    
    import com.agilapathy.twodotx.capabilities.*;
    import com.agilapathy.twodotx.entities.heartball.HeartballEntity;
    import com.agilapathy.twodotx.entities.heartball.HeartballRenderer;
    import com.agilapathy.twodotx.gen.GenerationEvents;
    import com.agilapathy.twodotx.util.ClientEvents;
    import com.agilapathy.twodotx.util.RegistryHandler;
    import com.agilapathy.twodotx.util.functions.*;
    import net.minecraft.block.DispenserBlock;
    import net.minecraft.client.Minecraft;
    import net.minecraft.client.renderer.ItemRenderer;
    import net.minecraft.client.renderer.entity.EntityRenderer;
    import net.minecraft.client.renderer.entity.EntityRendererManager;
    import net.minecraft.client.renderer.entity.SpriteRenderer;
    import net.minecraft.entity.EntityType;
    import net.minecraft.item.ItemGroup;
    import net.minecraft.item.ItemStack;
    import net.minecraft.item.Items;
    import net.minecraft.util.ResourceLocation;
    import net.minecraftforge.common.MinecraftForge;
    import net.minecraftforge.eventbus.api.EventPriority;
    import net.minecraftforge.fml.client.registry.ClientRegistry;
    import net.minecraftforge.fml.client.registry.IRenderFactory;
    import net.minecraftforge.fml.client.registry.RenderingRegistry;
    import net.minecraftforge.fml.common.Mod;
    import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
    import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
    import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
    import net.minecraftforge.fml.network.NetworkRegistry;
    import net.minecraftforge.fml.network.simple.SimpleChannel;
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    
    import java.util.function.Supplier;
    
    // The value here should match an entry in the META-INF/mods.toml file
    @Mod("twodotx")
    public class TwoDotXMod
    {
        private static final String PROTOCOL_VERSION = "1";
        public static final SimpleChannel PACKET_HANDLER = NetworkRegistry.newSimpleChannel(new ResourceLocation("twodotx", "twodotx"),
                () -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals);
    
        // Directly reference a log4j logger.
        private static final Logger LOGGER = LogManager.getLogger();
        public static final String MOD_ID = "twodotx";
    
        public TwoDotXMod() {
            // Register the setup method for modloading
            FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup);
            //FMLJavaModLoadingContext.get().getModEventBus().addListener(GenerationMain::generate);
    
            // Register the doClientStuff method for modloading
            FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff);
    
            //Register Other Stuff
    
            RegistryHandler.init();
    
            // Register ourselves for server and other game events we are interested in
            MinecraftForge.EVENT_BUS.register(this);
        }
    
        private void setup(final FMLCommonSetupEvent event)
        {
        }
    
        private void doClientStuff(final FMLClientSetupEvent event)
        {
            RenderingRegistry.registerEntityRenderingHandler(RegistryHandler.HEARTBALL_PROJECTILE.get(), HeartballRenderer::new);
        }
    
    }

     

    RegistryHandler.java

    package com.agilapathy.twodotx.util;
    
    import com.agilapathy.twodotx.TwoDotXMod;
    import com.agilapathy.twodotx.blocks.*;
    import com.agilapathy.twodotx.entities.heartball.HeartballEntity;
    import com.agilapathy.twodotx.items.*;
    import com.agilapathy.twodotx.util.enums.CustomArmorMaterial;
    import com.agilapathy.twodotx.util.enums.CustomItemTier;
    import net.minecraft.block.*;
    import net.minecraft.enchantment.Enchantment;
    import net.minecraft.enchantment.EnchantmentType;
    import net.minecraft.entity.EntityClassification;
    import net.minecraft.entity.EntityType;
    import net.minecraft.inventory.EquipmentSlotType;
    import net.minecraft.item.*;
    import net.minecraft.util.ResourceLocation;
    import net.minecraft.util.SoundEvent;
    import net.minecraftforge.fml.RegistryObject;
    import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
    import net.minecraftforge.registries.DeferredRegister;
    import net.minecraftforge.registries.ForgeRegistries;
    
    import static net.minecraftforge.registries.DeferredRegister.create;
    
    public class RegistryHandler
    {
        public static final DeferredRegister<EntityType<?>> ENTITIES = create(ForgeRegistries.ENTITIES, TwoDotXMod.MOD_ID);
    
        public static void init()
        {
            ENTITIES.register(FMLJavaModLoadingContext.get().getModEventBus());
        }
    
        //Entities
        public static final RegistryObject<EntityType<HeartballEntity>> HEARTBALL_PROJECTILE = ENTITIES.register("heartball", () -> EntityType.Builder.<HeartballEntity>create(HeartballEntity::new, EntityClassification.MISC).size(0.25F, 0.25F).trackingRange(4).func_233608_b_(10).build("heartball"));
    }

     

  3. On 2/25/2021 at 11:57 PM, diesieben07 said:

    Use RenderingRegistry.registerEntityRenderingHandler to register entity renderers.

    I tried this with no luck:

    RenderingRegistry.registerEntityRenderingHandler(RegistryHandler.HEARTBALL_PROJECTILE.get(), HeartballRenderer::new);

     

  4. My projectile is working completely as intended, but it's invisible for some reason. I think it has something to do with how I'm registering its renderer.

     

    Here I'm using an item to spawn it, identical to how a dragon would shoot a fireball: 

     

     

    Here's my Registry Object entry:

    public static final RegistryObject<EntityType<HeartballEntity>> HEARTBALL_PROJECTILE = ENTITIES.register("heartball", () -> EntityType.Builder.<HeartballEntity>create(HeartballEntity::new, EntityClassification.MISC).size(0.25F, 0.25F).trackingRange(4).func_233608_b_(10).build("heartball"));

    Here's the code in my main class that's handling the registry of the rendering:

    private void doClientStuff(final FMLClientSetupEvent event)
        {
            registerEntityModels(event.getMinecraftSupplier());
        }
    
        private void registerEntityModels(Supplier<Minecraft> minecraft)
        {
    
            EntityRendererManager renderer = minecraft.get().getRenderManager();
    
            renderer.register(RegistryHandler.HEARTBALL_PROJECTILE.get(), new HeartballRenderer(renderer));
    
        }

    (FMLJavaModLoadingContext.get().getModEventBus().addListener(this::doClientStuff); is called in the constructor.)

     

    HeartballEntity and HeartballRenderer are identical to DragonFireballEntity and DragonFireballRenderer for example purposes, with the exception of the texture in HeartballRenderer, which I have changed to a target texture of my own:

    private static final ResourceLocation HEARTBALL_TEXTURE = new ResourceLocation(TwoDotXMod.MOD_ID + ":textures/entity/heartball.png");

     

    I appreciate any help, let me know if more information is needed.

     

    Cheers.

  5. Hey all,

     

    I'm trying to get something to happen when a beehive or bee nest is harvested (Getting honeycombs from it) but I've already tried overriding shears (it's called on the block before the onUtemUse of the shears, so that's never called when the honey level is 5 on a bee nest or beehive) I also tried overriding the vanilla block, but bee's seem to be able to go into the hive, just not make honey It would be nice if there was some sort of PlayerEvent.HarvestBeehive event or something, but I can't seem to find one that can match that.

     

    Any other ideas, or maybe I'm missing something?

     

    Thanks for the help guys.

  6. Just now, diesieben07 said:

    Okay so it is a normal capability. Then why are you not just making normal fields in it that you put values in?

    Because I wanted the ability to add custom values when setting them. For example, I wouldn't have to go into the capability and add a new variable, I can just say "Set the variable 'Working' to 'true'."

    And if its trying to get a variable that hasn't been set yet, it'll return 0 or "" depending on what kind of variable I set it as, (Notice the "string" argument.)

     

    Its sort of mimicking the stuff you can do with getPersistentData() on an entity. TLDR; Convenience. ¯\_(ツ)_/¯

  7. Got it. Moved it to the client event class. Everything seems to be working.

     

    5 minutes ago, diesieben07 said:

    You don't need this "capability" stuff... If you do need to store data on players, use actual Forge capabilities.

    It is a Forge capability, with an interface, class, provider, and storage, along with a AttachCapabilitiesEvent<Entity> called in a class to attach it to PlayerEntity's. Sorry if that wasn't clear in my last post. Unless you're talking about a capability that is already in Forge?

  8. 36 minutes ago, diesieben07 said:

    You need to use the SRG name, this won't work outside the development environment.

     

    What...? What... Just... what is this?

     

    This will crash on a server, as the server has no idea about the Minecraft class.

     

    Client-only events must be in a separate @EventBusSubscriber class with Dist.CLIENT passed to @EventBusSubscriber.

    Alright, put this in a class that's only for client events.

     

    private static final Field field = ObfuscationReflectionHelper.findField(Minecraft.class, "field_71467_ac");
    
        @SubscribeEvent
    
        public static void onClientTickEvent(TickEvent.ClientTickEvent event) throws IllegalAccessException {
    
            if(event.phase == TickEvent.Phase.START)
            {
                try {
    
                    if(CustomCapabilities.getPlayer(Minecraft.getInstance().player).getVariable("string", "HasBrokenMyBlock").equals("true")){
                        field.setInt(Minecraft.getInstance(), 0);
                        CustomCapabilities.getPlayer(Minecraft.getInstance().player).setVariable("HasBrokenMyBlock", "false");
                    }
    
                } catch (NullPointerException e) {}
            }
        }

     

    I removed

    38 minutes ago, diesieben07 said:

    if(event.getPlayer() == Minecraft.getInstance().player){

    here is the new code that stayed in that class:

     

    @SubscribeEvent
        public void farmBlock(PlayerInteractEvent.RightClickBlock event) {
            if(event.getPlayer().getHeldItem(event.getHand()).getItem().equals(Items.SHEARS) && event.getWorld().getBlockState(event.getPos()).equals(RegistryHandler.MY_BLOCK.get().getDefaultState())){
                CustomCapabilities.getPlayer(event.getPlayer()).setVariable("HasBrokenMyBlock", "true");
            }
        }

     

    And

    39 minutes ago, diesieben07 said:

    CustomCapabilities.getPlayer(Minecraft.getInstance().player).getVariable("string", "HasBrokenMyBlock").equals("true")

    is the custom capability system of mine that I was talking about. You can think of it as setting a value on a capability that is attached to PlayerEntitys. The real variable-setting stuff is buried under a bunch of static methods for convenience.

     

    This seems to be the only way to put a boolean value on my player that really just functions to link the functions of the RightClickBlock event to the ClientTickEvent. I suppose if there was a better way to link them, I wouldn't have to do this.

     

    Everything works, even when the client logged into the server.

  9. 1 hour ago, diesieben07 said:

    You cannot do this. The player can and will change.

     

    You cannot do this. You are reaching across logical sides here.

     

    ObfuscationReflectionHelper.findField

     

    I believe this new version covers everything you said. I used an attached player capability for the boolean.

    private static final Field field = ObfuscationReflectionHelper.findField(Minecraft.class, "rightClickDelayTimer");
    
        @SubscribeEvent
        public void onClientTickEvent(TickEvent.ClientTickEvent event) throws IllegalAccessException {
    
            if(event.phase == TickEvent.Phase.START)
            {
                try {
    
                    if(CustomCapabilities.getPlayer(Minecraft.getInstance().player).getVariable("string", "HasBrokenMyBlock").equals("true")){
                        field.setInt(Minecraft.getInstance(), 0);
                        CustomCapabilities.getPlayer(Minecraft.getInstance().player).setVariable("HasBrokenMyBlock", "false");
                    }
    
                } catch (NullPointerException e) {}
            }
        }
    
        @SubscribeEvent
        public void farmBlock(PlayerInteractEvent.RightClickBlock event) {
            if(event.getPlayer() == Minecraft.getInstance().player){
                if(event.getPlayer().getHeldItem(event.getHand()).getItem().equals(Items.SHEARS) && event.getWorld().getBlockState(event.getPos()).equals(RegistryHandler.MY_BLOCK.get().getDefaultState())){
                    CustomCapabilities.getPlayer(event.getPlayer()).setVariable("HasBrokenMyBlock", "true");
                }
            }
        }

     

    Everthing works, but a server instance isn't running because it doesn't like the ClientTickEvent: "Attempted to load class net/minecraft/client/entity/player/ClientPlayerEntity for invalid dist DEDICATED_SERVER"

     

    Tried to use regular TickEvent and it gave me the same error. :/

  10. 31 minutes ago, diesieben07 said:

    You cannot do that. That is a server-side event.

     

    Do not get the Field instance every time, get it once and store it in a static final field. Then also note that Minecraft fields will be obfuscated outside the development environment, so you need to use ObfuscationReflectionHelper and give it the SRG name instead of plain reflection.

     

    I see. Okay, so I did this, and it works, but am I doing it wrong still?:

     

    private static boolean brokeMyBlock = false;
    
        @SubscribeEvent
        public void onClickTickEvent(TickEvent.ClientTickEvent event){
    
            PlayerEntity player = Minecraft.getInstance().player;
    
            if(brokeMyBlock){
                System.out.println("Sheared Block");
                ObfuscationReflectionHelper.setPrivateValue(Minecraft.class, Minecraft.getInstance(), 0, "rightClickDelayTimer");
                brokeMyBlock = false;
            }
        }
    
        @SubscribeEvent
        public void farmMyBlock(PlayerInteractEvent.RightClickBlock event) {
            if(event.getPlayer() == Minecraft.getInstance().player){
                if(event.getPlayer().getHeldItem(event.getHand()).getItem().equals(Items.SHEARS) && event.getWorld().getBlockState(event.getPos()).equals(RegistryHandler.MY_BLOCK.get().getDefaultState())){
                    brokeMyBlock = true;
                }
            }
        }

     

    32 minutes ago, diesieben07 said:

    Do not get the Field instance every time, get it once and store it in a static final field.

    Couldn't see how to fit this in, because from what I saw, ObfuscationReflectionHelper didn't have a variation that uses a Field.

     

    But I did put some code for it, don't know how to put the puzzles pieces together though with what I did above:

    private static final Field field = rightClickDelayTimerField();
    
        private static Field rightClickDelayTimerField() {
            try {
                return Minecraft.getInstance().getClass().getDeclaredField("rightClickDelayTimer");
            } catch (NoSuchFieldException e) {
                return null;
            }
        }

     

  11. 2 minutes ago, diesieben07 said:

    Minecraft#player is the player that you are "controlling".

    Okay cool, I did the reflection straight in the PlayerInteractEvent.RightClickBlock method though, and everything's working A-Okay. Thanks for the help man. Here's my code for anyone that views this thread:

     

    Actual Method:

    @SubscribeEvent
        public void farmBlock(PlayerInteractEvent.RightClickBlock event) throws IllegalAccessException, NoSuchFieldException, ClassNotFoundException {
            if(event.getPlayer().getHeldItem(event.getHand()).getItem().equals(Items.SHEARS) && event.getWorld().getBlockState(event.getPos()).equals(RegistryHandler.MY_BLOCK.get().getDefaultState())){
                JavaStuff.setInstanceValue(Minecraft.getInstance(), "rightClickDelayTimer", 0);
            }
        }

     

    And since I see a lot of people that don't know how to do reflection and need it done, here you go. Click the link in the comment for more ways to do it, like with static variables and stuff. Chances are you'll only be doing it with non-static variables though, since literally Minecraft itself is an instance in this situation lol:

    public static void setInstanceValue(final Object classInstance, final String fieldName, final Object newValue) throws SecurityException,
                NoSuchFieldException, ClassNotFoundException, IllegalArgumentException, IllegalAccessException {
            // Get the private field
            final Field field = classInstance.getClass().getDeclaredField(fieldName);
            // Allow modification on the field
            field.setAccessible(true);
            // Sets the field to the new value for this instance
            field.set(classInstance, newValue);
    
        }
    
        // Via https://blog.sevagas.com/Modify-any-Java-class-field-using-reflection
        //Thanks Sevagas!

     

  12. 1 hour ago, diesieben07 said:

    I think this is Minecraft#rightClickDelayTimer. You can subscribe to TickEvent.ClientTickEvent, check for Phase.START and set it to 0 to forcibly disable this cooldown.

    Okay, so I'll set Minecraft#rightClickDelayTimer to 0 through reflection, but won't this set the variable globally? Because TickEvent.ClientTickEvent doesn't have a "player" variable or "getPlayer()" method. 

  13. Hey all, I'm trying to find a way to decrease the interval in which onItemUse() is fired on an item. For my application, I'm trying to increase the use time of my overwritten vanilla shears on a certain block that yields drops after you shear it. I want it to shear as fast as someone would punch out a bunch of tall grass.

     

    Here's my code in attempt to set the swinging time to zero when detecting the block being right clicked in the RightClickBlock event, sadly it doesn't work:

    @SubscribeEvent
        public void farmBlock(PlayerInteractEvent.RightClickBlock event)
        {
            if(event.getPlayer().getHeldItem(event.getHand()).getItem().equals(Items.SHEARS) && event.getWorld().getBlockState(event.getPos()).equals(RegistryHandler.MY_BLOCK.get().getDefaultState())){
                System.out.println("My block was right clicked!"); //Testing purposes.
                event.getPlayer().swingProgressInt = 1;
            }
        }

     

    I also have code in the onItemUse() function in my new vanilla shears class that allows the player to shear my block (turns it into another block) much like using an axe to right click will shave wood. Everything works the way that it should, I just want to decrease the time in which the item is used. :)

     

    Like said before, I can change the shears however I want, as long as this works. Any pointers?

     

    Thanks a ton!

  14. 1 hour ago, Draco18s said:

    Here is a forum. It operates under "forum" rules: threads stay "open" by default. The only reason to close threads (as in, a moderator locks it) is if it has been necro'd or is off topic (asking for help with unsupported versions falls into "off topic").

     

    I don't know why so many people think that this place operates likes Stack Overflow.

    Sounds good, thanks for the info! 😁

  15. 6 hours ago, ChampionAsh5357 said:

    Deprecation of randomTick and tick have nothing to do with overriding the methods. The deprecation is for usage. When calling those methods, you should call them through BlockState rather than through Block.

     

    As for why it won't work, you probably didn't add tickRandomly() to your block's properties. Deprecated methods will still work regardless.

    Gotcha, I was able to utilize this and use tick() to check for surrounding blocks.

     

    5 hours ago, poopoodice said:

    use

    
    worldIn.getPendingBlockTicks().scheduleTick

    to tick

    This method is definitely a way to go for some actions, but unfortunately it didn't work for me,

     

    By putting this in onBlockPlace() and also putting it in tick(), I was able to make someone of a consistent tick loop that ran for every interval that the tick scheduler was set at. However, it was inconsistent as random ticks would overlap it and start new tick loops. Removing  the tickRandomly() also wouldn't work for natural block appearances.

     

    Basically any solution using this was going to lag out my game or just be straight up inconsistent. I really appreciate you showing me how to schedule a tick though, it is what I asked to do in the first place after all.

     

    End Result:

    I ended up using tickRandomly() and tick() like so:

     

    public MyBlock() {
        super(Properties.create(Material.ROCK)
        .harvestTool(ToolType.PICKAXE)
        .harvestLevel(2)
        .hardnessAndResistance(2.5f, 7f)
        .sound(SoundType.field_235583_E_)
        .func_235861_h_()
                        .tickRandomly()
        );
    }
    
    @Override
    public void tick(BlockState state, ServerWorld worldIn, BlockPos pos, Random rand) {
        super.tick(state, worldIn, pos, rand);
    
        if(JavaStuff.randomPercentChance(100))
        {
            

     

    Thanks guys for the help!! 😄

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.