Re-added PlayerEvent.NameFormat (#6992)

This commit is contained in:
Tschipp 2020-07-27 23:16:35 +02:00 committed by GitHub
parent 27798c5804
commit 941b9a1d1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 11 deletions

View file

@ -288,19 +288,20 @@
this.field_71068_ca += p_82242_1_;
if (this.field_71068_ca < 0) {
this.field_71068_ca = 0;
@@ -1704,7 +1745,10 @@
@@ -1704,7 +1745,11 @@
}
public ITextComponent func_145748_c_() {
- IFormattableTextComponent iformattabletextcomponent = ScorePlayerTeam.func_237500_a_(this.func_96124_cp(), this.func_200200_C_());
+ if (this.displayname == null) this.displayname = net.minecraftforge.event.ForgeEventFactory.getPlayerDisplayName(this, this.func_200200_C_());
+ IFormattableTextComponent iformattabletextcomponent = new StringTextComponent("");
+ iformattabletextcomponent = prefixes.stream().reduce(iformattabletextcomponent, IFormattableTextComponent::func_230529_a_);
+ iformattabletextcomponent = iformattabletextcomponent.func_230529_a_(ScorePlayerTeam.func_237500_a_(this.func_96124_cp(), this.func_200200_C_()));
+ iformattabletextcomponent = iformattabletextcomponent.func_230529_a_(ScorePlayerTeam.func_237500_a_(this.func_96124_cp(), this.displayname));
+ iformattabletextcomponent = suffixes.stream().reduce(iformattabletextcomponent, IFormattableTextComponent::func_230529_a_);
return this.func_208016_c(iformattabletextcomponent);
}
@@ -1956,4 +2000,37 @@
@@ -1956,4 +2001,45 @@
return this.field_221260_g;
}
}
@ -313,6 +314,14 @@
+ public Collection<IFormattableTextComponent> getSuffixes() {
+ return this.suffixes;
+ }
+
+ private ITextComponent displayname = null;
+ /**
+ * Force the displayed name to refresh, by firing {@link net.minecraftforge.event.entity.player.PlayerEvent.NameFormat}, using the real player name as event parameter.
+ */
+ public void refreshDisplayName() {
+ this.displayname = net.minecraftforge.event.ForgeEventFactory.getPlayerDisplayName(this, this.func_200200_C_());
+ }
+
+ private final net.minecraftforge.common.util.LazyOptional<net.minecraftforge.items.IItemHandler>
+ playerMainHandler = net.minecraftforge.common.util.LazyOptional.of(

View file

@ -245,7 +245,7 @@ public class ForgeEventFactory
return maxCanSpawnEvent.getResult() == Result.ALLOW ? maxCanSpawnEvent.getMaxPackSize() : entity.getMaxSpawnedInChunk();
}
public static String getPlayerDisplayName(PlayerEntity player, String username)
public static ITextComponent getPlayerDisplayName(PlayerEntity player, ITextComponent username)
{
PlayerEvent.NameFormat event = new PlayerEvent.NameFormat(player, username);
MinecraftForge.EVENT_BUS.post(event);

View file

@ -33,6 +33,7 @@ import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.ITextComponent;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.eventbus.api.Event;
@ -152,26 +153,27 @@ public class PlayerEvent extends LivingEvent
**/
public static class NameFormat extends PlayerEvent
{
private final String username;
private String displayname;
private final ITextComponent username;
private ITextComponent displayname;
public NameFormat(PlayerEntity player, String username) {
public NameFormat(PlayerEntity player, ITextComponent username)
{
super(player);
this.username = username;
this.setDisplayname(username);
}
public String getUsername()
public ITextComponent getUsername()
{
return username;
}
public String getDisplayname()
public ITextComponent getDisplayname()
{
return displayname;
}
public void setDisplayname(String displayname)
public void setDisplayname(ITextComponent displayname)
{
this.displayname = displayname;
}

View file

@ -0,0 +1,20 @@
package net.minecraftforge.debug.entity.player;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod("player_name_event_test")
@Mod.EventBusSubscriber()
public class PlayerNameEventTest
{
private static final boolean ENABLE = false;
@SubscribeEvent
public static void onPlayerNameEvent(PlayerEvent.NameFormat event)
{
if (!ENABLE) return;
event.setDisplayname(new StringTextComponent("Test Name"));
}
}

View file

@ -72,4 +72,6 @@ loaderVersion="[28,)"
[[mods]]
modId="raid_enum_test"
[[mods]]
modId="modded_overworld_biomes_test"
modId="player_name_event_test"
[[mods]]
modId="modded_overworld_biomes_test"