Jump to content

[1.7.2]Spurious PlayerUseItemEvent on specific servers only? SOLVED


PurpleRain

Recommended Posts

I'm trying to add a bow charge indicator to the game overlay - a green transparent rectangle showing up at full charge.

I'm registerig a Event listener to get the Bow use:

public void load(FMLInitializationEvent event){
public static final BowListener bowListener = new BowListener();
...	
MinecraftForge.EVENT_BUS.register(bowListener);

and I track the bow charge

public class BowListener {

public boolean scopeOn = false;
public float charge;

@SubscribeEvent
public void startUsingBow (PlayerUseItemEvent.Start event){
	if (itemNotBow(event.item.getItem(), true)) return;
	openScope();
	charge = calcCharge(event.item.getItem(), event.duration);
}

@SubscribeEvent
public void continueUsingBow (PlayerUseItemEvent.Tick event){
	if (itemNotBow(event.item.getItem(), false)) return;
	charge = calcCharge(event.item.getItem(), event.duration);
}

@SubscribeEvent
public void stopUsingBow (PlayerUseItemEvent.Stop event){
	if (itemNotBow(event.item.getItem(), false))return;
	cancelScope();
}

private boolean itemNotBow (Item item, boolean start){
	if (item instanceof ItemBow) {
		if (start && scopeOn) {
			FMLLog.info("DDDDDD>>> startUsingBow but scope still active");
		}
		if (!start && !scopeOn) {
			FMLLog.info("DDDDDD>>> UsingBow but scope not active");
		}
		return false;
	}
	if (scopeOn) {
		FMLLog.info("DDDDDD>>> UsingOtherThanBow but scope still active");
	}
	cancelScope();
	return true;
}

private void cancelScope () {
	charge = 0.0F;
	scopeOn = false;
}

private void openScope () {
	scopeOn = true;
}

private float calcCharge (Item item, int duration) {

	int j = item.getMaxItemUseDuration(null) - duration;
        float f = (float)j / 20.0F;

        f = (f * f + f * 2.0F) / 3.0F;

        if ((double)f < 0.1D) return 0.0F;
        if (f > 1.0F) return 1.0F;

        return f;
}
}

 

This works great for most of the servers, BUT if i'm going to certain other servers like shotbow.net, I'm getting called for "impossible" PlayerUseItemEvents.

I have set a BP on the line which holds "FMLLog.info("DDDDDD>>> UsingOtherThanBow but scope still active");" and get following stackdump:

Thread [Client thread] (Suspended (breakpoint at line 45 in BowListener))	
BowListener.itemNotBow(Item, boolean) line: 45	
BowListener.continueUsingBow(PlayerUseItemEvent$Tick) line: 24	
ASMEventHandler_4_BowListener_continueUsingBow_Tick.invoke(Event) line: not available	
ASMEventHandler.invoke(Event) line: 51	
EventBus.post(Event) line: 122	
ForgeEventFactory.onItemUseTick(EntityPlayer, ItemStack, int) line: 149	
EntityOtherPlayerMP(EntityPlayer).onUpdate() line: 281	
EntityOtherPlayerMP.onUpdate() line: 72	
WorldClient(World).updateEntityWithOptionalForce(Entity, boolean) line: 2254	
WorldClient(World).updateEntity(Entity) line: 2214	
WorldClient(World).updateEntities() line: 2064	
Minecraft.runTick() line: 2065	
Minecraft.runGameLoop() line: 997	
Minecraft.run() line: 912	
Main.main(String[]) line: 112	
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]	
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available	
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available	
Method.invoke(Object, Object...) line: not available	
Launch.launch(String[]) line: 134	
Launch.main(String[]) line: 28	

 

and item is an ItemFood:"melon"  :o But I don't have any melons, not in inventory nor in hotbar.  >:(

 

Did someone already observe this?

 

:-[ I'm a noob - I forgot to check the player in the event.  I changed to:

public class BowListener {

public boolean scopeOn = false;
public float charge;

@SubscribeEvent
public void startUsingBow (PlayerUseItemEvent.Start event){

	if (itemNotBow(event)) return;
	scopeOn = true;
	charge = calcCharge(event.item.getItem(), event.duration);
}

@SubscribeEvent
public void continueUsingBow (PlayerUseItemEvent.Tick event){

	if (itemNotBow(event)) return;
	charge = calcCharge(event.item.getItem(), event.duration);
}

@SubscribeEvent
public void stopUsingBow (PlayerUseItemEvent.Stop event){

	if (itemNotBow(event)) return;
	charge = 0.0F;
	scopeOn = false;
}

private boolean itemNotBow (PlayerUseItemEvent event){

	if (event.entityPlayer.getEntityId() != Minecraft.getMinecraft().thePlayer.getEntityId()) return true; // someone other is using item ...
	if (event.item.getItem() instanceof ItemBow) return false;
	return true;
}

private float calcCharge (Item item, int duration) {

	int j = item.getMaxItemUseDuration(null) - duration;
        float f = (float)j / 20.0F;

        f = (f * f + f * 2.0F) / 3.0F;

        if ((double)f < 0.1D) return 0.0F;
        if (f > 1.0F) return 1.0F;

        return f;
}
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • How to fix file server-1.20.1-20230612.114412-srg.jar  
    • Just a few months ago I was creating my own plugin for Minecraft 1.20.2 spigot that did the same thing, but the skins were not saved, if you can understand this code that I made a long time ago it may help you.   //This is a class method private static String APIRequest(String value, String url, String toSearch) { try { URL api = new URL(url + value); HttpURLConnection connection = (HttpURLConnection) api.openConnection(); connection.setRequestMethod("GET"); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder response = new StringBuilder(); for (String responseChar; (responseChar = reader.readLine()) != null; ) response.append(responseChar); reader.close(); JSONObject responseObject = new JSONObject(response.toString()); if (!toSearch.equals("id")) return responseObject .getJSONArray("properties") .getJSONObject(0) .getString("value"); else return responseObject.getString("id"); } else { AntiGianka.ConsoleMessage(ChatColor.RED, String.format( "Could not get %s. Response code: %s", ((toSearch.equals("id")) ? "UUID" : "texture"), responseCode )); } } catch (MalformedURLException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } catch (IOException error) { AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while attempting to connect to the URL. Error: " + error); } return ""; } //other class method private void SkinGetter() { String uuid; String textureCoded; if ((uuid = APIRequest(args[0], "https://api.mojang.com/users/profiles/minecraft/", "id")).isEmpty() || (textureCoded = APIRequest(uuid, "https://sessionserver.mojang.com/session/minecraft/profile/", "value")).isEmpty() ) sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); else SkinSetter(textureCoded); } //other more private void SkinSetter(String textureCoded) { JSONObject profile = new JSONObject(new String(Base64.getDecoder().decode(textureCoded))); try { URL textureUrl = new URL(profile.getJSONObject("textures"). getJSONObject("SKIN"). getString("url")); if (sender instanceof Player && args.length == 1) { PlayerTextures playerTextures = ((Player) sender).getPlayerProfile().getTextures(); playerTextures.setSkin(textureUrl); ((Player) sender).getPlayerProfile().setTextures(playerTextures); if (((Player) sender).getPlayerProfile().getTextures().getSkin() != null) sender.sendMessage(((Player) sender).getPlayerProfile().getTextures().getSkin().toString()); else sender.sendMessage("Null"); sender.sendMessage("Skin changed successfully.a"); } else { } AntiGianka.ConsoleMessage(ChatColor.GREEN, "Skin command executed successfully."); } catch (MalformedURLException error) { sender.sendMessage(ChatColor.RED + String.format( "An error has occurred while trying to obtain the %s player skin, please check if the name %s is spelled correctly or try again later.", args[0], args[0] ) ); AntiGianka.ConsoleMessage(ChatColor.RED, "An error occurred while trying to access the URL. Error: " + error); } }  
    • Use /locate structure The chat should show all available structures as suggestion For the Ancient City it is /locate structure ancient_city
    • So does it work without this mod? Did you test it with other builds?
  • Topics

×
×
  • Create New...

Important Information

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